A CI/CD Terraform apply job was killed abruptly by an OOM error or spot termination. Subsequent runs fail with 'Error acquiring the state lock'. How do you safely resolve this?
“First, verify that no concurrent CI runner or team member is genuinely modifying the infrastructure by checking CI/CD execution status and the lock timestamp. Once confirmed orphaned, run terraform force-unlock <LOCK_ID> using the Lock ID reported in the error message. I never delete the lock entry directly from DynamoDB unless the CLI unlock command is strictly unavailable. Immediately following unlock, execute terraform plan to detect whether the interrupted apply left partially provisioned resources or state divergence.”
S Situation
An AWS spot CI/CD runner running terraform apply was terminated mid-run during a VPC subnet rollout, leaving DynamoDB state locked and blocking 18 developer deployments.
T Task
Safely release the state lock without introducing concurrent modification hazards or state corruption.
A Action
1. Retrieved Lock ID e84f7b2c-9a41-419b-a0ef-182390f7d541 from runner output.
2. Confirmed the originating EC2 spot instance was terminated.
3. Executed terraform force-unlock e84f7b2c-9a41-419b-a0ef-182390f7d541.
4. Executed terraform plan -refresh-only to reconcile an orphaned subnet into state before re-running the pipeline.
R Result
Lock was cleanly released in under 2 minutes, state consistency was confirmed, and subsequent pipeline runs completed successfully.
Explain the architectural prevention: Configure GitHub Actions or GitLab CI with concurrency groups (e.g. concurrency: production-terraform-apply). This guarantees that only one pipeline can ever execute simultaneously, eliminating lock collisions, and pin apply runners to On-Demand compute rather than interruptible Spot instances.