Skip to content

Lesson 3 of 9 · 27m

Locking, Partial Applies, and State Recovery

Handle stale locks, failed applies, orphaned objects, and state surgery without compounding the original incident.

Terraform failures are rarely total failures. The graph partially applies, the provider times out, the network drops, and now some objects exist, some were modified, and state is a mixture of before, during, and after. The practitioner skill is not avoiding every failure. It is refusing to guess during recovery.

Respect the lock until you understand it

A lock error usually means another apply is active or one ended badly:

Error acquiring the state lock
Lock Info:
  ID:        7b01a2f1-...
  Path:      prod/network/terraform.tfstate
  Operation: OperationTypeApply
  Who:       deployer@ci

Do not reflexively force-unlock. Find whether the process is alive. Check CI jobs, operator shells, and provider operations that may still be completing. If the lock is genuinely stale, force unlock by ID:

terraform force-unlock 7b01a2f1-4d65-9e43-a2b0-example

That command does not repair infrastructure. It only removes the mutex. The next step is assessment.

After a failed apply, build a factual inventory

Run a normal plan and inspect state and cloud reality:

terraform plan -detailed-exitcode
terraform state list
terraform state show aws_db_instance.main

Then compare with the cloud console or API. You are answering three questions: which objects exist, which are recorded in state, and which configuration addresses now disagree. Never start deleting manually because the console looks messy.

Import orphans instead of recreating them

If Terraform created an object but failed before recording it, a second apply may try to create a duplicate and collide on a unique name. Import the real object into the intended address:

terraform import aws_db_subnet_group.main prod-main-subnets
terraform plan

A correct import should produce a small plan reflecting configuration differences, not a replacement storm. If Terraform insists on replacement because immutable attributes differ, decide whether to change configuration or deliberately replace the object.

Move and remove state deliberately

Use state operations when ownership changes, not as a shortcut around design:

terraform state mv aws_instance.old aws_instance.api
terraform state rm aws_cloudwatch_log_group.legacy

state mv changes Terraform's address for an object. state rm makes Terraform forget it while leaving the remote object alive. Removal is appropriate when intentionally handing ownership to another stack or operator. It is not a cleanup command.

Backup before surgery

Before manual state operations, snapshot the state:

terraform state pull > state-backup-$(date +%Y%m%d%H%M%S).json

Backend versioning is even better because it is automatic and shared, but a local pull before surgery gives you a concrete comparison point. Store it securely; state commonly contains sensitive data.

Reapply is sometimes the correct repair

Terraform is declarative but providers are imperfect. If the failure was a transient timeout after successful remote mutation, a refresh and re-apply may converge cleanly. The discipline is to inspect first, apply second. A retry is a recovery tool only after you know which side effects already occurred.

Field notes

  • Force-unlocking a live operation can create exactly the split-brain state the lock existed to prevent.
  • Cloud operations often continue after the client times out. Check the provider before assuming nothing happened.
  • Imports are resource-specific and provider-specific; read the resource documentation for the exact identity format.
  • Never edit state JSON to 'fix' values unless no supported CLI path exists, and even then review the diff with another engineer.
  • Post-incident, record why the graph failed. Flaky provider operations need retries, smaller changes, or a bug report — not folklore.

Was this lesson helpful?

Sign in to rate