How to Create an Azure Resource Group with Terraform
In this lab, you create an Azure resource group with Terraform. The configuration is small, reusable, and available as a download.
- Authenticate to Azure with the Azure CLI
- Configure the official AzureRM provider
- Create a resource group with Terraform
- Validate and destroy the lab
Goal
Create rg-azfundamentals-lab in West Europe with a simple Terraform configuration.
Requirements
You need:
- An active Azure subscription
- Terraform installed
- Azure CLI installed
- Permission to create resource groups
Microsoft documents Azure CLI authentication as a supported way to authenticate Terraform to Azure. For automation, use an identity designed for the workload and do not save secrets in the Terraform files.
Download the code
The repository contains these files:
Authenticate to Azure
Sign in with the Azure CLI:
az login
Check the active subscription:
az account show --output table
Set the correct subscription when needed:
az account set --subscription "YOUR_SUBSCRIPTION_ID"
Terraform configuration
The provider version is constrained to AzureRM 4.x. The subscription ID is passed as a variable, so it is not hardcoded in the configuration.
versions.tf
terraform {
required_version = ">= 1.8.0"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4.0"
}
}
}
providers.tf
provider "azurerm" {
features {}
subscription_id = var.subscription_id
}
main.tf
resource "azurerm_resource_group" "this" {
name = var.resource_group_name
location = var.location
tags = var.tags
}
variables.tf
variable "subscription_id" {
description = "Azure subscription ID used by the AzureRM provider."
type = string
}
variable "resource_group_name" {
description = "Name of the Azure resource group."
type = string
default = "rg-azfundamentals-lab"
}
variable "location" {
description = "Azure region used for the resource group metadata."
type = string
default = "westeurope"
}
variable "tags" {
description = "Tags applied to the resource group."
type = map(string)
default = {
environment = "lab"
managed_by = "terraform"
}
}
Prepare the variable file
Copy the example file:
cp terraform.tfvars.example terraform.tfvars
On Windows PowerShell:
Copy-Item terraform.tfvars.example terraform.tfvars
Open terraform.tfvars and add your subscription ID:
subscription_id = "00000000-0000-0000-0000-000000000000"
The .gitignore file excludes terraform.tfvars and Terraform state files. Do not commit credentials or sensitive values.
Run Terraform
Initialize the working directory:
terraform init
Format and validate the files:
terraform fmt
terraform validate
Review the plan:
terraform plan
Create the resource group:
terraform apply
Read the plan before entering yes.
Validate the result
You can check the output:
terraform output
You can also open Resource groups in the Azure Portal and confirm that rg-azfundamentals-lab exists with the expected tags.
Remove the lab
When the test is complete, run:
terraform destroy
Review the plan and confirm only when the resource group contains no data you need.
The local state file can contain infrastructure details. Teams should use a secure remote backend with access control and state locking.