Create Resource Group using Terraform in Microsoft Azure

Today, we will create a resource group in Microsoft Azure using Terraform and Azure CLI. First, let’s write terraform code locally using Visual Studio Code.

Open Microsoft Visual Studio Code and search for Terraform; install the HashiCorp Terraform extension as shown below.

 

A screenshot of a computer

Description automatically generated

 

Restart your Visual Studio Code, and create a new folder with two new files called Providers.tf and ResourceGroup.tf, as below.

 

A screenshot of a computer

Description automatically generated

Providers.tf

provider"azurerm"{

    features {}

}

 

ResourceGroup.tf

resource"azurerm_resource_group""rg"{

    location ="eastus"

    name ="techcohererg"

}

 

As shown above, the providers.tf file has Azure provider code (specific to your cloud provider), and ResourceGroup.tf file has code to create a new resource group called “techcohererg”.

Now, we need to deploy our terraform code on Azure. To deploy our code, we use Azure CLI.

Open Azure Portal and select Azure CLI as below. You can create a free Azure account by using https://azure.microsoft.com/en-gb/free link.

A blue rectangular object with a red mark

Description automatically generated

If you are opening Azure CLI for the first time, it will ask you to create a storage account. Here, I am using Bash; you can use PowerShell, too.

Execute the below commands to create and open a specific folder, TerraformCode.

 

A screenshot of a computer

Description automatically generated

 

Let’s copy the terraform code to Azure CLI. Use the below commands to copy the code.

Code Provider.tf => to create a new file called Provider.tf and copy the code as below.

 

A screenshot of a computer

Description automatically generated

 

Use Ctrl + Q to save the file. We need to do same for ResourcGroup.tf file also.

 

A screenshot of a computer

Description automatically generated

 

Our Terraform Code is ready for execution. Before that, let’s see all files in the “TerraformCode” folder using the “dir” command.

 

A screenshot of a computer

Description automatically generated

 

Use “clear” command to clear everything on the Azure CLI command prompt.

First, we need to initiate using the “terraform init” command.

 

A screenshot of a computer

Description automatically generated

 

Create terraform plan using “terraform plan --out myplan.tfplan” command.

 

A screenshot of a computer program

Description automatically generated

 

After creating the plan, we must use the “terraform apply main.tfplan” command to apply the terraform plan.

 

A screen shot of a computer

Description automatically generated

 

You can find new resource group, “techcohererg” under Resource Groups in Azure Portal.

 

A screenshot of a chat

Description automatically generated

Execute “dir” command to see all files.

 

A screenshot of a computer

Description automatically generated

 

You can see an extra file called terraform,tfstate, a terraform state file used to track changes between multiple deployments. Open terraform.tfstate file using “code terraform.tfstate” command.

 

A screenshot of a computer

Description automatically generated

 

terraform.tfstate

{

"version": 4,

"terraform_version": "1.3.2",

"serial": 2,

"lineage": "b18df855-8899-9354-eb84-cfd4648ecca1",

"outputs": {},

"resources": [

    {

     "mode": "managed",

     "type": "azurerm_resource_group",

     "name": "rg",

     "provider": "provider[\"registry.terraform.io/hashicorp/azurerm\"]",

     "instances": [

        {

         "schema_version": 0,

         "attributes": {

            "id": "/subscriptions/7bb4f362-b6a0-4302-9e89-a14b3602f271/resourceGroups/techcohererg",

           "location": "eastus",

           "managed_by": "",

           "name": "techcohererg",

           "tags": null,

           "timeouts": null

          },

         "sensitive_attributes": [],

         "private": "eyJlMmJmYjczMC1lY2FhLTExZTYtOGY4OC0zNDM2M2JjN2M0YzAiOnsiY3JlYXRlIjo1NDAwMDAwMDAwMDAwLCJkZWxldGUiOjU0MDAwMDAwMDAwMDAsInJlYWQiOjMwMDAwMDAwMDAwMCwidXBkYXRlIjo1NDAwMDAwMDAwMDAwfX0="

        }

      ]

    }

  ],

"check_results": []

}

 

As shown above, we are having techcohererg resource group details in terraform.tfstate file.

 

Please find below all Azure CLI and Terraform commands, and it’s usage.

 

mkdir TerraformCode => to create folder TerraformCode

 

cd TerraformCode => to navigate to TerraformCode folder

 

Code Provider.tf => to open Provider.tf file. If the provider.tf file does not exist, it creates a new file Provider.tf and opens.

 

terraform init => to initiate terraform

 

terraform plan --out main.tfplan => to create terraform plan “main.tfplan”

 

terraform apply main.tfplan => to apply main.tfplan terraform plan

 

dir => to see all files in adirectory

 

code terraform.tfstate => to see the terraform state file