Interview Prep/Terraform/State Locking and Backend Errors

`terraform init` warns `Warning: Missing backend configuration` and creates a local `terraform.tfstate` in CI. Later, the next pipeline wants to create everything again.

AdvancedTroubleshootingState Locking and Backend Errors
Answer

Likely causes → -backend-config was passed but there is no backend block in the code (so Terraform ignores it and uses local state), or the backend block lives in a file excluded from the job. Each CI run starts with an empty local state.

Diagnose →

grep -rn 'backend\s*"' *.tf
ls terraform.tfstate .terraform/terraform.tfstate

Fix → Add the backend block. If a CI run already created resources with local state that was thrown away, import them into the remote state or delete the orphans manually.

Prevent → Fail the pipeline if terraform.tfstate exists in the working directory after apply, and enforce a backend via policy or a wrapper script.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors