Before every plan and apply OpenTofu locks the state in the backend so that two runs never write at the same time. If a run crashes, the lock stays – and every further run fails until someone releases it on purpose.
Symptom
tofu plan -no-color -lock-timeout=5s 2>&1 | grep -E '^Error|ID:|Who:' Error: Error acquiring the state lock ID: 7d1c2a9e-0b6f-4c1e-9a55-3f0e8b2d4c11 Who: kollege@laptop
“My colleague started an apply this morning, then his laptop crashed. Since then nothing works for me.”
Diagnosis
Next to the state there is a .tflock file in the bucket.
s3ls 'tofu-state?list-type=2' | grep -o '<Key>tag15[^<]*</Key>' <Key>tag15/terraform.tfstate</Key> <Key>tag15/terraform.tfstate.tflock</Key>
Causes
WhoOrphaned lock of an aborted run
The colleague's run set the lock and never released it. OpenTofu cannot know whether it is still running.
-lock-timeoutWaiting does not help
-lock-timeout waits a while for the release – good against short overlaps, useless against an orphaned lock.
Fix
tofu force-unlock -force 7d1c2a9e-0b6f-4c1e-9a55-3f0e8b2d4c11 -no-color | head -1 tofu plan -no-color | tail -1 OpenTofu state has been successfully unlocked! No changes. Your infrastructure matches the configuration.
Don't: Run force-unlock out of habit – if an apply is still running behind the lock, two runs write at once and can destroy the state.
OpenTofu hands-on, Day 15: fix eight faults in state, providers, modules and encryption against the clock.
Go to day 15 →
FAQ
How do I know who holds the lock?
The message shows the lock info with ID, Operation, Who and Created – Who is user@machine of the locking run.
Does this apply to Terraform too?
Yes, locking and force-unlock work the same way in Terraform.