Home Azure Cloud Using Azure Storage as a Terraform Backend

Using Azure Storage as a Terraform Backend

242
0

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.tf to 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

ResourceNamePurpose
Resource Groupcocan-infra-ado-rgHolds the backend resources
Storage AccountcocantfstatestrgacctStores Terraform state securely
ContainertfstateHolds 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 init connects to Azure Storage and sets up the backend.
  • terraform plan shows what resources will be created.
  • terraform apply creates 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:

  1. Navigate to Resource Groups → find cocan-dev-rg.
  2. Navigate to your Storage Accounttfstate container → confirm terraform.tfstate exists.

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

StepDescription
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.
Previous articleSetting Up and Using Self-Hosted Agents in Azure DevOps
Next articleCreating a Free Tier Account and Getting Started with Azure Cloud
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here