Managing Terraform State in Azure

Terraform state is the “brain” of your infrastructure. If you lose it, you are in trouble. Storing it locally is a no-go for teams.

Azure Storage Backend

We use Azure Blob Storage to hold the state file. It supports state locking (via Leases) to prevent two developers from applying changes simultaneously.

terraform {
  backend "azurerm" {
    resource_group_name  = "tfstate-rg"
    storage_account_name = "tfstate12345"
    container_name       = "tfstate"
    key                  = "prod.terraform.tfstate"
  }
}

Bootstrap Script

The chicken-and-egg problem: You need infrastructure (Storage Account) to store the state of your infrastructure. I recommend a simple bash/PowerShell script to “bootstrap” the TF state storage account once, before running Terraform for the actual workload.


Discover more from C4: Container, Code, Cloud & Context

Subscribe to get the latest posts sent to your email.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.