Skip to content

Move Terraform/OpenTofu state from local to remote

Move an existing Terraform/OpenTofu stack from local state to S3. In this compatibility workflow, BackendChain chooses the backend to synthesize and the external CLI migrates the state. For a hybrid app, start with backends and state.

Before you start: an app with local state, the netherframe.backends.aws package, and credentials for the destination S3 backend. Register the app in [tool.nf].legacy_apps so nf synth can export its backend configuration.

Goal

Keep local state for initial bootstrap, then switch to a remote backend once the required backend resources exist.

Why This Pattern Exists

Remote state often depends on infrastructure that does not exist yet. For example, an S3 backend may require:

  • an S3 bucket
  • the locking configuration your execution engine uses

That creates a bootstrap problem. BackendChain is the clean way to model it.

Migration Diagram

flowchart LR
    A[Local backend active] --> B[BackendChain configured]
    B --> C[Deploy while local backend still wins]
    C --> D[Bootstrap or verify remote backend resources]
    D --> E[Remote backend becomes selectable]
    E --> F[Plan explicit state migration]

Step 1: Start With A Local Backend

from nf.core import App, LocalBackend, Stack


app = App(nf_id="backend-demo")
stack = Stack(nf_scope=app, nf_id="demo")
backend = LocalBackend(
    nf_scope=stack,
    nf_id="local",
    path=".terraform/local.tfstate",
)

Step 2: Replace It With BackendChain

from nf.backends.aws.s3 import S3Backend
from nf.core import App, BackendChain, BackendSettings, LocalBackend, Stack


app = App(nf_id="backend-demo")
stack = Stack(nf_scope=app, nf_id="demo")
backend = BackendChain(
    nf_scope=stack,
    nf_id="backend_chain",
    backends=[
        S3Backend(
            nf_scope=stack,
            nf_id="s3",
            bucket="my-company-terraform-state",
            key="terraform.tfstate",
            region="us-east-1",
            nf_backend_settings=BackendSettings(register=False),
        ),
        LocalBackend(
            nf_scope=stack,
            nf_id="local",
            path=".terraform/local.tfstate",
            nf_backend_settings=BackendSettings(register=False),
        ),
    ],
)

Step 3: Synthesize and inspect

nf synth
nf plan --stack .nf.out/backend-demo/demo

At this stage the chain will typically still select local state.

Step 4: Bootstrap Or Verify Remote Backend Resources

Use either:

  • nf backend bootstrap
  • explicit bootstrapper code
  • an out-of-band manual setup if that matches your process

A chain whose backends can bootstrap their resources does it from code too:

from nf.core import BackendChain
from nf.core.constructs.backend_base import Backend


def bootstrap(backend: BackendChain) -> None:
    # `backend` is the chain from Step 2. This checks for the bucket, so it needs AWS credentials
    result = backend.bootstrap_resources(
        policy=Backend.BootstrapPolicy.IF_MISSING,
        dry_run=False,
    )
    print(result.action)

Step 5: Re-Synthesize And Check Selection

from nf.core import BackendChain


def show_selection(backend: BackendChain) -> None:
    # `backend` is the chain from Step 2
    decision = backend.resolve()
    print(decision.backend.backend_type)
    print(decision.reason)

Once the remote backend is selectable, re-run nf synth.

Step 6: Migrate state explicitly

After confirming that the synthesized backend configuration names the intended S3 destination, use the external engine's migration flow in the same working directory that held the local state:

terraform -chdir=.nf.out/backend-demo/demo init -migrate-state
terraform -chdir=.nf.out/backend-demo/demo plan

Review the migration prompt and verify that the remote state contains the expected objects. Preserve a copy of the original state before the migration. BackendChain selects a backend; it does not itself copy local state into that destination. If your Terraform working directory or state path differs from this example, keep using the original location rather than initializing an unrelated empty state.

Continue with state operations and CI/CD once the backend and plan are verified.

Notes

  • Keep register=False (in BackendSettings) on the backends inside a chain: the chain is the stack's backend, not them.
  • Keep the same local state path during the migration if you already have state in a known location.