How to deploy an Azure Resource Group using Terraform

Hello everyone, in my last post I showed you how to install Terraform on your systems and how to configure it to allow resource manipulation in your Azure subscription. Today I will show you how to deploy your first Azure Resource group using Terraform.

Prerequisites

  • This tutorial assumes that you already have a Microsoft Azure account configured.
  • Terraform must be installed and configured to allow manipulation of resources in your subscription Azure, check out this link.

First Terraform code

To begin the use of Terraform to deploy a resource in Azure, we will deploy a simple Azure Resource, a Resource Group. To do this, in the same directory where you previously created the provider.tf file, you should create a new file, main.tf with the following code.

A resource block in Terraform has two parameters before opening the block:

The resource type (“azurerm_resource_group“) and the resource name (“rg“), this name is used as a reference by Terraform, it is not the name of the resource. The type and name combination must be unique in the configuration.

Within the resource block enclosed by {} is the list of resource properties. The available properties depend on each type of resource.

In this example, we specify the required properties: the name and the location. We will also specify Tags for our resource that although it is not mandatory, will allow us to improve the administration of our resources.

Initialization

Once the main.tf file has been created, we can start the deployment of resources. the first step is to run the following command.

terraform init

This command triggers the following tasks:

  • Create a hidden directory .terraform
  • Download the plugins of the provide.
  • Verify the connection between the terraform provider and the remote service (Azure)

Show the execution plan

Before you can deploy the infrastructure, Terraform needs to generate an execution plan. for this, you must use the following command.

terraform plan

This command triggers the following tasks:

  • Checks the syntax of the configuration.
  • Connects to Azure to refresh the state before checking for differences with the current configuration.

Important: if you want to export the plan to a file and then execute that same plan, use the -out argument.

Apply Configuration

The last step is the application of the Terraform code to provision and applies the changes to our Azure infrastructure.

terraform apply

As you can see in the previous screenshot, you will be asked for confirmation before applying the changes.

This command triggers the following tasks:

  • This command does the same operation as the plan and implements the changes.
  • Create the terraform.tfstate file: This file tracks of all managed resources and their associated properties with current values and is essential for managing changes to infrastructure over time.

Check the state

You can use the following command to inspect the current state.

terraform azure resource group

Destroy Infrastructure

And finally, if you want to delete the deployed resources, you should follow the next steps.

First, use the following command to check before deleting which resources will be affected.

terraform azure resource group

once you have performed the relevant checks, you can run the following command.

terraform destroy

As you can see in the previous screenshot, you will be asked for confirmation before destroying the resources.

Thanks for reading my post. I hope you find it useful.

If you want to know more about Terraform on Azure, check out this link: https://docs.microsoft.com/en-us/azure/terraform/