Interview Prep/Terraform/State Locking and Backend Errors

Two pipelines for different stacks keep blocking each other with `Error acquiring the state lock`, though they manage unrelated resources.

IntermediateTroubleshootingState Locking and Backend Errors
Answer

Likely causes → Both stacks use the same backend key (copy-pasted backend.tf), so they share one state and one lock. That's worse than locking: each apply wants to destroy the other's resources.

Diagnose →

grep -rn 'key' --include='*.tf' --include='*.hcl' . | grep tfstate
terraform state list | head

If one stack's state list shows the other's resources, they share state.

Fix → Give each stack its own key. Split the shared state with terraform state mv -state-out= or removed/import blocks into the new key.

Prevent → Derive keys from directory path (Terragrunt path_relative_to_include()), or add a CI check that no two stacks use the same key.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors