Manage Azure Virtual Machine Scale Sets with Terraform
Azure Virtual Machine Scale Sets let you create and manage a group of load balanced virtual machines. You can then use autoscale rules to automatically increase or decrease the number of virtual machines that run your application.
In this tutorial, you will create a Virtual Machine Scale Set using Terraform, then configure two autoscale rules that adjust your application’s capacity based on CPU load.
Prerequisites
This tutorial assumes that you are familiar with the standard Terraform workflow. If you are new to Terraform, complete the Get Started tutorials first.
For this tutorial, you will need:
- Terraform v1.4+ CLI installed locally.
- An Azure account and subscription.
- Azure CLI installed and authenticated locally.
Clone example repository
In your terminal, clone the example repository.
Navigate to the cloned repository.
Review configuration
This repository contains the configuration to deploy a Virtual Machine Scale Set as well as the required network infrastructure.
Here, you will find the following files:
- The
main.tf
file defines the Azure provider version and configures it to deploy towestus3
Azure region. - The
network.tf
file defines the required network configuration, such as the virtual network, subnet, security group, and load balancer. - The
outputs.tf
file declares the output from the Terraform configuration that will display the application endpoint. - The
compute.tf
file creates the Virtual Machine Scale Set. - The
autoscale.tf
file creates an autoscale profile for the scale set with two autoscale rules.
Scale set configuration
Open the compute.tf
file to review the scale set configuration.
The azurerm_orchestrated_virtual_machine_scale_set
resource creates the scale set and configures the VM template, including the OS, source image, and networking. This resource then defines an os_profile
.
The user_data_base64
parameter uploads the user-data.sh
script to configure the software running on each machine. The os_profile
parameter sets the administrator username and uploads your SSH public key, allowing you to connect to each instance with SSH.
Finally, the scale set resource defines the source_image_reference
.
This parameter defines the OS image. In this case, the scale set creates virtual machines running Ubuntu 22.04.
Autoscale configuration
Open autoscale.tf
to review the autoscaling configuration.
The azurerm_monitor_autoscale_setting
resource configures the autoscale rules of the scale set created in compute.tf
and references it with the target_resource_id
parameter. It creates an autoscale profile that can deploy between 1 and 10 VMs. The default
parameter configures the scale set to deploy 3 VMs if there are no scaling rules or metrics available.
The example configuration has two autoscale rules commented out by default, which you will enable later.
Apply Configuration
In your terminal, initialize the configuration.
Now, apply the configuration to create the scale set, autoscale configuration, and network resources. Respond yes
to the prompt to confirm the operation.
Visit the application_endpoint
address output value in your browser to test out your application.
Note
It may take several minutes for the application to respond to requests.
Add autoscale rules
Use the az
CLI to confirm that the scale set created 3 virtual machines.
Open autoscale.tf
and uncomment the two autoscale rules.
These two rules tell the scale set to add a VM when the average CPU load is greater than 90% and to remove a VM when it is less than 10%. The scale_action
parameter in each autoscale rule configures the scale set to only add or remove one VM at a time, and the cooldown
parameter configures the scale to wait one minute between scaling actions. Remember that the autoscale profile that you created in the previous step ensures a minimum of 1 and a maximum of 10 virtual machines will be deployed.
Apply the updated configuration to add the two rules to your autoscale settings.
Since each VM is using less than 10% of their CPU, the scale set scales down the number of VMs, waiting one minute between each action. Run the following command after a few minutes to verify that only a single VM is running.
Destroy configuration
Now that you have completed this tutorial, destroy the Azure resources you provisioned to avoid incurring unnecessary costs. Respond yes
to the prompt to confirm the operation.
Next steps
In this tutorial, you used Terraform to provision a Virtual Machine Scale Set that deployed three virtual machines. You then added two rules to the scale set autoscale profile to scale up and down based on VM CPU load.
Learn more about Azure Virtual Machine Scale Sets and Terraform:
- Read about Azure Virtual Machine Scale Sets.
- Learn how to Get Started with the Azure Terraform provider.