Skip to content

BackendChain API Reference

BackendChain lets you express an ordered set of backend options and pick the first viable choice at synthesis time.

Core Idea

Typical usage is:

  1. prefer a remote backend such as S3
  2. fall back to a local backend during bootstrap
  3. migrate state later once remote dependencies exist

Example

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

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

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

Important Methods

resolve()

Returns a structured backend decision.

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

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

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

decision = backend.resolve()
print(decision.backend.backend_type)
print(decision.reason)

bootstrap_ready()

Reports whether the chain contains a backend that can be bootstrapped explicitly.

ready, reason = backend.bootstrap_ready()

bootstrap_resources(...)

Attempts backend bootstrapping when supported.

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

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

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


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


# result = bootstrap(); print(result.action)
  • BackendDecision: selected backend, candidate list, and migration hints
  • BackendBootstrapResult: structured bootstrap outcome
  • ConditionalBackend: chooses between two backend definitions based on a condition

See Also