Sphere
Log in Join

Terraform 1.7 S3 backend 'Error acquiring the state lock' after a crashed apply

The runner lost its disk in the middle of a Terraform apply on a Friday afternoon. Nothing got committed, the pipeline went red, and every subsequent run failed before it could even plan. Terraform 1.7.5, S3 backend with a DynamoDB lock table, which is still the setup a lot of us have.

## The error

$ terraform plan -input=false

Error Error acquiring the state lock: ConditionalCheckFailedException: The conditional request failed

Lock Info:
ID: 8f3c2a10-9d4b-4a1e-b7c9-2e5f6a0d1c33
Path: mycompany-tfstate/prod/network.tfstate
Operation: OperationTypeApply
Who: runner@ip-10-0-3-41
Version: 1.7.5
Created: 2024-03-08 15:22:41 +0000 UTC
Info:

The scary part is the "OperationTypeApply". It looks like an apply is still happening. It is not. The runner was terminated, so the process that created the lock is gone.

## Why the lock stays

Terraform writes an item into DynamoDB before touching the state file and deletes it at the end. There is no TTL and no heartbeat, so if the process dies without running its cleanup, the item sits there forever. This is by design: a stale lock is much safer than two applies writing the same state.

The instinct is to run terraform force-unlock immediately. Do not, at least not before checking who owns it.

## Checking what actually holds it

First I looked at the lock record directly:

aws dynamodb get-item \
--table-name terraform-locks \
--key '{"LockID":{"S":"mycompany-tfstate/prod/network.tfstate-md5"}}' \
--output json

That gave me the same ID, user, and timestamp. Then I checked whether the instance was still alive:

aws ec2 describe-instances \
--filters "Name=private-ip-address,Values=10.0.3.41" \
--query 'Reservations[].Instances[].State.Name'
[]

No instance. That is enough evidence that the lock is orphaned. I also asked in the team channel, because the other way this happens is a colleague running apply from their laptop with a shared state file.

## Clearing it

With the ID from the plan output:

terraform force-unlock 8f3c2a10-9d4b-4a1e-b7c9-2e5f6a0d1c33
Do you really want to force-unlock? [yes/no]: yes
Terraform state has been successfully unlocked!

Then a fresh terraform plan showed a clean diff with no unexpected changes, which matters. If a crashed apply had partially written state, you would see resources recreating. We were lucky: the lock is acquired before any writes.

## Stopping it from happening again

Three things changed in the pipeline. The job now has a hard timeout longer than the longest apply we have seen but short enough to kill a hung one, and the runner has enough disk that it does not die. I also added -lock-timeout=3m to the plan step so a lock held by a running job waits instead of failing instantly, which was annoying us on concurrent PR plans.

The bigger fix is separating state by environment and keeping production applies behind a single approval job. We used to have four pipelines sharing one state file, and even without crashes they were stepping on each other. That was the real problem; the crashed runner just made it obvious.