Get Started: Create an Azure resource group using Terraform

  • 3 minutes to read

Terraform enables the definition, preview, and deployment of cloud infrastructure. Using Terraform, you create configuration files using HCL syntax. The HCL syntax allows you to specify the cloud provider - such as Azure - and the elements that make up your cloud infrastructure. After you create your configuration files, you create an execution plan that allows you to preview your infrastructure changes before they're deployed. Once you verify the changes, you apply the execution plan to deploy the infrastructure.

This article shows how to create an Azure resource group using Terraform.

In this article, you learn how to:

  • Create an Azure resource group to hold other Azure resources
  • Verify (using Azure CLI and Azure PowerShell) the resource group was created
  • Delete the resource group when finished using it

1. Configure your environment

  • Azure subscription: If you don't have an Azure subscription, create a free account before you begin.
  • Configure Terraform: If you haven't already done so, configure Terraform using one of the following options:

    • Configure Terraform in Azure Cloud Shell with Bash
    • Configure Terraform in Azure Cloud Shell with PowerShell
    • Configure Terraform in Windows with Bash
    • Configure Terraform in Windows with PowerShell

2. Implement the Terraform code

  1. Create a directory in which to test the sample Terraform code and make it the current directory.

  2. Create a file named main.tf and insert the following code:

                    terraform {    required_version = ">=0.12"      required_providers {     azurerm = {       source = "hashicorp/azurerm"       version = "~>2.0"     }   } }  provider "azurerm" {   features {} }  resource "random_pet" "rg-name" {   prefix    = var.resource_group_name_prefix }  resource "azurerm_resource_group" "rg" {   name      = random_pet.rg-name.id   location  = var.resource_group_location }                              
  3. Create a file named variables.tf to contain the project variables and insert the following code:

                    variable "resource_group_name_prefix" {   default       = "rg"   description   = "Prefix of the resource group name that's combined with a random ID so name is unique in your Azure subscription." }  variable "resource_group_location" {   default = "eastus"   description   = "Location of the resource group." }                              

3. Initialize Terraform

Run terraform init to initialize the Terraform deployment.

            terraform init                      

Key points:

  • This command downloads the Azure modules required to create an Azure resource group.

4. Create a Terraform execution plan

Run terraform plan to create an execution plan.

            terraform plan -out main.tfplan                      

Key points:

  • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
  • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
  • To read more about persisting execution plans and security, see the security warning section.

5. Apply a Terraform execution plan

Run terraform apply to apply the execution plan to your cloud infrastructure.

            terraform apply main.tfplan                      

Key points:

  • The terraform apply command above assumes you previously ran terraform plan -out main.tfplan.
  • If you specified a different filename for the -out parameter, use that same filename in the call to terraform apply.
  • If you didn't use the -out parameter, simply call terraform apply without any parameters.

6. Verify the results

  • Azure CLI
  • Azure PowerShell

Run az group show to display the resource group.

                az group show --name <resource_group_name>                              

7. Clean up resources

When you no longer need the resources created via Terraform, do the following steps:

  1. Run terraform plan and specify the destroy flag.

                    terraform plan -destroy -out main.destroy.tfplan                              

    Key points:

    • The terraform plan command creates an execution plan, but doesn't execute it. Instead, it determines what actions are necessary to create the configuration specified in your configuration files. This pattern allows you to verify whether the execution plan matches your expectations before making any changes to actual resources.
    • The optional -out parameter allows you to specify an output file for the plan. Using the -out parameter ensures that the plan you reviewed is exactly what is applied.
    • To read more about persisting execution plans and security, see the security warning section.
  2. Run terraform apply to apply the execution plan.

                    terraform apply main.destroy.tfplan                              

Troubleshoot Terraform on Azure

Troubleshoot common problems when using Terraform on Azure

Next steps