Recovering from a Partially Failed Terraform Apply

Recover a partially failed Terraform apply by resolving the error, replanning from current state, and reviewing the replacement plan.

A Terraform apply can fail after completing only part of its proposed work. The next apply should not be treated as a simple replay: Terraform does not automatically roll back completed operations. The practical problem is deciding how to establish a new planning and approval boundary before allowing further changes.

Source facts and design judgments

Explicit source facts: When Terraform or a provider encounters an error during an apply step, Terraform reports the error, updates state with changes made to resources, unlocks state, and exits. The infrastructure may be left in an invalid state because Terraform does not automatically roll back a partially completed apply. After resolving the error, the documented next step is to apply the configuration again. These behaviors are defined in the terraform apply command reference.

By default, planning reads existing remote objects, compares the configuration with prior state, and proposes actions intended to make remote objects match the configuration. Planning does not execute those proposed actions. A plan created without -out is speculative; -out=FILE instead creates a saved plan that can later be passed to apply. HashiCorp also warns that intervening changes can make the eventual effect differ from an earlier speculative plan. See the terraform plan reference.

If the selected backend supports locking, Terraform automatically locks state for operations that could write it and does not continue when locking fails. The state-locking documentation says force-unlock should be used only for your own lock when automatic unlocking failed.

Design judgment: Treat a failed apply as the end of the previous planning decision. Pause further application, resolve the reported error, create a new plan using normal refresh behavior, and review that plan before authorizing another apply.

Why the earlier plan is no longer sufficient

After partial completion, some remote changes may exist and be recorded in state while other planned operations remain incomplete. Because completed work is not automatically reversed, the actions needed from the new starting point may differ from those approved before the failure.

Saved-plan behavior makes this boundary important. Passing a saved plan file to terraform apply executes its operations without another confirmation prompt. Terraform does not accept additional planning modes or planning options in that invocation because the saved file already contains the results of those planning decisions.

This is a restriction on adding planning flags while applying a saved plan, not a restriction on the recovery workflow. A team can stop, resolve the failure, generate a replacement plan, review it, and then apply the newly approved file. Planning and applying remain separate steps: creating the replacement plan does not itself carry out its proposed infrastructure changes.

A bounded recovery workflow

  1. Pause automated retries. This is a design judgment intended to prevent another apply from starting before the failed run has been assessed.
  2. Retain the reported error. Terraform logs and reports the apply error before exiting. Use that information to identify what must be resolved before applying again.
  3. Respect lock ownership. Terraform normally unlocks state on the documented apply-error path. If automatic unlocking failed for your own operation, use only the lock ID Terraform supplied. Do not force-unlock a lock held by another operation.
  4. Resolve the reported Terraform or provider error. Keep this step distinct from authorization: correcting the cause does not apply the remaining infrastructure changes.
  5. Create a fresh plan with normal refresh behavior. Default planning reads existing remote objects. Avoid -refresh=false and -minimal-refresh for this recovery plan because the documentation says they can ignore external changes and produce an incomplete or incorrect plan.
  6. Review proposed actions. Inspect individual deletions and replacements rather than relying only on aggregate counts. For an automated two-step workflow, create the reviewed artifact with -out=FILE and pass that same saved file to terraform apply.

What state locking does not prove

State locking coordinates operations that could write state when the backend supports locking. It does not establish that every planned remote operation completed. The documented failure path can update state, release the lock, and exit without rolling back completed work.

The recovery rule is therefore narrow: preserve locking, do not presume rollback, and replace the earlier planning decision with a newly reviewed plan based on current remote objects, configuration, and state.

Law Wen Feng

Cloud and AI Architect in Malaysia. Writes about Azure infrastructure, AI agents, cloud migration, disaster recovery and Microsoft Fabric. More about Law Tech Rants