I am trying to deploy 6 identical virtual machines (except for their names etc.), and spread them across multiple availability zones.
So for example VM1 = Zone 1, VM2 = Zone 2, VM3 = Zone 3, VM4 = Zone 1 and so on.
The terraform code for VM creation I am using is as follows MAIN.TF
###################################
# VIRTUAL MACHINE ZONE 1 CREATION #
###################################
resource "azurerm_virtual_machine" "avd_vm" {
count = 2
name = "${var.avd_vm.name}${count.index 1}"
resource_group_name = var.rg.name
location = var.rg.location
vm_size = var.avd_vm.size
network_interface_ids = ["${azurerm_network_interface.avd_nic.*.id[count.index]}"]
license_type = var.avd_vm.license_type
delete_os_disk_on_termination = true
zones = ["1"]
storage_image_reference {
id = var.avd_vm.image_id
}
storage_os_disk {
name = "${lower(var.avd_vm.os_disk_name)}-${count.index 1}"
caching = var.avd_vm.caching
create_option = "FromImage"
managed_disk_type = var.avd_vm.managed_disk_type
}
os_profile {
computer_name = "${var.avd_vm.name}${count.index 1}"
admin_username = var.local_username.username
admin_password = var.local_password.password
}
os_profile_windows_config {
provision_vm_agent = true
timezone = var.avd_vm.timezone
}
depends_on = [
azurerm_network_interface.avd_nic
]
}
I can get it to deploy to a single zone, but not sure how to loop it and deploy to multiple zones.
Thanks Jon
CodePudding user response:
You can use the count.index
and with a bit of math we can loop over the 3 zones you need.
Code will look something like this:
resource "azurerm_virtual_machine" "avd_vm" {
count = 6
name = "${var.avd_vm.name}${count.index 1}"
resource_group_name = var.rg.name
location = var.rg.location
...
license_type = var.avd_vm.license_type
delete_os_disk_on_termination = true
zones = [ count.index % 3 1 ]
...
}