Creating and Testing an Availability Set with Terraform

Learn how to create and test an availability set with Terraform.

Let’s actually create an availability set and see how they work. In this chapter project, you’re going to build an availability set along with all of the usual resources that come with it, such as a set of VMs and a load balancer.

Project overview

This chapter project is going to cover all of the bases that you need covered to create and take advantage of VM availability sets. Since a VM availability set isn’t very good on its own (i.e., if you’re using it to host a web service), you need a load balancer too. To ensure this chapter provides a real-world perspective of availability sets, we’ll also create a load balancer. And since this is a DevOps course, after all, we’re not going to create all of this stuff in the Azure portal. Instead, we’ll build everything with Terraform!

By the end of this chapter project, you’ll learn how to build a set of VMs inside of an availability set, a load balancer, and we might even get around to adding additional VMs to the set and do some testing.

Tools to have

To follow along with the project in this chapter, please be sure that:

  • You have created an Azure service principal and have configured Terraform to connect to your Azure subscription. Details on how to do this are here. If you already have the Azure CLI configured, Terraform can also use that service principal.

Building the Terraform configuration

There are many tools to build an Azure VM availability set from the Azure Portal, ARM templates, PowerShell, the Azure CLI, Terraform, and others. You first need to decide on which tool to use. Since this is a course on DevOps, we’ve got to use an automation tool. One of the most popular tools is Terraform.

To build infrastructure with Terraform, you must first create a configuration file. Since this chapter isn’t on Terraform, we’re not going to cover the ins and outs of what it takes to build one. For now, you just need to get an availability set created with a few VMs in it.

To create this chapter’s VM availability set and VMs, first create a folder called NoBSAvailSetDemo. Then, head over to the BookResource GitHub repo and download the Terraform configuration file main.tf and the two variable definition files (terraform.tfvars and secrets.tfvars) into the NoBSAvailSetDemo folder. All of these files will eventually come together to not only build a VM availability set but also a set of common resources that typically accompany an availability set.

Assuming Terraform is available and you’ve already set all of the appropriate environment variables to authenticate Terraform, start building the infrastructure. To do so, run the following Terraform commands. These commands will first download the Azure Terraform provider, ensure the configuration files are valid, and finally provision all of the resources in the main.tf configuration file.

terraform init
terraform plan --var-file="secrets.tfvars"
terraform apply --var-file="secrets.tfvars" -auto-approve

The configuration file main.tf will create a group of resources to mimic a Remote Desktop Services (RDS) group. Users connecting to the public IP address should be able to RDP to a VM, regardless if one goes down or not.

Availability sets can also be created for other applications like web services, domain controllers, Kubernetes nodes, and more. Using RDS is just a simple example of the workload that takes advantage of availability sets.

While the Azure resources are deploying, here’s what that configuration creates:

  • An Azure virtual network and subnet to connect all of the VMs together.

  • An Azure availability set

  • Five Azure VMs running Windows Server 2019 in the availability set with NICs assigned to the subnet created.

  • A network security group to open up RDP on all VMs.

  • An Azure load balancer to distribute traffic to each VM in the availability set with:

    • A load balancer backend pool to send traffic to (all VMs in the availability set).
    • A frontend IP configuration attached to the public IP.
    • A load balancer rule to handle RDP traffic
    • A load balancer health probe to continually test RDP on all VMs in the availability set
  • A public IP to assign to the load balancer.

Try it yourself!

We have already configured an environment for you to practice working with Terraform. So, press the Run button, and try it yourself.

Get hands-on with 1000+ tech skills courses.