Terraform: 'Error Acquiring the State Lock'

A crashed run or killed CI job leaves the state lock held. The safe fix takes 30 seconds once you confirm nothing is actually running.

What you'll see

Root causes

Interrupted run

CI job killed mid-apply, laptop slept, Ctrl-C at the wrong moment. The lock never released.

Genuinely concurrent run

Two pipelines ran at once and one holds the lock legitimately.

Fix it

  1. Confirm no other apply is actually running (check CI and teammates)
    ps aux | grep terraform; # plus check your CI dashboard
  2. Force-release the lock by its ID from the error message
    terraform force-unlock <LOCK_ID>
  3. Re-run and confirm it acquires cleanly
    terraform plan -detailed-exitcode

Field note

Force-unlocking while another apply is genuinely in progress can corrupt state. The 60 seconds spent verifying nothing is running is the whole safety margin.

Common questions

How do I safely remove a stuck state lock?

First verify nobody is actually running an operation (check your team and CI): force-unlock after confirming the holder crashed. terraform force-unlock <lock-id> — the ID is in the error output. Force-unlocking an active run corrupts concurrent state writes.

Why does the lock keep reappearing?

A crashed runner (SIGKILL'd CI job) leaves the lock behind each run. Fix the crash-and-kill pattern — failed runs should exit, not be force-killed at the job level — or add a lock-cleanup step with verification to the pipeline.

Ship it right the first time

An opinionated VPC module: per-AZ NAT, explicit dependencies, EKS-ready outputs.

Terraform AWS Foundation — $37 →

One-time. Yours to modify. Instant download from the NinjaOps template store.