- Consulting
- Training
- Partners
- About Us
x
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).
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
1 2 3 4 |
$ sudo apt update $ sudo apt install wget unzip -y $ wget https://releases.hashicorp.com/Terraform/0.12.26/Terraform_0.12.26_linux_amd64.zip$ unzip Terraform_0.12.18_linux_amd64.zip $ ls |
Step-2:
Move the Terraform executable into the binary location of Linux and verify
1 2 |
$ sudo mv Terraform /usr/local/bin $ ls |
Step-3:
Check if Terraform command is available and check the version of Terraform
1 2 |
$ Terraform $ Terraform -v |
Step-1:
Get packages needed for the install process:
1 2 |
$ sudo apt-get update $ sudo apt-get install ca-certificates curl apt-transport-https lsb-release gnupg |
Step-2:
Download and install the Microsoft signing key:
1 2 3 |
curl -sL https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor | sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null |
Step-3:
Add the Azure CLI software repository:
1 2 3 |
AZ_REPO=$(lsb_release -cs) echo "deb [arch=amd64] https://packages.microsoft.com/repos/azure-cli/ $AZ_REPO main" | sudo tee /etc/apt/sources.list.d/azure-cli.list |
Step-4:
Update repository and install the azure-cli package:
1 2 |
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:
1 |
az login |
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 :
To create these resources, we are going to create 2 File with .tf extension in VS Code
Provider:
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.
1 2 3 4 5 6 7 |
# Configure the Azure provider provider "azurerm" { version = "~>1.32.1" #use_msi = true #subscription_id = "xxxxxxxxxxxxxxxxx" #tenant_id = "xxxxxxxxxxxxxxxxx" } |
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.
1 2 3 4 |
resource "azurerm_resource_group" "example" { name = "nb-terra-resources" location = "East US" } |
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:
1 2 3 4 5 6 |
resource "azurerm_virtual_network" "example" { name = "acctvn" address_space = ["10.0.0.0/16"] location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name } |
Subnet:
Now we are going to create a subnet in this Vnet. For more information you can refer to the below link:
https://www.Terraform.io/docs/providers/azurerm/r/subnet.html
1 2 3 4 5 6 |
resource "azurerm_subnet" "example" { name = "acctsub" resource_group_name = azurerm_resource_group.example.name virtual_network_name = azurerm_virtual_network.example.name address_prefix = "10.0.2.0/24" } |
Public IP:
Next, we are going to create “Public IP”
1 2 3 4 5 6 7 8 9 10 11 |
resource "azurerm_public_ip" "example" { name = "test" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name allocation_method = "Static" domain_name_label = azurerm_resource_group.example.name tags = { environment = "staging" } } |
Load Balancer:
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:
1 2 3 4 5 6 7 8 9 10 |
resource "azurerm_lb" "example" { name = "test" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name frontend_ip_configuration { name = "PublicIPAddress" public_ip_address_id = azurerm_public_ip.example.id } } |
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:
1 2 3 4 5 |
resource "azurerm_lb_backend_address_pool" "bpepool" { resource_group_name = azurerm_resource_group.example.name loadbalancer_id = azurerm_lb.example.id name = "BackEndAddressPool" } |
Load Balancer NAT pool:
1 2 3 4 5 6 7 8 9 10 |
resource "azurerm_lb_nat_pool" "lbnatpool" { resource_group_name = azurerm_resource_group.example.name name = "ssh" loadbalancer_id = azurerm_lb.example.id protocol = "Tcp" frontend_port_start = 50000 frontend_port_end = 50119 backend_port = 22 frontend_ip_configuration_name = "PublicIPAddress" } |
Load Balancer probe:
1 2 3 4 5 6 7 8 |
resource "azurerm_lb_probe" "example" { resource_group_name = azurerm_resource_group.example.name loadbalancer_id = azurerm_lb.example.id name = "http-probe" protocol = "Http" request_path = "/" port = 80 } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
resource "azurerm_virtual_machine_scale_set" "example" { name = "mytestscaleset-1" location = azurerm_resource_group.example.location resource_group_name = azurerm_resource_group.example.name upgrade_policy_mode = "Manual" sku { name = "Standard_DS2_v2" tier = "Standard" capacity = 2 } storage_profile_image_reference { publisher = "Canonical" offer = "UbuntuServer" sku = "16.04-LTS" version = "latest" } storage_profile_os_disk { name = "" caching = "ReadWrite" create_option = "FromImage" managed_disk_type = "Standard_LRS" } storage_profile_data_disk { lun = 0 caching = "ReadWrite" create_option = "Empty" disk_size_gb = 10 } os_profile { computer_name_prefix = "vmtest" admin_username = "myadmin" admin_password = "Password1234" } os_profile_linux_config { disable_password_authentication = false } network_profile { name = "Terraformnetworkprofile" primary = true ip_configuration { name = "TestIPConfiguration" primary = true subnet_id = azurerm_subnet.example.id load_balancer_backend_address_pool_ids = [azurerm_lb_backend_address_pool.bpepool.id] load_balancer_inbound_nat_rules_ids = [azurerm_lb_nat_pool.lbnatpool.id] } } tags = { environment = "staging" } } |
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”.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
resource "azurerm_monitor_autoscale_setting" "example" { name = "myAutoscaleSetting" resource_group_name = azurerm_resource_group.example.name location = azurerm_resource_group.example.location target_resource_id = azurerm_virtual_machine_scale_set.example.id profile { name = "defaultProfile" capacity { default = 2 minimum = 2 maximum = 10 } rule { metric_trigger { metric_name = "Percentage CPU" metric_resource_id = azurerm_virtual_machine_scale_set.example.id time_grain = "PT1M" statistic = "Average" time_window = "PT5M" time_aggregation = "Average" operator = "GreaterThan" threshold = 50 } scale_action { direction = "Increase" type = "ChangeCount" value = "1" cooldown = "PT1M" } } rule { metric_trigger { metric_name = "Percentage CPU" metric_resource_id = azurerm_virtual_machine_scale_set.example.id time_grain = "PT1M" statistic = "Average" time_window = "PT5M" time_aggregation = "Average" operator = "LessThan" threshold = 30 } scale_action { direction = "Decrease" type = "ChangeCount" value = "1" cooldown = "PT1M" } } } notification { email { send_to_subscription_administrator = true send_to_subscription_co_administrator = true custom_emails = ["abc@gmail.com"] } } } |
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.
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.
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:
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:
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:
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.
https://www.Terraform.io/docs/index.html
https://www.Terraform.io/downloads.html
Follow the below link for more information:
https://docs.microsoft.com/en-us/cli/azure/install-azure-cli-apt?view=azure-cli-latest
Voiced by Amazon Polly |
Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!
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.
Shyla
Dec 3, 2020
Also check out advanced Terraform Cloud https://blog.cloudthat.com/manage-infrastructure-on-aws-using-terraform-cloud-for-free/#.X8jzY27FayQ.whatsapp
Anusha Shanbhag
Oct 15, 2020
Very informative and detailed article. Thanks for sharing the related links.
Shweta
Oct 14, 2020
Good content, very helpful
Click to Comment