Skip to content

Backend Chain

BackendChain solves the bootstrap problem for remote state backends.

The Problem

Remote state usually depends on infrastructure that does not exist yet. For example, an S3 backend may require an S3 bucket and lock table that are themselves created by the stack.

BackendChain lets you declare:

  • a preferred remote backend
  • a local fallback backend
  • the migration path between them

Basic Example

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


app = App(nf_id="my-app")
stack = Stack(nf_scope=app, nf_id="my-app")

state_bucket = Resource(
    nf_scope=stack,
    nf_id="state_bucket",
    nf_type="aws_s3_bucket",
    bucket="replace-with-a-globally-unique-name",
)

lock_table = Resource(
    nf_scope=stack,
    nf_id="state_locks",
    nf_type="aws_dynamodb_table",
    name="replace-with-a-unique-lock-table",
    billing_mode="PAY_PER_REQUEST",
    hash_key="LockID",
    attribute=[{"name": "LockID", "type": "S"}],
)

backend = BackendChain(
    nf_scope=stack,
    nf_id="backend_chain",
    backends=[
        S3Backend(
            nf_scope=stack,
            nf_id="s3",
            bucket=state_bucket,
            key="terraform.tfstate",
            dynamodb_table=lock_table,
            region="us-east-1",
            nf_backend_settings=BackendSettings(register=False),
        ),
        LocalBackend(
            nf_scope=stack,
            nf_id="local",
            path=".terraform/bootstrap.tfstate",
            nf_backend_settings=BackendSettings(register=False),
        ),
    ],
)

How Selection Works

At a high level, the chain prefers:

  1. a backend that already has state
  2. otherwise, the first backend whose dependencies are satisfied
  3. otherwise, the fallback backend

That behavior keeps the team on a consistent backend once state exists.

Typical Workflow

  1. Run nf synth.
  2. The chain falls back to local state if the remote backend dependencies are missing.
  3. Apply infrastructure so the remote backend resources are created.
  4. Re-synthesize and confirm the remote backend is now selectable.
  5. Migrate state with Terraform if required.

Useful APIs

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

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

decision = backend.resolve()
ready, reason = backend.bootstrap_ready()

If the backend supports explicit bootstrapping:

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

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


def bootstrap():
    """Create the remote backend's resources (needs credentials, so it is not called here)."""
    return backend.bootstrap_resources(
        policy=Backend.BootstrapPolicy.IF_MISSING,
    )