When a Terraform run stops mid-flight

A cancelled pipeline, a laptop that goes to sleep, or a dropped VPN connection can leave a Terraform state lock behind. The next plan or apply then fails with an error saying the state is locked. It is tempting to reach straight for force-unlock, but that is how teams end up with corrupted state and confusing drift. Recovery is usually straightforward, provided you slow down for a moment and confirm what actually happened.

Work out whether the lock is genuinely orphaned

State locking exists to stop two people or pipelines writing to the same state at the same time. A lock is therefore not automatically a problem. Before you remove it, check whether another run is still active. Look at your CI system, ask colleagues, and inspect the lock metadata. With an S3 backend using DynamoDB, the lock item records an ID, who created it, when, and which operation was running. That information is your starting point, not an inconvenience. A lock that has been sitting for hours is suspicious; a lock that appeared two minutes ago probably means someone is working.

  • Check the pipeline that last touched the stack. Was it cancelled, timed out, or still shutting down?
  • Ask whether anyone is running Terraform locally against the same workspace.
  • Compare the lock timestamp with deployment logs and recent changes.
  • Confirm the state file itself looks healthy and recent enough to trust.

Recover with the lock ID, not guesswork

When you are confident the lock is stale, use terraform force-unlock with the exact lock ID. This is safer than deleting the lock item by hand because Terraform validates the ID and keeps the action visible in the run history. If the lock record is damaged and force-unlock cannot work, manual removal may be the only option, but treat that as an incident: take a state backup first, record who removed what, and run a plan immediately afterwards to inspect the result. A good companion resource on this is www.thevarsityjacket.shop.

After unlocking, do not rush into apply. Run terraform plan and read it. An interrupted apply may have partially changed infrastructure, so the plan is your first sanity check. If the plan is unexpectedly large, stop and investigate rather than accepting it as normal drift.

Patterns that reduce interrupted-run pain

Most lock incidents are operational, not Terraform bugs. A few habits make recovery calmer and less frequent. The aim is not to eliminate locks, but to make them short-lived and visible.

  • Give pipeline jobs sensible timeouts and cancellation hooks, so a stuck run releases its lock rather than lingering overnight.
  • Separate plan and apply stages, and keep apply runs short and single-purpose. Long, sprawling applies are more likely to be interrupted.
  • Use retries with backoff for transient network failures, but do not retry blindly after a lock error. Check first.
  • Monitor the lock table and alert on locks older than an agreed threshold. A quiet alarm is better than a Friday afternoon surprise.
  • Keep a short runbook that names the lock ID location, the force-unlock command, and the person or team to contact.
  • Discourage local applies against shared state. If they are unavoidable, use the same locking backend and announce the run.

What not to do

Do not disable state locking to make the error go away. That trades a visible failure for silent corruption. Do not force-unlock while another apply is still running, even if the pipeline looks stuck; cancel it properly first. Do not delete lock items by hand as a routine fix, and do not assume the state is intact after an interrupted apply. Finally, avoid having CI and a laptop apply to the same workspace at the same time. Locking is there to stop that behaviour, not to be worked around. That approach trades convenience for risk.

A calm recovery checklist

  • Confirm no active run owns the lock.
  • Capture the lock ID and metadata for the audit trail.
  • Use force-unlock with that ID, not a manual table edit.
  • Run plan and review the diff before applying.
  • Record the cause and adjust timeouts or runbooks if needed.

Interrupted runs are normal in distributed systems. Treat the leftover lock as a signal to verify, not an emergency to bypass. With a little discipline, recovery becomes a five-minute check rather than a state-file rescue mission. Document the fix so the next person does not have to rediscover it.