I made a generalized image of a Windows virtual machine with a separate data disk attached. In the Azure Portal, generalized images containing this data disk are well-created, but with terraform code, the following error occurs in this generalized image.
Virtual Machine Name: "test-vm"): performing CreateOrUpdate: unexpected status 400 (400 Bad Request) with error: InvalidParameter: StorageProfile.dataDisks.lun does not have required value(s) for image specified in storage profile.
│
│ with azurerm_windows_virtual_machine.vm["vm1"],
│ on vm.tf line 67, in resource "azurerm_windows_virtual_machine" "vm":
│ 67: resource "azurerm_windows_virtual_machine" "vm" {
My code is as below.
- var.tf
############################
### Location & Network
############################
### Location
variable "location" {
type = string
default = "Korea Central"
}
### Resource Group Valiable - Network
variable "network_rg" {
type = string
default = "test-vm-rg"
}
### Virtual Network Valiable
variable "network_vnet" {
type = string
default = "vnet-test-vm"
}
### Subnet Valiable
variable "network_subnet" {
type = string
default = "subnet-test-vm"
}
### Private IP Setting
variable "private-ip-type" {
type = string
default = "Static"
}
############################
### Resource & OS env
############################
### Resource Group
variable "system_rg" {
type = string
default = "test-vm-d-rg"
}
############################
### Image Information
############################
### Image Publisher
variable "vm_template" {
type = map(any)
default = {
vm1 = {
source_image_id = "/subscriptions/0000000000000/resourceGroups/test-vm-rg/providers/Microsoft.Compute/images/vm-test-vm-image-v1"
vm_name = "test-vm"
hostname = "test-vm"
os_publisher = "MicrosoftWindowsServer"
os_offer = "WindowsServer"
os_sku = "2022-datacenter-azure-edition"
license_type = "Windows_Server"
os_version = "latest"
osdisk_type = "StandardSSD_LRS"
osdisk_size = "128"
vm_type = "Standard_D2s_v5"
adminid = "testadmin"
private_ip = "10.10.10.10"
nic = "nic01"
os_disk = "osdisk01"
}
}
}
- vm.tf
data "azurerm_resource_group" "system_rg_name" {
name = var.system_rg
}
data "azurerm_subnet" "network_subnet" {
name = var.network_subnet
virtual_network_name = var.network_vnet
resource_group_name = var.network_rg
}
resource "azurerm_network_interface" "nic" {
for_each = var.vm_template
name = "${each.value.vm_name}-${each.value.nic}"
location = var.location
resource_group_name = data.azurerm_resource_group.system_rg_name.name
accelerated_networking_enabled = true
ip_configuration {
name = "internal"
subnet_id = data.azurerm_subnet.network_subnet.id
private_ip_address_allocation = var.private-ip-type
private_ip_address = each.value.private_ip
}
}
resource "azurerm_windows_virtual_machine" "vm" {
for_each = var.vm_template
name = each.value.vm_name
location = var.location
resource_group_name = data.azurerm_resource_group.system_rg_name.name
source_image_id = each.value.source_image_id
license_type = each.value.license_type
size = each.value.vm_type
network_interface_ids = [
azurerm_network_interface.nic[each.key].id,
]
identity {
type = "SystemAssigned"
}
os_disk {
name = "${each.value.vm_name}-${each.value.os_disk}"
caching = "ReadWrite"
storage_account_type = each.value.osdisk_type
disk_size_gb = each.value.osdisk_size
}
computer_name = each.value.hostname
admin_username = each.value.adminid
admin_password = data.azurerm_key_vault_secret.password.value
timezone = "Korea Standard Time"
}
How do I create code to restore the data disks contained in the generalized VM image?