Attach multiple managed disks to multiple azure VM

632 views
Skip to first unread message

Jay

unread,
Jun 6, 2019, 11:33:08 PM6/6/19
to Terraform
Hello,

What I am trying to achieve is the following.

I would like to have a module which creates multiple VMs with multiple managed disks.

For example:
I have 3 disks-DATA, LOG, TMP
and I have 2 VMs: VM1 and VM2.
 I am using following terraform script.

variable "data_disk_sizes" {
  description
= "Storage data disk size size"
 
default     = ["DATA","LOG","TMPDB"]
}
variable
"data_disk_names" {
  description
= "Storage data disk names"
 
default     = ["100", "100","20"]
}


resource
"azurerm_virtual_machine" "vm-windows" {
    count
= 2
   
.
   
.
   
.
}

resource
"azurerm_managed_disk" "vm" {
  name                
= "${var.data_disk_names[count.index % length(var.data_disk_names)]}${count.index}"
  location            
= "${azurerm_resource_group.vm.location}"
  resource_group_name  
= "${azurerm_resource_group.vm.name}"
  storage_account_type
= "Standard_LRS"
  create_option        
= "Empty"
  disk_size_gb        
= "${var.data_disk_sizes[count.index % length(var.data_disk_sizes)]}"
  count                
= "${var.data_disk == "true" ? var.nb_instances *  length(var.data_disk_names) : 0}"
}

I have tried
this but no luck.

resource
"azurerm_virtual_machine_data_disk_attachment" "vm" {
  count              
= "${var.data_disk == "true" ?   length(azurerm_virtual_machine.vm-windows) *  length(azurerm_managed_disk.vm) : 0}"
  managed_disk_id    
= "${azurerm_managed_disk.vm[count.index % length(azurerm_managed_disk.vm)].id}"
  virtual_machine_id
= "${azurerm_virtual_machine.vm-windows[count.index %  length(azurerm_virtual_machine.vm-windows)].id}"
  lun                
= "${count.index  + 10}"
  caching            
= "ReadWrite"
  depends_on
= ["azurerm_virtual_machine.vm-windows"]

}



Reply all
Reply to author
Forward
0 new messages