In this blog article, I will discuss how you can create a Virtual Machine Scale Set with Auto Scale settings in Microsoft Azure Cloud using Terraform. In my next blog article I will explain how to automate the configuration of all VM(s) using Ansible.
I will use VS Code to write code for Terraform and Ansible and to perform the command (CLI), I am going to use VS Code Terminal (WSL Ubuntu).
1) You need to install Terraform in Ubuntu (WSL)
Step-1: Install wget and unzip package from apt repository and download Terraform from given link and unzip the downloaded file to get the Terraform binary
Step-4: Update repository and install the azure-cli package:
sudo apt-get update
sudo apt-get install azure-cli
Run the Azure CLI with the az command. To sign in, use the az login command. Step-1: Run the login command:
If the CLI can open your default browser, it will do so and load an Azure sign-in page.
Otherwise, open a browser page https://aka.ms/devicelogin and enter the authorization code displayed in your terminal. Step-2: Sign in with your account credentials in the browser.
Now let’s Build an Infrastructure on Azure Cloud:
In this blog, we are going to create VMSS with autoscaling settings and all other required resources in Azure.
These resources include the following Services :
Azure Load Balancer (backend pool, nat pool, probe)
Virtual Machine Scale Set (AutoScale Setting)
To create these resources, we are going to create 2 File with .tf extension in VS Code
Let’s Create Provider.tf file
To create resources we need to provide a provider like AWS, Azure, GCP.
We are going to use azurerm as a provider as I’m using Microsoft Azure Cloud.
# Configure the Azure provider
#use_msi = true
#subscription_id = "xxxxxxxxxxxxxxxxx"
#tenant_id = "xxxxxxxxxxxxxxxxx"
Now let’s Create Main.tf file for resources:
First I will create a resource group in an azure Cloud by adding the “azurerm_resource_group” block with name “example” with name and location.
Now, I am going to create a virtual network (Vnet) by adding the “azurerm_virtual_network” block with the name “example”. In this block, I will add a name, address_space, location, resource group. There is more option available. You can find it in the official documentation of Terraform in their site. For this blog this much is required. It will be like:
In this block we are going to create LoadBalancer by adding “azurerm_lb” block in main.tf file.
In this block we will add name. location, resource_group_name and frontend_ip_configuration with name and public_ip_address_id. It will look like:
Now we will set autoscaling configuration in this file as well which will set autoscaling settings based on the Percentage CPU threshold. It will also set how many VM will be increasing at a time and what will be the default value, minimum value and maximum value for scaling.
After writing the script we will run the script. Make sure you used az login and set your subscription in a terminal.
Running the Script :
We follow three steps for deploying with Terraform. These steps are:
and to destroy it, we follow one step is:
Now we have two file provider.tf and main.tf these scripts will create VMSS, Vnet, subnet, Load Balancer and what all given in script on Microsoft Azure Cloud.
We will run the first step Terraform init which will initialize the directory where we created our script files (you have to cd into that directory where files are present then run this command).
It is checking all the providers mentioned in the script and download the files needed to run the script.
Now we will run our second step: Terraform plan, it will check if some error in code or syntax error and it will give a lot of output in a terminal where you find what all is going to create with “+” sign.
You can see in the below terminal run:
In this third step, Terraform apply, you are applying all the changes, you will see the changes in the terminal. In this command, you will get the option to perform actions. When you are verified with changes and agree with it, you enter “yes” and your infrastructure will be created on the Azure Cloud.
You can see in terminal output as given below:
Now let’s Check our infrastructure is created in azure Cloud or not by visiting:
We got a message that Apply complete! and Resources added. To check this we will go to the Azure portal.
You can see here that all the resources have been created in the resource group name that we gave in the script. There are VMSS, loadbalancer, Vnet and public IP are there. Now our deployment is successful.
Terraform is a strong and mature tool for managing resources. It has many strengths and has a clear use-case for multi-Cloud environments, as it enables managing these environments with only one tool instead of gluing scripts or tools together. It also has a clear focus which is infrastructure and is very well defined in how it works. Terraform supports over 100 providers which are supported by engineers from their own providers, but it also has over 100 community providers. With all these combined it enables enterprises to flourish in complex environments.