Starting use Terraform for Azure

Suren Edilyan
5 min readApr 13, 2024

--

Prerequisites

  • Installed Visual Studio Code. And make sure that “File >> Auto Save” is checked.
  • An active Azure subscription. If you don’t have one, create a free account.

Environment setup

  1. Download the latest version of Terraform (in my case I used Terraform for Windows AMD64 Version 1.8.0). After downloading, you need to extract the folder and add terraform.exe to the Windows Environment Variables. We can do this by accessing Windows Settings and selecting “Edit environment variables for your account”.

It will open popup window. In the “User variable for …” section, we need to select the “Path” row and click edit.

After clicking “Edit”, a new popup will open. Next, we need to add a new path pointing to the directory of terraform.exe. Then we need to submit all opened windows by clicking “OK”.

2. Download and install the Azure CLI (in my case, I used MSI of the Azure CLI 64-bit). After a simple wizard installation, we can connect to our Azure account via PowerShell terminal, which I’m going to use inside Visual Studio Code.

3. We need to create a new folder named “terraform-azure” and open it in Visual Studio Code in “Run as administrator” mode. Then, we should install the Terraform extension for Visual Studio Code.

Working with Terraform

4. Now, with the help of Terraform Azure Provider, which utilizes the Azure Resource Manager API’s for allowing Terraform to configure infrastructure in Microsoft Azure, we need to Authenticate to Azure using the Azure CLI. Inside Visual Studio Code, open terminal, ensure that it is set to PowerShell, and type:

> az login

It will open browser tab where you should log in to your Azure account.

If you have multiple subscriptions, you can check:

> az account show

If it’s not the appropriate one, you can change it by typing (instead of 0000…s chose the corresponding subscription id):

az account set -s 00000000-0000-0000-0000-000000000000

5. Create main.tf file inside “terraform-azure” folder and add on it and input the following command in the terminal: > terraform fmt. This command is used to rewrite Terraform configuration files to a canonical format and style.

# We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "=3.0.0"
}
}
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
features {}
}

6. Input the following command in the terminal: > terraform init . This will create the Terraform project inside the “terraform-azure” folder.

7. Next, add the following code into the main.tf file.

resource "azurerm_resource_group" "rg-terraform" {
name = "rg-terraform-stage"
location = "East Us"
tags = {
environment = "stage"
}
}

Input in terminal > terraform plan command. It’ll create terraform project inside “terraform-azure” folder. After that it will showing us what is going to be built if we run Terraform.

8. For deploying, input the following command in the opened terminal: > terraform apply. The terminal will prompt you with “Enter a value”. Type “yes”, and press “Enter” to apply the creation of the resource group in Azure. Afterward, you can check in the Azure portal to find the resource group.

9. Now we can add other resource, for example, a virtual network. We will continue working in the same “terraform-azure” folder.

resource "azurerm_virtual_network" "vn-terraform" {
name = "vn-terraform-stage"
resource_group_name = azurerm_resource_group.rg-terraform.name
location = azurerm_resource_group.rg-terraform.location
address_space = ["10.123.0.0/16"]

tags = {
environment = "dev"
}
}

For code accuracy view, input the following command in the terminal to correct the formatting: > terraform fmt command for correction.

Then, run terraform plan to review the planned changes.

After reviewing, execute > terraform apply -auto-approve to implement the changes directly without needing manual approval. You can then check the Azure portal to find your virtual network.

10. The > terraform state list command will display all the resources we have.

11. Use the command:

> terraform state show azurerm_resource_group.rg-terraform, where azurerm_resource_group.rg-terraform is one of existing resources.

This command will display information about the resource. In our case, it since it’s a resource group, running this command will show the ID, location, name, and tags of our resource group. However, if we want to see the states of all resources, we only need to run > terraform show.

12. When we want to delete all available resources, we can input the command > terraform plan -destroy. The terminal will display a list of all resources marked with a red dash that are going to be deleted. Finally, when we are decided to destroy all resources, we need to input the command > terraform apply -destroy into the terminal.

13. In our Terraform project, we have the terraform.tfstate file, which maintains information about the current state, and the terraform.tfstate.backup file, which is a backup of the previous state. After destroying all the resources, we can restore them using the terraform.tfstate.backup file by inputting the standard command terraform apply -auto-approve in the terminal.

14. After restoring backup, we can input > terraform state list in the terminal to check the existence of resources.

If you have any questions, thoughts, or need further clarification on any aspect discussed in this article, please don’t hesitate to get in touch with me. Your feedback and inquiries are valuable. Feel free to reach out to me via LinkedIn as well, where we can connect and discuss in more detail. Let’s explore and delve deeper into the topics together, ensuring your understanding and success.

linkedin.com/in/surenedilyan/

--

--

Suren Edilyan

Voracious software developer. Avid investigator. Active researcher.