Trying to build iteration of multiple vm’s and inside iteration of multiple disks per VM as per input format and getting the below error. Please help
Error: Missing required argument
on modules/azure-vm/main.tf line 118, in resource “azurerm_virtual_machine” “vm”:
118: content {
The argument “name” is required, but no definition was found.
terrafor.tfvars
instance = [{name="test1",size="Standard_E4s_v4",os_disk_size_gb=512, data_disk_details=[{name = "data",lun = 0,size=1060,caching = "None",sku = "Premium_LRS",storage_url = "",accelerated = false},{name = "data1",lun = 1,size=4060,caching = "None",sku = "Premium_LRS",storage_url = "",accelerated = false}]},{name="test2",size="Standard_E2s_v4",os_disk_size_gb=128, data_disk_details=[{name = "data",lun = 0,size=2060,caching = "None",sku = "Premium_LRS",storage_url = "",accelerated = false}]}]
variables.tf
variable "instance" {
type = list(object({
name = string
os_disk_size_gb = number
size = string
data_disk_details = list(object({
name = string
lun = number
size = number
caching = string
accelerated = bool
sku = string
storage_url = string
}))
}))
}
main.tf
locals {
instance = flatten([
for value in var.instance: {
name = value.name
size = value.size
os_disk_size_gb = value.os_disk_size_gb
data_disks = [for value in value["data_disk_details"]: {
name = value.name != "" ? value.name : "disk-data-${value.lun}"
lun = value.lun
size = value.size
caching = value.caching != "" ? value.caching : "None"
accelerated = value.accelerated != "" ? value.accelerated : false
sku = value.sku != "" ? value.sku : "Standard_LRS"
storage_url = value.storage_url
}]
}])
instance_map = {
for value in var.instance: "${value.name}" => value
}
}
resource "azurerm_virtual_machine" "vm" {
for_each = local.instance_map
name = each.value.name
location = var.location
resource_group_name = var.resource_group_name
vm_size = each.value.size
dynamic "storage_data_disk" {
for_each = var.instance
iterator = object
content {
dynamic "data_disk_details" {
for_each = lookup(object.value, "data_disk_details", [])
content {
name = "${each.value.name}-${data_disk_detais.value["name"]}"
lun = data_disk_detais.value["lun"]
create_option = "Empty"
disk_size_gb = data_disk_detais.value["disk_size_gb"]
caching = data_disk_detais.value["caching"]
write_accelerator_enabled = data_disk_detais.value["write_accelerator_enabled"]
managed_disk_type = data_disk_detais.value["managed_disk_type"]
vhd_uri = data_disk_detais.value["storage_url"] != "" ? "${data_disk_detais.value["storage_url"]}/${each.value.name}-${data_disk_detais.value.name}.vhd" : null
}
}
}
}}