Terraform's count meta attribute is not working when I'm trying to launch multiple Azure VMs, and also there's no such option available on Azure Management Console.
The code works just fine by launching just a single VM, and it doesn't even give any error during the plan or apply stages of execution.
resource "azurerm_virtual_machine" "main" {
name = "terra-vm"
location = "${var.location}"
count = 2
resource_group_name = "${var.resourcegpname}"
network_interface_ids = ["${element(var.netif, count.index)}"]
vm_size = "Standard_B1s"
delete_os_disk_on_termination = true
delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk1"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
custom_data = "sudo apt-get install apache2 -y && sudo systemctl
start apache2"
}
os_profile_linux_config {
disable_password_authentication = "false"
}
tags {
environment = "staging"
}
}
variable "netif" {
type = "list"
}
variable "resourcegpname" {}
variable "location" {}
Like AWS I was expecting it should launch a specified number of virtual machines.
I think it does not create just a single VM for you. The reason is that you just create a single OS disk and then the VMs are overridden one by one with the single OS disk, and that result in just a single VM at the end.
So you should create different OS disk name with your VM, the other resources will also need to create more, it all dependant on the number of your VM. You can do that like this:
resource "azurerm_virtual_machine" "test" {
count = 2
name = "acctvm${count.index}"
location = "${azurerm_resource_group.test.location}"
availability_set_id = "${azurerm_availability_set.avset.id}"
resource_group_name = "${azurerm_resource_group.test.name}"
network_interface_ids = ["${element(azurerm_network_interface.test.*.id, count.index)}"]
vm_size = "Standard_DS1_v2"
# Uncomment this line to delete the OS disk automatically when deleting the VM
# delete_os_disk_on_termination = true
# Uncomment this line to delete the data disks automatically when deleting the VM
# delete_data_disks_on_termination = true
storage_image_reference {
publisher = "Canonical"
offer = "UbuntuServer"
sku = "16.04-LTS"
version = "latest"
}
storage_os_disk {
name = "myosdisk${count.index}" <--here is core in your case
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
# Optional data disks
storage_data_disk {
name = "datadisk_new_${count.index}"
managed_disk_type = "Standard_LRS"
create_option = "Empty"
lun = 0
disk_size_gb = "1023"
}
storage_data_disk {
name = "${element(azurerm_managed_disk.test.*.name, count.index)}"
managed_disk_id = "${element(azurerm_managed_disk.test.*.id, count.index)}"
create_option = "Attach"
lun = 1
disk_size_gb = "${element(azurerm_managed_disk.test.*.disk_size_gb, count.index)}"
}
os_profile {
computer_name = "hostname"
admin_username = "testadmin"
admin_password = "Password1234!"
}
os_profile_linux_config {
disable_password_authentication = false
}
tags {
environment = "staging"
}
}
You can get the whole template from Create Multiple VMs through Terraform .
another way of fixing this is just omitting the os disk name:
storage_os_disk {
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
It will generate random names for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.