Azure, Cloud Computing, DevOps

7 Mins Read

Setting Up Azure Infrastructure Using Terraform

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)

Install wget and unzip package from apt repository and download Terraform from given link and unzip the downloaded file to get the Terraform binary

Move the Terraform executable into the binary location of Linux and verify

Check if Terraform command is available and check the version of Terraform

2) You need to install Azure CLI. Follow the below steps

Get packages needed for the install process:

Download and install the Microsoft signing key:

Add the Azure CLI software repository:

Update repository and install the azure-cli package:

Run the Azure CLI with the az command. To sign in, use the az login command.
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 and enter the authorization code displayed in your terminal.
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 :

To create these resources, we are going to create 2 File with .tf extension in VS Code

  1. tf
  2. tf

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.

 Resource Group:
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.

Virtual Network:
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:

Now we are going to create a subnet in this Vnet. For more information you can refer to the below link:

Public IP:
Next, we are going to create “Public IP

Load Balancer:
In this block we are going to create LoadBalancer by adding “azurerm_lb” block in 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:

You can see that we used public_ip_address_id  with the value of public IP we created before this.
Load Balancer backend pool address:

Load Balancer NAT pool:

Load Balancer probe:

Now our Load Balancer backend pool address, NAT pool and a probe are created for Load Balancer.

Virtual Machine Scale Set:
Lets create virtual machine scale set itself by adding “azurerm_virtual_machine_scale_set” with sku, storage_profile_image_reference, storage_profile_os_disk, storage_profile_data_disk, os_profile, os_profile_linux_config, network_profile in block of vmss.

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.

Lets set  by adding “azurerm_autoscale_setting”.

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 and these scripts will create VMSS, Vnet, subnet, Load Balancer and what all given in script on Microsoft Azure Cloud.

Terrafom init:

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.

Setting Up Azure Infrastructure Using Terraform

Terraform plan:

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:

Setting Up Azure Infrastructure Using Terraform

Setting Up Azure Infrastructure Using Terraform

Terraform apply:

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:

Setting Up Azure Infrastructure Using Terraform

Setting Up Azure Infrastructure Using Terraform

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.

Let’s see:

Setting Up Azure Infrastructure Using Terraform

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.

Closing words

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.

If you want to know more about Terraform and other Azure services kindly check our Developing Solutions for Microsoft Azure AZ-204.

Please comment if you have any questions.

Follow Terraform official site for new updates and Documentation.

Follow the below link for more information:

WRITTEN BY Narendra Bamaniya



  1. Manohar

    Jun 19, 2022


    I need to update Terraform AzureRm module from 1.32.1 to latest version. can u please suggest how to proceed.

  2. Anusha Shanbhag

    Oct 15, 2020


    Very informative and detailed article. Thanks for sharing the related links.

  3. Shweta

    Oct 14, 2020


    Good content, very helpful

  4. Click to Comment