Reputation: 2860
I have created VM and added Data Disk to be, however there is one issue.The VM name and data disk name DO NOT align. Please refer to the screenshot below.
The terraform code is below:
resource "azurerm_managed_disk" "tf-mdsk-cluster" {
count = 5
name = "${var.ax_base_hostname}-${count.index+1}-DATADISK-1"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1024"
}
resource "azurerm_managed_disk" "tf-mdsk-2-cluster" {
count = 5
name = "${var.ax_base_hostname}-${count.index+1}-DATADISK-2"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
storage_account_type = "Standard_LRS"
create_option = "Empty"
disk_size_gb = "1024"
}
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
count = 5
name = "${var.ax_base_hostname}-${count.index+1}"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
availability_set_id = "${azurerm_availability_set.tf-as-cluster-aos.id}"
network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index+1)}"]
vm_size = "${var.ax_vm_size}"
storage_data_disk {
name = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.name, count.index+1)}"
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index+1)}"
create_option = "Attach"
lun = 0
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.disk_size_gb, count.index+1)}"
}
storage_data_disk {
name = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.name, count.index+1)}"
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.id, count.index+1)}"
create_option = "Attach"
lun = 1
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.disk_size_gb, count.index+1)}"
}
}
I have changed the managed disk block
FROM
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index+1)}"
TO
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index)}"
But that has not solved the mystery, any idea as to how to resolve it?
Upvotes: 1
Views: 793
Reputation: 31462
I think there is something you have misunderstand and make it a wrong setting. When you create the managed disk, you set the index with count.index+1
. Then the data name will has the number {1,2,3,4,5} with the index {0,1,2,3,4}. And when you create the VM and add managed disk in it. You set the index in the storage_data_disk
also as count.index+1
. So when the index is 1 of the VM, the disk number will be 2.
You can change the index in the storage_data_disk
into count.index
. And it will work as you want. The azurerm_virtual_machine
will like this:
resource "azurerm_virtual_machine" "tf-vm-cluster-aos" {
count = 5
name = "${var.ax_base_hostname}-${count.index+1}"
location = "${azurerm_resource_group.tf-rg-cluster-aos.location}"
resource_group_name = "${azurerm_resource_group.tf-rg-cluster-aos.name}"
availability_set_id = "${azurerm_availability_set.tf-as-cluster-aos.id}"
network_interface_ids = ["${element(azurerm_network_interface.tf-ni-cluster-aos.*.id, count.index+1)}"]
vm_size = "${var.ax_vm_size}"
storage_data_disk {
name = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.name, count.index)}"
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.id, count.index)}"
create_option = "Attach"
lun = 0
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-cluster.*.disk_size_gb, count.index)}"
}
storage_data_disk {
name = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.name, count.index)}"
managed_disk_id = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.id, count.index)}"
create_option = "Attach"
lun = 1
disk_size_gb = "${element(azurerm_managed_disk.tf-mdsk-2-cluster.*.disk_size_gb, count.index)}"
}
}
Upvotes: 1