Sphere
Log in Join

Terraform 1.6 'Error acquiring the state lock' after a runner died

A CI job got cancelled mid-apply because the runner lost its Docker socket. Nobody thought much of it, since the plan had looked fine. The next pipeline failed immediately:

Error: Error acquiring the state lock

Error message: ConditionalCheckFailedException: The conditional request failed
Lock Info:
ID: 9f3c2a11-4b2d-0e19-a7c1-3f6b8d90e2aa
Path: terraform-state/prod/network.tfstate
Operation: OperationTypeApply
Who: runner@ip-10-0-3-44
Created: 2024-02-19 09:14:31 +0000 UTC
Info: Terraform v1.6.6

We were on Terraform 1.6.6 with the old S3 backend and a DynamoDB lock table, which is still what a lot of setups use. The CI box was Ubuntu 22.04 on a self-hosted GitLab runner.

## Reading the lock info

Terraform is telling you the truth here. A lock record exists in DynamoDB and the process that created it never got the chance to release it. The ConditionalCheckFailedException comes from a PutItem with a condition that the lock key must not exist. It is the expected failure mode when a lock is held.

The important field is Who. If it matches a runner that is still alive and applying, do not touch the lock. We had two pipelines queued at the time, so I checked the GitLab job log first and confirmed the old job was dead before doing anything.

Created matters too. A lock from three weeks ago is almost certainly stale. A lock from ninety seconds ago might be a colleague who is mid-apply and about to be very annoyed.

## force-unlock is fine here

I asked in the channel, got no reply for ten minutes, and ran:

$ terraform force-unlock 9f3c2a11-4b2d-0e19-a7c1-3f6b8d90e2aa
Do you really want to force unlock the state for the configured environment?
Only do this if you know for sure that nobody else is running Terraform
on this same state. (yes/no): yes
Terraform state has been successfully unlocked.

The next apply ran clean.

I know force-unlock has a reputation for being dangerous. It is dangerous if you run it while someone else is actually writing state. After a killed process it is exactly the tool you want, and refusing to use it just leaves you stuck. Take the lock ID from the error output rather than guessing it, and check Who first.

If you need to do it by hand, the record is a single item in the DynamoDB table with the lock ID as the sort key. Deleting that item is equivalent, just less friendly.

## Preventing the next one

Two things helped. We added a GitLab interruptible rule so a superseded pipeline does not get killed mid-apply, and we stopped allowing apply on branches that are not the default branch without a manual gate.

We also moved new state files to S3 native locking with use_lockfile = true in the backend block, which drops the DynamoDB table entirely. That option landed in Terraform 1.10, so it was not available to us on 1.6 at the time. If you are starting fresh on a newer version, use it. The DynamoDB table is one more thing to size and one more thing to break.

I still get a small tight feeling when a pipeline dies mid-apply. Now I check Who and Created before I panic.