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.