Snippets

Terraform snippet: diagnose a stuck state lock before force-unlock

A short procedure to qualify a stuck Terraform lock, confirm that no apply is still active, and restart with a clean plan.

02 Jun 2026 terraformstatelockcirunbook

terraform force-unlock should not be the first reflex. The lock protects the state from concurrent writes. Before removing it, prove that the related pipeline or apply is no longer active.

text terraform-lock-checklist.txt
1. Copy the Lock ID displayed by Terraform
2. Identify the owning pipeline, job, or user
3. Confirm that no apply is still running
4. Notify the relevant team if the lock comes from shared CI
5. Remove the lock only after confirmation
6. Run terraform plan before any apply

The unlock command is simple, but the intervention should be traceable.

bash terraform-force-unlock.sh
LOCK_ID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
terraform force-unlock "$LOCK_ID"
terraform plan

If the next plan shows unexpected changes, stop the intervention and return to backend, CI, and real infrastructure state diagnostics.