Associate Managed disk to multiple VMs

Hi All,

I am trying to create multiple vms and managed disk to associate after creation. I could see the disks are created and getting associated only for the first VM in the list. I am not sure how to assign the right index number in the below code.

I believe Virtual_Machin_id is creating this issue, has any one came across the similar, please advice.

resource “azurerm_managed_disk” “app_managed_disk” {

count = “${length(var.data_disk_names)}”

name = “{var.app_hostname}-{var.data_disk_names[count.index % length(var.data_disk_names)]}”

location = azurerm_resource_group.poc_rg.location

resource_group_name =

storage_account_type = var.app_disk_type

create_option = “Empty”

disk_size_gb = “${var.data_disk_sizes[count.index % length(var.data_disk_sizes)]}”


resource “azurerm_virtual_machine_data_disk_attachment” “app_disk_attach” {

count = “${length(var.data_disk_names)}”

vm_count = length(var.vm_app_name)

managed_disk_id = “${azurerm_managed_disk.app_managed_disk[count.index % length(azurerm_managed_disk.app_managed_disk)].id}”

virtual_machine_id =[0].id

lun = “${count.index + 1}”

caching = “ReadWrite”