Interview Prep/Terraform/State Backends and Workspaces

A laptop apply and a CI apply start at the same time. What stops state corruption?

IntermediateScenarioState Backends and Workspaces
Answer

Situation read → Concurrent writers are the classic cause of state corruption.

What I'd do →

  1. Rely on backend locking: the second run fails with "Error acquiring the state lock".
  2. Don't use -lock=false for applies.
  3. Restrict who can apply prod to the CI role only.

Why → The lock holds an ID and owner info; the second run waits (-lock-timeout=5m) or fails instead of interleaving writes.

Interview tip → The cleanest control is making CI the only identity allowed to write prod state.

All Terraform questions Roadmap: Terraform in production

More on State Backends and Workspaces