Deploying an Azure VM into an Availability Zone with Terraform
A simple guide to deploying an Azure VM into a chosen Availability Zone using Terraform. Perfect for learning how to improve resilience and follow Azure best practices with IaC.
High availability is a consideration that every Architect needs to think about when building cloud infrastructure. In Azure, Availability Zones help you achieve resiliency by distributing your resources across physically separate locations within a region. This ensures that even if one zone experiences downtime, your resources remain available.
Terraform, as an Infrastructure-as-Code (IaC) tool, makes deploying these resources simple, repeatable, and allows you to version control deployments. In this post, I’ll show you how to deploy an Azure Virtual Machine (VM) in a specific Availability Zone using Terraform.
Prerequisites
Before you start, you’ll need:
- An Azure subscription.
- Azure CLI is installed on your machine.
- Terraform is installed on your machine.
- Basic Terraform knowledge
Terraform configuration overview
I have uploaded the full Terraform configuration to GitHub so that you can retrieve the code from the repository there. I’ve tried to keep it small and tidy, with just three files:
- variables.tf – defines the availability zone
- main.tf – contains all the resource definitions
- outputs.tf – prints out some deployment information at the end
Choosing a zone: variables.tf
We’ve defined a variable file, keeping it simple now, with the Azure Subscription ID and Azure Availability Zone in there.
variable "availability_zone" {
description = "Azure availability zone (1, 2, or 3)"
type = string
default = "1"
}
This variable lets you choose which zone your VM should be placed in.
Azure identifies zones with string values "1", "2", and "3", so the Terraform variable is a string rather than a number.
Randomising the region: a quick detour
Before we get to the VM, there’s a small but fun addition in main.tf: a random region selector.
locals {
azure_regions = [
"ukwest",
"westeurope",
"francecentral",
"swedencentral"
]
selected_location = element(local.azure_regions, random_integer.region_index.result)
}
resource "random_integer" "region_index" {
min = 0
max = length(local.azure_regions) - 1
}
Each time you deploy, Terraform picks a random region from that list. This is something I use more often than not in my demos, but it might not be something you want to do in production, as you will probably need the predictability of where your resources are deployed.
Placing the VM in a zone: main.tf
The main.tf file might look busy at first glance. That’s because it handles a complete virtual machine deployment with all the necessary components within the file, such as resource group, virtual network, subnet, network security group (NSG), NIC, virtual machine and also saving the private key for access to the VM.
I’ve included all of this configuration to make it a great starter set for you; however if you already have existing networking, you can plug the VM into that.
The key section we want to focus on is when you are defining the VM configuration, you can define the availability zone that the VM should deploy into:
resource "azurerm_linux_virtual_machine" "vm" {
name = module.naming.linux_virtual_machine.name_unique
resource_group_name = azurerm_resource_group.rg.name
location = azurerm_resource_group.rg.location
size = "Standard_B1s"
admin_username = "azureuser"
network_interface_ids = [
azurerm_network_interface.nic.id
]
zone = var.availability_zone
admin_ssh_key {
username = "azureuser"
public_key = tls_private_key.vm_key.public_key_openssh
}
os_disk {
caching = "ReadWrite"
storage_account_type = "Standard_LRS"
}
source_image_reference {
publisher = "Canonical"
offer = "0001-com-ubuntu-server-jammy"
sku = "22_04-lts-gen2"
version = "latest"
}
}
How to deploy
To deploy this Terraform configuration, follow these steps:
Open a terminal and download the code to your machine:
git clone https://github.com/weeyin83/terraform-azure-vm-az.git
Open up your favourite editor, I like VS Code, and open up the Terraform folder you’ve just copied.
Create a new file named terraform.tfvars in the project folder. Add your details to it:
azure_subscription_id = "your-subscription-id"
availability_zone = "1" # Options: "1", "2", or "3"
Specifying your specific information and choices.
Once you’ve saved the file. Open up your terminal again, this time you need to log in to your Azure subscription.
Az login
Once logged in you can start the Terraform deployment. Prepare Terraform and download provider modules:
Terraform init
Then we can run the plan command to understand what the Terraform configuration will do:
Terraform plan
Once you are happy, you can confirm the deployment using:
Terraform apply -auto-approve
Within a few minutes, the resources will have been deployed and you can SSH using the output information to the Linux VM. And you can also check the Azure portal to check the availability zone deployment.

Conclusion
Deploying VMs in specific Availability Zones is a small Terraform configuration change, but can make a big difference in your resources’ resilience and allow you to follow design choices by your architects. Terraform helps you automate and manage this process efficiently, ensuring your infrastructure is reliable and easy to maintain.