Setting Up Azure Infrastructure Using Terraform

Setting Up Azure Infrastructure Using Terraform

September 9, 2022 Off By Nick

This blog article will show you how to create a Virtual Machine Scale Set in Microsoft Azure Cloud with Terraform. I will be explaining how to automate all VM(s), using Ansible in my next blog post.
To write code for Terraform, Ansible, and perform the command (CLI), VS Code Terminal (WSL Ubuntu) will be used.
1) Terraform must be installed in Ubuntu (WSL).
Step 1:Install wget from apt repository. Download Terraform from the given link. Unzip the downloaded file to obtain the Terraform binary
$ 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$ ls1234$ 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$ lsStep-2:Move the Terraform executable into the binary location of Linux and verify
$ sudo:mv Terraform/usr/local/bin ls12$ sudo:mv Terraform/usr/local/bin ls3:Check if Terraform command exists and verify the Terraform version
Terraform $ Terraform Terraform v12 Terraform Terraform $ Terraform Terraform v12 Terraform Terraform Terraform Terraform v12 Terraform Terraform Terraform Terraform Terraform Terraform Terraform Terraform Terraform Terraform Terraform Terraform v
2) You will need to install Azure CLI. Follow these steps
Step 1: Get the packages you need for the installation process.
$ sudo apat-get upgrade$ sudo apat-get install certificates curl apt–transport-https–release gnupg12$ sudo apat-get up$ sudo suapt-get get update$ sudo sudo sudo sudo sudo sudoapt-get installation certificates curl apt–transport-https–release gnupgStep-2 :Download and install the Microsoft signed key:
curl -sL https://packages.microsoft.com/keys/microsoft.asc |gpg –dearmor |sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/null123curl -sL https://packages.microsoft.com/keys/microsoft.asc |gpg –dearmor |sudo tee /etc/apt/trusted.gpg.d/microsoft.gpg > /dev/nullStep-3:Add the Azure CLI software repository:
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.list123AZ_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.listStep-4:Update repository and install the azure-cli package:
sudo aptget updatesudoapt-get update azurecli12sudoapt-get up-datesudoapt-get install install azurecli12sudoapt-get update azurecli12sudoapt-get aptget install azurecliRun the Azure CLI using the az command. Step 1: Run the login command.
az login1az loginIf 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.
Let’s now build an infrastructure on Azure Cloud.
Resource Group
Vnet
Subnet
Public IP
Azure Load Balancingr (backend pool nat pool probe)
Virtual Machine Scale Set (AutoScale Setting)
To create these resources, you will need to create 2 Files with the.tf extension in Visual Studio Code
tf
tf
Let’s create provider.tf file
Provider: To create resources, we need to provide a provider such as AWS, Azure or GCP. We are going to use azurerm to provide a provider since I’m using Microsoft Azure Cloud.
# Configure the Azure providerprovider “azurerm” version = “~>1.32.1” #use_msi = true #subscription_id = “xxxxxxxxxxxxxxxxx” #tenant_id = “xxxxxxxxxxxxxxxxx”1234567# Configure the Azure providerprovider “azurerm” version = “~>1.32.1″#use_msi = true#subscription_id = “xxxxxxxxxxxxxxxxx”#tenant_id = “xxxxxxxxxxxxxxxxx”
Let’s now create Main.tf to get resources:
Resource Group:First, I will create a resource category in an azure Cloud. I will add the