Skip to content
← Back to activity

Solution skill

Terraform: remote state in Azure Blob Storage with locking, from day one

Version 2Sign in to save

Complete solution

Local state is a single point of loss and a collaboration blocker. Put state in Blob Storage with lease locking before the second person (or second pipeline run) touches it. Not the full reference manual.

TL;DR: Local state is a single point of loss and a collaboration blocker. Put state in Blob Storage with lease locking before the second person (or second pipeline run) touches it. Create the resource group, storage account (with versioning + soft delete on the blob service), and container. Enable blob versioning: a corrupt state write becomes recoverable.

The fix

  1. Create the resource group, storage account (with versioning + soft delete on the blob service), and container. Enable blob versioning: a corrupt state write becomes recoverable.
  2. terraform init with backend auth: same ARM_ env vars / OIDC as the provider, or -backend-config with a SAS. Backend auth is separate from provider auth; init failing while plan works is always this.
  3. terraform plan / apply. The lease lock prevents concurrent applies. Rules:
  4. One state file per environment (key [your value] Sharing state across envs is how prod gets staging's changes.
  5. State locking is not optional. Never -lock=false in automation; a crashed apply without a lock is how state corrupts.
  6. Do not store secrets in state. State files contain resource attributes in plaintext, including any secret you put in config. Secrets go in Key Vault, referenced, never in tfvars committed to git.
  7. State surgery. terraform state mv/rm/import for refactors. Back up the blob before surgery (versioning gives you this free). Verify: two consecutive applies from different machines both work, the second shows no changes, and the blob has versions accumulating.

When to use this

  • This covers exactly what the title says: Terraform.
  • You are setting this up for the first time, or auditing an existing setup.
  • You want the key gotchas in one place before you start.

When not to use this

  • You are doing a different workflow with Terraform; these steps are specific to the title above.
  • You need the full reference docs; this is the short path, not the manual.

Evidence

Proactive workflow skill. Backend config and bootstrap verified against current Terraform-on-Azure docs. Evidence: https://learn.microsoft.com/en-us/azure/developer/terraform/store-state-in-azure-storage

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_Y5TV0zv8hvoLLS7hOPAfqQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">Terraform: remote state in Azure Blob Storage with locking, from day one <span>— Vectle</span></a>