When we use Terraform to create Azure resources, Terraform keeps track of what it creates using a state file — usually named terraform.tfstate.
By default, this file is stored locally, but for better collaboration, reliability, and security, you can store it remotely in Azure Storage.
In this post, we’ll learn how to:
- Create an Azure Storage backend for Terraform
- Configure your Terraform
main.tfto use it - Understand how it works — in the simplest way possible
🧠 Why Store the State in Azure Storage?
Terraform’s state file is like a memory of our cloud infrastructure.
If multiple people work on the same setup, or if our machine is lost, keeping the state locally can cause problems.
Using Azure Storage for the backend gives us:
✅ A central place to store the state file
✅ Automatic locking when used with proper configuration
✅ Safe and secure storage that can be backed up and versioned
✅ Team collaboration across environments
🧩 Prerequisites
Before setting up Terraform with an Azure Storage backend, make sure you have the following in place:
✅ Azure Subscription
You’ll need an active Azure subscription with permission to create and manage resources such as resource groups, storage accounts, and containers.
✅ Terraform Installed
Terraform is the tool you’ll use to define and manage infrastructure as code.
See:
👉 Installing Terraform on Linux (Ubuntu) and Windows (WSL2 with Ubuntu)
👉 Installing Terraform on macOS
✅ Azure CLI Installed (Optional but Useful)
The Azure CLI helps you verify or create Azure resources directly from your terminal.
See:
👉 Getting Started with Azure CLI on Linux (Ubuntu) and Windows (WSL2 with Ubuntu)
👉 Getting Started with Azure CLI on Your Local Machine
✅ Text Editor or IDE
Use any text editor (for example, Visual Studio Code) to create and edit your Terraform configuration file (main.tf).
🏗️ Step 1: Create the Azure Storage Backend
Before setting up Terraform, we’ll need a few Azure resources that will hold your state file:
# Create a resource group for backend
az group create -n cocan-infra-ado-rg -l canadacentral
# Create a storage account (name must be unique)
az storage account create \
-n cocantfstatestrgacct \
-g cocan-infra-ado-rg \
-l canadacentral \
--sku Standard_LRS
# Create a container inside the storage account
az storage container create \
--name tfstate \
--account-name cocantfstatestrgacct
📘 What These Do
| Resource | Name | Purpose |
|---|---|---|
| Resource Group | cocan-infra-ado-rg | Holds the backend resources |
| Storage Account | cocantfstatestrgacct | Stores Terraform state securely |
| Container | tfstate | Holds your terraform.tfstate file |
📄 Step 2: Create Your Terraform Configuration
Now that your Azure Storage is ready, create a folder (for example environments/dev/) and inside it, create a file named main.tf.
Here’s what it should look like:
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 4.0"
}
}
# Remote backend using Azure Storage
backend "azurerm" {
resource_group_name = "cocan-infra-ado-rg"
storage_account_name = "cocantfstatestrgacct"
container_name = "tfstate"
key = "terraform.tfstate"
}
}
provider "azurerm" {
features {}
}
# Example Resource: Azure Resource Group
resource "azurerm_resource_group" "rg" {
name = "cocan-dev-rg"
location = "canadacentral"
}
🪄 Step 3: Initialize and Apply Terraform
Once your main.tf is ready, open your terminal, navigate to the folder containing it, and run the following commands one by one:
1️⃣ Initialize Terraform
This command downloads the required provider plugins and connects Terraform to your Azure Storage backend.
terraform init
2️⃣ Preview the Changes
This command shows what resources Terraform will create or modify before making any changes.
terraform plan
3️⃣ Apply the Configuration
This command actually creates the resources in Azure.
terraform apply -auto-approve
💬 What Happens
terraform initconnects to Azure Storage and sets up the backend.terraform planshows what resources will be created.terraform applycreates the resources in Azure.
After running successfully, Terraform will store your state file in the container tfstate inside your Azure Storage Account, instead of keeping it locally.
🔍 Step 4: Verify in Azure Portal
Go to the Azure Portal:
- Navigate to Resource Groups → find
cocan-dev-rg. - Navigate to your Storage Account →
tfstatecontainer → confirmterraform.tfstateexists.
You’ve successfully configured a remote backend for Terraform using Azure Storage! 🎉
🧹 Step 5: Clean Up (Optional)
To avoid ongoing costs, you can delete the resources:
terraform destroy -auto-approve
✅ Summary
| Step | Description |
|---|---|
| 1️⃣ | Create a resource group, storage account, and container |
| 2️⃣ | Configure backend in main.tf |
| 3️⃣ | Run terraform init, plan, and apply |
| 4️⃣ | Verify state file in Azure Portal |
💡 Key Takeaways
- Storing state in Azure Storage makes Terraform safer and easier to manage in shared environments.
- The
backend "azurerm"block tells Terraform where to save and retrieve the state file. - This approach works well for teams or projects that want a secure, centralized, and scalable setup.