Skip to content

Lesson 2 of 9 · 23m

State Backends, Locking, and Ownership

State is production infrastructure, not a build artifact — backend choice, encryption, locking, and blast-radius decisions.

Terraform state is the memory of your infrastructure. Lose it and you may spend days importing resources; leak it and you may expose private addresses, identifiers, and sometimes secrets. Teams talk endlessly about state syntax and forget the core fact: the backend is a production service with its own availability, audit, and recovery requirements.

Use a remote backend before the second engineer arrives

A mature AWS setup commonly uses S3 for state and native S3 locking:

terraform {
  backend "s3" {
    bucket       = "acme-terraform-state"
    key          = "prod/network/terraform.tfstate"
    region       = "us-east-1"
    use_lockfile = true
    encrypt      = true
  }
}

The exact backend varies — Azure Blob Storage, Google Cloud Storage, Terraform Cloud or HCP Terraform — but the operational requirements do not: server-side encryption, tightly-scoped write access, versioning or object retention, locking, and backup or recovery procedures. If the bucket can be emptied by the same credentials that run CI for every pull request, your state design is fragile.

State locking prevents human scheduling failures

Terraform uses locking so two plans do not mutate the same dependency graph concurrently. If a run ends with Error acquiring the state lock, do not make force-unlocking your reflex. First identify whether another run is active. A stale lock is usually a crashed process; an active lock is a teammate changing the same infrastructure.

terraform force-unlock 6c1a5f8c-8e1e-2fd6-91a7-552768bf4ad1

That command is a break-glass tool. Record why you used it. In regulated environments, it belongs in an incident or change ticket.

Split state by failure domain, not by aesthetics

One giant state makes every plan slow and every lock global. Hundreds of tiny states make dependency tracking miserable. Split around operational blast radius:

terraform/
  bootstrap/          # state buckets, CI roles, foundational access
  shared-network/     # VPCs, transit routing, shared DNS
  prod/billing-api/   # one service or bounded environment slice
  prod/payments-db/   # data resources whose replacement risk differs

State ownership should match human ownership. The team that can approve a destructive change should control writes to that state. Cross-state reads are fine; cross-state write coupling is how small changes become outages.

State contents are sensitive even when no literal password appears

Terraform can record generated credentials, private keys, user data, and attribute values marked sensitive in configuration. terraform state list shows little, but terraform show -json can reveal much more. Treat state read access as infrastructure read access — and often as secret read access.

Field notes

  • Migrating backends with terraform init -migrate-state is safe only after the old backend is backed up.
  • Backend configuration cannot use ordinary variables; generate or inject it deliberately in CI.
  • Enable object versioning before the first real state write, not after the first accidental deletion.
  • If different lifecycles constantly fight over one lock, that is a state-splitting signal.

Was this lesson helpful?

Sign in to rate