State Backends Setup and Permissions
Every hybrid app stores state in a backend—either selected globally via backend_*
settings ([tool.nf.config] / nf flags) or configured explicitly in Python on the
App. This guide provides copy-pasteable setup, locking behavior, and minimal IAM/RBAC
permissions for every backend Netherframe ships.
For how state locking, conditional writes, and checkpoints work conceptually, see Backends and State.
Backend comparison
| Backend | Package | Locking & concurrency | Checkpoints |
|---|---|---|---|
| Local | netherframe.core |
OS flock (POSIX) / heartbeat lockfile (Windows); atomic fsync rename |
<outdir>/checkpoints/ (last 5) |
| AWS S3 | netherframe.backends.aws |
DynamoDB lock table + conditional S3 writes | <state>.checkpoints/ in bucket (last 5) |
| Google Cloud Storage | netherframe.backends.gcp |
GCS object-generation preconditions + lock object | <state>.checkpoints/ in bucket (last 5) |
| Azure Blob Storage | netherframe.backends.azure |
Azure Blob leases + ETag conditional writes | <state>.checkpoints/ in container (last 5) |
| HashiCorp Consul | netherframe.backends.consul |
Consul sessions + ModifyIndex CAS | <state>.checkpoints/ in KV store (last 5) |
| HCP Terraform / TFE | netherframe.backends.remote |
Workspace lock + state versions | Every state version retained |
| PostgreSQL (Document) | netherframe.backends.postgresql |
Row/advisory locks + heartbeat + serial CAS | Stored in PostgreSQL checkpoint table |
| PostgreSQL (Graph) | netherframe.backends.postgresql |
Renewable leases, fenced ops, --concurrent-applies resource grants |
Row-level nf_checkpoints + full nf_versions history |
1. Local file backend (default)
No extra package required. Best for learning, offline tests, and single-developer rehearsals.
from nf.core import App
app = App(nf_id="demo")
app.enable_hybrid_execution_local(path=".nf.out/demo.tfstate")
Or via pyproject.toml:
[tool.nf.config]
backend_type = "local"
backend_local_path = ".nf.out/demo.tfstate"
2. AWS S3 (netherframe.backends.aws)
uv add netherframe.backends.aws
Option A: Configure in pyproject.toml (recommended for team projects)
[tool.nf.config]
backend_type = "s3"
backend_s3_bucket = "acme-nf-state-prod"
backend_s3_key_prefix = "platform" # hybrid state -> platform/nf.tfstate
backend_s3_region = "us-east-1"
backend_s3_profile = "prod-ops" # optional; defaults to AWS SDK chain
bootstrap_backend = false # set true to auto-create bucket/lock on first apply
Option B: Configure explicitly in Python
from nf.core import App
app = App(nf_id="platform")
app.enable_hybrid_execution_s3(
bucket="acme-nf-state-prod",
key="platform/nf.tfstate",
region="us-east-1",
dynamodb_table="acme-nf-locks",
encrypt=True,
)
Bootstrap and validate storage from the CLI:
nf backend show
nf backend bootstrap --tag Team=platform --tag ManagedBy=netherframe
nf backend validate
Minimal AWS IAM permissions
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": "arn:aws:s3:::acme-nf-state-prod"
},
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject", "s3:DeleteObject"],
"Resource": "arn:aws:s3:::acme-nf-state-prod/platform/*"
},
{
"Effect": "Allow",
"Action": ["dynamodb:GetItem", "dynamodb:PutItem", "dynamodb:DeleteItem"],
"Resource": "arn:aws:dynamodb:us-east-1:123456789012:table/acme-nf-locks"
}
]
}
Add kms:Decrypt and kms:GenerateDataKey on your KMS key if using SSE-KMS.
3. Google Cloud Storage (netherframe.backends.gcp)
uv add netherframe.backends.gcp
from nf.backends.gcp import GcsStateBackend
from nf.core import App
def web() -> App:
app = App(nf_id="web")
backend = GcsStateBackend(
bucket="acme-nf-state-prod",
prefix="web/prod",
project="acme-prod",
)
app.enable_hybrid_execution(state_backend=backend)
return app
- Authentication: Uses Application Default Credentials (
gcloud auth application-default loginor Workload Identity Federation), or passcredentials="/path/to/key.json". - Minimal IAM role:
roles/storage.objectUseron the state bucket (enable Uniform Bucket-Level Access and Object Versioning on the bucket).
4. Azure Blob Storage (netherframe.backends.azure)
uv add netherframe.backends.azure
from nf.backends.azure import AzurermStateBackend
from nf.core import App
app = App(nf_id="storage")
backend = AzurermStateBackend(
storage_account_name="acmenfstateprod",
container_name="nfstate",
key="storage/prod.tfstate",
resource_group_name="rg-nf-state",
use_msi=True,
)
app.enable_hybrid_execution(state_backend=backend)
- Minimal Azure RBAC role: Storage Blob Data Contributor scoped to the
nfstatecontainer (supports blob leasing and ETag conditional writes). Enable blob versioning and soft delete on the storage account.
5. HashiCorp Consul (netherframe.backends.consul)
uv add netherframe.backends.consul
from nf.backends.consul import ConsulStateBackend
from nf.core import App
app = App(nf_id="edge")
backend = ConsulStateBackend(
address="consul.internal:8500",
path="netherframe/edge/prod",
scheme="https",
)
app.enable_hybrid_execution(state_backend=backend)
- Minimal Consul ACL policy:
key_prefix "netherframe/edge/prod" { policy = "write" }andsession_prefix "" { policy = "write" }for lock sessions.
6. HCP Terraform / Terraform Enterprise (netherframe.backends.remote)
uv add netherframe.backends.remote
from nf.backends.remote import RemoteStateBackend
from nf.core import App
app = App(nf_id="core")
backend = RemoteStateBackend(
hostname="app.terraform.io",
organization="acme",
workspaces={"name": "core-prod"},
)
app.enable_hybrid_execution(state_backend=backend)
- When using
workspaces={"prefix": "core-"}, setTF_WORKSPACEto select the workspace. - Supply an API token via
token=or Terraform's~/.terraform.d/credentials.tfrc.json.
7. PostgreSQL: Document vs. Graph State (netherframe.backends.postgresql)
uv add netherframe.backends.postgresql
PostgreSQL Graph State (recommended for PostgreSQL)
Stores state as transactional rows with fenced operations, full version history, and
opt-in --concurrent-applies / --graph-plan-scope affected:
from nf.backends.postgresql import PostgreSQLGraphBackend
from nf.core import App
app = App(nf_id="parameters")
backend = PostgreSQLGraphBackend(
state_id="parameters-prod",
host="db.internal",
database="netherframe",
username="nf_apply",
ssl_mode="verify-full",
ssl_ca="/etc/ssl/rds-ca.pem",
)
app.enable_hybrid_execution(state_backend=backend)
Supply the password via --postgresql-password-file /run/secrets/pg-pass,
[tool.nf.config].postgresql_password_file, or password_source= callable—never as a
CLI flag or hardcoded string.
PostgreSQL Document State
Stores a classic single state document inside PostgreSQL (PostgreSQLBackend):
from nf.backends.postgresql import PostgreSQLBackend
from nf.core import App, Secret
def web(pg_password: Secret[str]) -> App:
app = App(nf_id="web")
backend = PostgreSQLBackend(
state_id="web-prod",
host="db.internal",
database="netherframe",
username="nf_apply",
password=pg_password,
ssl_mode="verify-full",
)
app.enable_hybrid_execution(state_backend=backend)
return app
- Minimal PostgreSQL privileges:
USAGE, CREATEon schemanf_graph(orpublic) for initial schema migration;SELECT, INSERT, UPDATE, DELETEon backend tables for steady-state runs (plus optional read-onlyreader_rolefor auditors).