I want to deploy multiple virtual machines with different data disk sizes to each machine. Also, number of disk may vary from virtual machine to machine. I am using Azure. I am facing issues in iterating the vms_disks object to get disk sizes. Also, once disks gets created then need to map against particular virtual machine in code block ""azurerm_virtual_machine_data_disk_attachment"

Any help would be highly appreciated.

Tried this code

variable.tf
variable "instance_name" {
  type = list(object({
    vmname  = string
    disks = list(string)
 }))
}

variable "disks" {
  type = list(object({
    disk_name = string
    disk_type = string
    disk_size = number
 }))
}
dev.tfvars

disks = [
         {
        disk_name = "disk1"
        disk_type = "pd-standard"
        disk_size = 5
        },
        {
        disk_name = "disk01"
        disk_type = "pd-standard"
        disk_size = 10
        },
        {
        disk_name = "disk02"
        disk_type = "pd-standard"
        disk_size = 11
        }
    ]

 instance_name = [
  {
    vmname  = "vm1"
    disks =  [0]
   },
  {
   vmname  = "vm2"
   disks =  [0,2]
   }
]

main.tf

locals {

    vms_disks = merge([
      for vm_values in var.instance_name: {
        for disk_idx in vm_values.disks: 
             vm_values.vmname => var.disks[disk_idx]...
      }
    ]...)    
}

resource "azurerm_managed_disk" "external" {
  count = length(local.vms_disks)
  name =  local.vms_disks[count.index].disk_name
  location             = var.location
  resource_group_name  = var.resource_group_name
  storage_account_type = "Standard_LRS"
  create_option        = "Empty"
  // issue is here to fetch disk size.
  disk_size_gb         =  local.vms_disks[count.index].disk_size

}

resource "azurerm_virtual_machine_data_disk_attachment" "external" {
 
  count = length(local.vms_disks)
  managed_disk_id    = "${azurerm_managed_disk.external.*.id[count.index]}"
  // How to map local.vms_disks object to particular VM.
  virtual_machine_id = azurerm_linux_virtual_machine.hyvm.*.id[ceil((count.index + 1) * 1.0 / length(var.disks)) - 1]
  lun                = count.index + 10
  caching            = "ReadWrite"
   
}
0

There are 0 best solutions below