Resolving Terraform State Locking and Concurrency Deadlocks
Scenario Prompt:
“A CI/CD pipeline failed midway through a terraform apply due to a runner timeout. Subsequent runs fail with "Error acquiring the state lock". Developers cannot deploy hotfixes. How do you safely resolve this without risking state corruption?”
Substantive Technical Concepts to Hit:
Lock Verification
Verify in AWS DynamoDB (or Consul/GCS) that the lock ID matches the dead CI job and that no orphaned background process is still mutating cloud resources.
terraform force-unlock
Execute terraform force-unlock <LOCK_ID> with explicit audit logging and peer confirmation to release the lock safely.
Drift & State Refresh
Run terraform plan -refresh-only to detect what resources were created or modified before the runner timed out.
Atomic CI Architecture
Configure pipeline timeouts and trap handlers to ensure CI runners release locks on SIGTERM or cancellation signals.
- Can we confirm with 100% certainty that the original timed-out runner process was killed and is not still applying changes?
- What backend is storing our state lock (DynamoDB table, Terraform Cloud, or GCS)?
- Do we have state versioning enabled in the S3 backend so we can rollback if corruption occurs?