Deploy Azure Virtual Desktop Session Hosts across availability zones with Terraform

This week Tom Hickling shared the news that the host pool deployment now supports availability zones during the AVD deployment. this new feature will speed up the process of deploying your session hosts in for example the 3 availability zones of West Europe.

For the people who want to read the official announcement click here.

In this blog post I’ll show you what Terraform code to add in your deployment for this to work. In this example I’m deploying 4 session hosts across the 3 availability zones of West Europe.

What is an availability zone?

Azure availability zones are physically and logically separated datacenters with their own independent power source, network, and cooling.

With 4 session hosts they will be placed like this:

  • avd-prd-jvn-1 > zone 1
  • avd-prd-jvn-2 > zone 2
  • avd-prd-jvn-3 > zone 3
  • avd-prd-jvn-4 > zone 1

Be sure to use the azurerm_windows_virtual_machine resource for the zones feature to work.

resource "azurerm_windows_virtual_machine" "vm" {
  name                  = "${var.vm_name}${count.index + 1}"
  location            = data.azurerm_resource_group.rg-sessionhosts.location
  resource_group_name =
  size               = "${var.vm_size}"
  network_interface_ids = ["${element(azurerm_network_interface.nic.*.id, count.index)}"]
  count                 = "${var.vm_count}"
  identity {
    type = "SystemAssigned"
    admin_username = "${var.admin_username}"
    admin_password = "${var.admin_password}"
  source_image_reference {
    publisher = "${var.image_publisher}"
    offer     = "${var.image_offer}"
    sku       = "${var.image_sku}"
    version   = "${var.image_version}"

  os_disk {
    name          = "${var.vm_name}${count.index + 1}-c"
    caching = "ReadWrite"
    storage_account_type = "Premium_LRS"

    provision_vm_agent = true
  zone = "${(count.index%3)+1}"

The most important piece of code for the availability zone is the following

 zone = "${(count.index%3)+1}"

The entire code for the host pool, session hosts and the extensions can be found on my github.

After deployment you can check the availability zone for each session host in the portal.

This concludes this blog post about AVD across availability zones. I hope this blog post is helpful for you and if you have any more questions, feel free to contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *