Skip to content

Rollback and Recovery

Rollback in infrastructure is rarely a single button. Netherframe can help you inspect and control changes, but safe recovery still depends on understanding which execution path you are using and what state already changed.

First Classify The Failure

Ask first:

  • did the failure happen during synth, plan, apply, or destroy?
  • is this a legacy stack or a hybrid app?
  • did the provider change real infrastructure already?
  • is the problem mainly state, backend, credentials, or model drift?

Do not start editing code until you know which of those categories you are in.

Recovery Flow Diagram

flowchart TD
    A[Failure occurs] --> B{Legacy stack or hybrid app?}
    B -->|Legacy| C[Re-run plan and inspect diff or state]
    B -->|Hybrid| D[Re-run plan at app scope and inspect runtime configuration]
    C --> E{State or backend issue?}
    D --> E
    E -->|Yes| F[nf backend show or validate and nf state]
    E -->|No| G[Correct model, provider config, or environment issue]
    F --> H[Make one corrective change]
    G --> H
    H --> I[Re-run plan before apply]

Legacy Stack Recovery

For synthesis-driven stacks, prefer this order:

  1. re-run nf plan --stack ...
  2. inspect nf diff --stack ...
  3. inspect outputs or state with nf show and nf state
  4. correct the model or environment problem
  5. apply again only after the plan is understandable

If the failure happened after partial resource creation, treat it as a state and model reconciliation problem, not just a retry problem.

Hybrid Recovery

Choose the strategy before starting an apply:

Strategy On failure Use when
CHECKPOINT (default) Keeps completed changes in state and saves the pre-apply state You want to inspect partial progress and normally continue with a new plan
ROLLBACK Attempts to delete objects this apply created; reports completed changes it cannot reverse The new objects are disposable and deleting them is an acceptable recovery action
NONE Keeps completed changes in state without creating a checkpoint or attempting rollback Your own recovery process manages snapshots and remediation

For a worked example, a release upload whose second step fails and whose first is then deleted, with the output it prints, see Why Netherframe.

A failed nf destroy follows the same strategy: CHECKPOINT saves the state before the destroy, and ROLLBACK, having nothing it created to delete, reports the deletions it cannot revert.

Configure it in Python or for one command:

from nf.core import App

app = App(nf_id="platform", recovery_strategy="CHECKPOINT", enable_progress_reporting=True)
nf apply --app platform --recovery-strategy checkpoint

The flag (on nf apply, nf destroy and nf deploy) wins over recovery_strategy in [tool.nf.config], which wins over the App argument.

Continue after a failed apply

  1. Read the resource failures and recovery notes. Check the cloud object and stored state for each failed call; a failed RPC can have changed infrastructure.
  2. Correct the cause (for example credentials, permissions, a conflicting name or configuration).
  3. Run nf plan --app platform against the same backend and output directory. Review the partial progress and any objects reported missing before applying again.
  4. Apply the reviewed plan. A retry is a new plan/apply, not resumption of a provider RPC.

If storing state failed, preserve <outdir>/errored.tfstate and reconcile it with the backend before another apply. It contains changes the run recorded but could not store. See State Operations and Safety.

Stop an apply in progress

HybridExecutionMode.request_stop() stops the apply or destroy in progress, as Terraform's first interrupt does. It is safe to call from another thread (the run's own approval callback included). The first Ctrl-C of nf apply, nf destroy and nf deploy calls it once the run is changing infrastructure (a second stops without waiting; see nf apply), and so does the TUI's ctrl-k (see nf tui):

  • resources being applied finish and are recorded in state
  • nothing that has not started starts: no provider call is made and, on a graph state backend, no operation is reserved, so nf state recover has nothing to resolve for them
  • the run ends unsuccessful with ExecutionResult.stopped set and an error starting Stopped by user, counting the changes not started
  • the recovery strategy then applies as for any failed apply: CHECKPOINT keeps the pre-apply checkpoint, ROLLBACK deletes what the run created (rollback itself is not stopped)

A stop asked while the run waits for its approval ends it without changes. request_stop() returns False when no apply or destroy is in progress, and nothing is kept for the next run. changes_started says whether the run is past its approval, which is when stopping (rather than aborting) matters. Continue as after a failed apply: plan again to apply the changes that did not start.

What rollback can reverse

ROLLBACK deletes newly created objects in dependency order, including a failed create whose provider returned an object that nf recorded. It restores recorded outputs that came from the deleted objects to their pre-apply values. Imported objects leave nf state but remain in the cloud: adoption did not create them.

It cannot undo completed updates, deletes or replacements. For example, if an apply updates an existing SSM parameter, creates a second parameter and then fails on a third, rollback deletes the second parameter while the first retains its updated value. The result reports that update as not reverted. A failed rollback deletion is also reported; inspect the remaining object and state before retrying.

Rollback is skipped if another run took over the state, to avoid deleting objects that run now manages. Preserve the errored state file and investigate the ownership conflict.

Find and restore a checkpoint

The run logs the checkpoint location. Supported remote backends keep it beside their state (<state>.checkpoints/<name> for S3, GCS, Azure and Consul); PostgreSQL stores a row, and HCP Terraform stores a state version. Local state and fallback copies go under <outdir>/checkpoints/. The newest five are retained, except HCP Terraform retains all state versions. A fallback on the runner must be retained as a CI artifact.

A checkpoint restores state only. It does not restore deleted cloud objects or revert updates. Usually continuing from the partial state with a fresh plan is preferable. Restore an old checkpoint only after reconciling the infrastructure to match it; otherwise nf may attempt to create objects that still exist or lose track of completed changes.

Restore through the app's configured HybridExecutionMode (hybrid below), with the checkpoint location the run reported.

# coding: nf_autoid

from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.random import Pet, RandomProvider

with App(nf_id="platform") as app, Stack() as names, BindContext:
    random: Bind = RandomProvider()
    service: Bind = Pet(nf_provider=random, length=2)

hybrid = app.enable_hybrid_execution_local(path=".nf.out/platform/nf.tfstate")


def restore(location: str) -> object:
    """Put the state a checkpoint holds back, once the infrastructure matches it again, and plan from it.

    ``location``: what the failed run reported, e.g. ``.nf.out/checkpoints/pre-apply-20261120T200852185556Z-3f704802.json``.
    """
    hybrid.restore_checkpoint(location)  # takes the state lock
    return hybrid.plan()

restore_checkpoint() takes the state lock. Check the new plan before applying; do not copy an old state file over a backend while another run is active.

Backend And State Recovery

If recovery looks state-related:

nf backend show
nf backend validate
nf state --stack network list

Typical state-recovery situations:

  • backend resources exist but are unreachable from the current environment
  • migration from local to remote state is only partially complete
  • imported resources do not match the modeled resource addresses

Practical Recovery Rules

  • make one corrective change at a time
  • re-run plan before re-running apply
  • prefer explicit state inspection over assumptions
  • avoid manual state edits unless normal model fixes cannot solve the issue
  • document the exact command sequence if the failure happened in CI