I’m stumped on how to get terraform to create multi vm with data disks in availability zones.
Here is my VM.tf
resource “azurerm_windows_virtual_machine” “vm” {
for_each = toset(var.instances)
admin_password = var.admin_password
admin_username = var.admin_username
location = var.location
name = each.key
network_interface_ids = [azurerm_network_interface.nic[format("%s", each.key)].id]
resource_group_name = data.azurerm_resource_group.dnatest.name
size = var.vmsize
zone = index(var.instances, each.value) + 1
os_disk {
caching = var.caching
storage_account_type = var.storage_account_type
name = "${each.key}-OS-DISK-01"
}
source_image_reference {
publisher = var.vm_publisher
offer = var.vm_offer
sku = var.vm_sku
version = "latest"
}
tags = {
"Company Name" = local.company_name
"System Type" = "${local.prod} ${local.vm}"
Function = local.function
OS = local.oswin
Subnet = local.frontend
"OS Version" = local.osver-svr2016
"Hardware Type" = local.vm
}
}
here is my local.tf
vm_datadiskdisk_count_map = { for k in toset(var.instances) : k => var.nb_disks_per_instance }
luns = { for k in local.datadisk_lun_map : k.datadisk_name => k.lun }
datadisk_lun_map = flatten([
for vm_name, count in local.vm_datadiskdisk_count_map : [
for i in range(count) : {
datadisk_name = format("%s_Data_Disk_%02d", vm_name, i)
lun = i
}
]
])
datadisk.tf
here is the part that fails when I try to set the zone
resource “azurerm_managed_disk” “managed_disk” {
for_each = toset([for j in local.datadisk_lun_map : j.datadisk_name])
name = replace(each.key, “_”, “-”)
location = var.location
resource_group_name = data.azurerm_resource_group.dnatest.name
storage_account_type = var.storage_account_type
create_option = “Empty”
disk_size_gb = var.disksize
zones = not sure what I put here. I have tried toset and it fails. Tried for each but it pulls the vm name not the zone?
tags = {
"Company Name" = local.company_name
"Hardware Type" = local.datadisk
}
}
resource “azurerm_virtual_machine_data_disk_attachment” “managed_disk_attach” {
for_each = toset([for j in local.datadisk_lun_map : j.datadisk_name])
managed_disk_id = azurerm_managed_disk.managed_disk[each.key].id
virtual_machine_id = azurerm_windows_virtual_machine.vm[element(split("_", each.key), 0)].id
lun = lookup(local.luns, each.key)
caching = “ReadWrite”
}