Using for loop to create multiple instances of multiple data disks per server in Azure

Hello all. I am writing a module to deploy an Azure Windows VM that has multiple data disks. Easy enough, right? Here’s the catch. Each VM will have 3 different data disk types (size) and will have multiple instances of that data disk type. I am passing the data disks to the module like this:

disk_instances = [
    {
      name                 = "disk-app-${var.primary_region}-bld-rpt"
      location             = var.primary_region
      resource_group_name  = format("rg-app-${var.primary_region}-bld")
      storage_account_type = "Premium_LRS"
      create_option        = "Empty"
      disk_size_gb         = 512
      disk_iops_read_write = 2300
      disk_mbps_read_write = 150
      instance_count       = 8
    },
    {
      name                 = "disk-app-${var.primary_region}-bld-stg"
      location             = var.primary_region
      resource_group_name  = format("rg-app-${var.primary_region}-bld")
      storage_account_type = "Premium_LRS"
      create_option        = "Empty"
      disk_size_gb         = 1024
      disk_iops_read_write = 5000
      disk_mbps_read_write = 200
      instance_count       = 4
    }
]

I have built a nested for loop that I thought would iterate through each of the instance_count and create that number of that type of data disk. I can’t seem to get it to work, so any assistance would be greatly appreciated.

Here is the module that I have written:

locals {
  os_list = split("-", var.os_sku)

  publisher = contains(local.os_list, "enterprise") ? "MicrosoftSQLServer" : "MicrosoftWindowsServer"
  offer     = contains(local.os_list, "enterprise") ? "SQL2022-WS2022" : "WindowsServer"
  sku       = var.os_sku
  version   = "latest"

  

}

resource "random_password" "admin_password" {
  length  = 16
  special = true
}

resource "random_integer" "lun_id" {
  min = 0
  max = 9
}

resource "azurerm_key_vault_secret" "os_local_admin" {
  name         = format("${var.app_resource_name}-administrator")
  value        = random_password.admin_password.result
  key_vault_id = var.key_vault_id

}

resource "azurerm_network_interface" "network_interface" {
  name                = format("nic-${var.app_resource_name}-${var.app_resource_stamp}")
  location            = var.location
  resource_group_name = var.resource_group_name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = var.subnet_id
    private_ip_address_allocation = "Dynamic"
  }
}


#Create data disks
resource "azurerm_managed_disk" "data_disk" {
  for_each = merge(
    [for disk_instance in var.disk_instances : 
      { for instance_number in range(disk_instance.instance_count) : 
        "${disk_instance.name}_${instance_number}" => {
          name                 = format("%s-%d", disk_instance.name, instance_number)
          location             = disk_instance.location
          resource_group_name  = disk_instance.resource_group_name
          storage_account_type = disk_instance.storage_account_type
          create_option        = disk_instance.create_option
          disk_size_gb         = disk_instance.disk_size_gb
          disk_iops_read_write = disk_instance.disk_iops_read_write
          disk_mbps_read_write = disk_instance.disk_mbps_read_write
        }
      }
    ]
  )

  name                 = each.value.name
  location             = each.value.location
  resource_group_name  = each.value.resource_group_name
  storage_account_type = each.value.storage_account_type
  create_option        = each.value.create_option
  disk_size_gb         = each.value.disk_size_gb
  disk_iops_read_write = each.value.disk_iops_read_write
  disk_mbps_read_write = each.value.disk_mbps_read_write

}

#Define VMs
resource "azurerm_windows_virtual_machine" "windows_vm" {


  name                = format("vm-${var.app_resource_name}-${var.app_resource_stamp}")
  resource_group_name = var.resource_group_name
  location            = var.location
  size                = var.sku
  admin_password      = random_password.admin_password.result
  admin_username      = "app_admin"


  network_interface_ids = [azurerm_network_interface.network_interface.id]

  os_disk {
    storage_account_type = var.os_disk_type
    caching              = "ReadWrite"
    disk_size_gb         = var.os_disk_size

  }

  additional_capabilities {
    ultra_ssd_enabled = var.ultra_ssd_enabled
  }

  source_image_reference {
    publisher = local.publisher
    offer     = local.offer
    sku       = local.sku
    version   = local.version
  }

}

resource "azurerm_virtual_machine_data_disk_attachment" "disk_attachment" {

  for_each = azurerm_managed_disk.data_disk

  managed_disk_id    = each.value.id
  virtual_machine_id = azurerm_windows_virtual_machine.windows_vm.id
  lun                = format("%d%d", random_integer.lun_id, each.key)
  caching            = "ReadWrite"

}



output "windows_vm" {
  value = azurerm_windows_virtual_machine.windows_vm
}

output "data_disk_list" {
  value = azurerm_managed_disk.data_disk

}