Interview Prep/Terraform/State Locking and Backend Errors

Apply was cancelled in GitHub Actions. The next run fails with the state lock held by `Who: runner@fv-az123` and the plan shows half the resources changed.

AdvancedTroubleshootingState Locking and Backend Errors
Answer

Likely causes → Cancelling a job sends SIGINT then kills the process after a short grace period. Terraform could not finish writing state or releasing the lock.

Diagnose → Check the cancelled run's log for the last Creation complete / Modifications complete lines and whether errored.tfstate was uploaded. Confirm no other runner is active.

Fix → terraform force-unlock <ID>, then terraform plan. Resources finished before the kill are usually in state (Terraform persists state periodically during apply). Import anything the log shows as created but the plan wants to create again.

Prevent → Don't cancel applies; use concurrency: { group: tf-prod, cancel-in-progress: false }. Set timeout-minutes generously and upload errored.tfstate on failure.

All Terraform questions Roadmap: Terraform in production

More on State Locking and Backend Errors