Skip to content

nf backend

Show the state backend the settings choose, and create or check its resources.

Synopsis

nf backend show|bootstrap|validate [OPTIONS]

The Backend

The settings choose it: backend_type and its options, as flags of nf (written before or after the command) or in [tool.nf.config]. Without them it is local. It is the backend of everything whose code names none:

  • legacy synthesis: the Terraform backend block of a stack with none
  • hybrid runs (nf plan|apply|destroy --app): the state of an app that names no state backend
  • nf drift: the state it refreshes resources from
Setting Flag Meaning
backend_type --backend-type TYPE local (default) or s3; another type gets a legacy backend block without options
backend_s3_bucket --backend-s3-bucket NAME The bucket
backend_s3_key_prefix --backend-s3-key-prefix PREFIX What each state's key starts with
backend_s3_region --backend-s3-region REGION The region (default AWS_DEFAULT_REGION)
backend_s3_profile --backend-s3-profile PROFILE The AWS profile state is read and written with (default the AWS SDK's credential chain)
backend_local_path --backend-local-path FILE The local state file

Where states go:

Backend A hybrid app's state A legacy stack's state
local backend_local_path, else .nf.out/nf.tfstate backend_local_path, else terraform.tfstate in the stack's directory
s3 s3://BUCKET/PREFIX/nf.tfstate s3://BUCKET/PREFIX/STACK/terraform.tfstate

Nothing is saved: the settings are read each time, so a flag always wins over [tool.nf.config]. Put a project's backend in [tool.nf.config]:

[tool.nf.config]
backend_type = "s3"
backend_s3_bucket = "acme-nf-state"
backend_s3_key_prefix = "platform"
backend_s3_region = "us-east-1"
backend_s3_profile = "platform-state"  # optional: else the AWS SDK's credential chain

A run stops when the settings moved an app's state and left it behind: the configured state was never written, but .nf.out/nf.tfstate still holds resources. It says how to move the state (aws s3 cp .nf.out/nf.tfstate s3://...); a state emptied by a destroy does not stop it.

In code, app.enable_hybrid_execution_s3(bucket=..., key=..., region=..., profile=...) names the backend, profile included, for that app instead of the settings.

A hybrid app's state in another backend type (gcs, azurerm, PostgreSQL, graph state) is named in its code: app.enable_hybrid_execution(state_backend=...).

Creating It On The First Apply

With bootstrap_backend on (nf apply --bootstrap-backend, or bootstrap_backend = true in [tool.nf.config]), a hybrid run creates the backend's storage when it does not exist yet:

  • Before reading state, the backend checks, reading only, that its storage exists (S3: HeadBucket, and DescribeTable for its lock table). A backend that exists needs no permission to create anything, and starts no provider.
  • A plan made before it exists plans against empty state, without a lock, and says so. A saved plan made then applies once the apply has created it.
  • An apply creates it first, before taking the lock: the bucket (versioning, encryption, a TLS-only policy, public access blocked) and the lock table. It uses the app's own configuration of hashicorp/aws, with the app's credentials, profile and role, in the backend's region: bootstrap_backend_provider names one (network.aws, aws.west), else the app's default (its unaliased configuration; several stacks must configure it alike). An app without one uses the provider's default credentials. It creates nothing unless that provider acts as the account the backend reads and writes state as (aws_caller_identity against STS with the backend's credentials). The run's output says what it created.

nf backend bootstrap creates it on its own, without an app, with the default credentials.

Commands

Examples

nf backend show
nf --backend-type s3 --backend-s3-bucket acme-nf-state --backend-s3-region us-east-1 backend show
nf backend bootstrap --dry-run
nf backend bootstrap --tag Team=platform
nf backend validate

In Python: Project().backend.show(), .bootstrap(...), .validate(), with the settings in Project(settings={"backend_type": "s3", ...}).