Azurerm_virtual_machine & Zones

I’m hoping someone will be able to point me in the right direction of how to place a VM into an Azure zone. This is easy when it’s just one VM you’re deploying but I’m doing a count to deploy 5 VMs and would like to spread them out over 3 Azure zones. For example, 2 in Zone 1, 2 in zone 2 and 1 in zone 3.

The Zone parameter is a ‘list of a single item’ there a way to on each iteration of the loop (using count =5) to set the zone?

Example code:

resource “azurerm_virtual_machine” “vm_app_servers” {
count = “${var.Number_of_App_Machines}”
zones = [" "] # This needs to be the zone number.

If I did something like this: zones = [“1”, “2”]

I get the error - Error: zones: attribute supports 1 item maximum, config has 2 declared. So I need a way to specify the zone.

Hope that makes sense?

Thanks, Joe

Here is what we do for that same scenario:

resource "azurerm_virtual_machine" "vm" {
  count                            = var.vm_count
  name                             = "${var.vm_name}${count.index + 1}"
  location                         = var.resource_group_details["location"]
  resource_group_name              = var.resource_group_details["name"]
  network_interface_ids            = [azurerm_network_interface.nic[count.index].id]
  zones                            = [element(split(",", var.av_zone), count.index)]
  vm_size                          = var.vm_sku
  delete_os_disk_on_termination    = "true"
  delete_data_disks_on_termination = "true"
}

Using element() will allow you to specify more VMs than there are zones and it will continue to loop through the list. For example you could pass in zones = [1,2,3] and vmcount = 5 and it will place them in zones 1, 2, 3, 1, 2, respectively