Managing Multiple Environments
The recommended Netherframe multi-environment pattern uses one Python app factory
parameterized by typed inputs (++env=prod, ++@envs/prod.toml), with separate state
per environment and explicit environment-aware guardrails—zero copy-pasted directories
and zero NF_* environment variables.
For a guided promotion exercise, see Tutorial: Promote across environments and Tutorial 2: Inputs, stacks, and safe teardown.
Architecture of a multi-environment app
flowchart LR
A["pyproject.toml ([tool.nf].apps)"] --> B["App factory def web(settings: EnvConfig)"]
C["++@envs/prod.toml + ++replicas=5"] --> B
B --> D["Per-env Stack & Resources"]
B --> E["Per-env State Backend (web/prod.tfstate)"]
B --> F["Env-aware @app.check guardrails"]
Keep these boundaries clean:
- One state per environment: Never point
devandprodat the same state file or S3 key. If you switch++env=devagainst a state holdingweb-prod, a whole-app plan will treatweb-prodas an undeclared stack and plan its deletion. - Typed inputs instead of env vars: Netherframe reads no
NF_*environment variables. Every environment parameter is declared on your factory function and checked before any provider runs. - Recorded deployment inputs: Applying with
++env=prodrecordsenv=prodin state, so a laternf destroyautomatically rebuilds withenv=prodand refuses++env=dev.
Concrete pattern: Dataclass config + TOML files + guardrails
Use a dataclass input group so each environment's settings live cleanly in a TOML file
under envs/ while remaining overridable on the command line:
# coding: nf_autoid
from dataclasses import dataclass
from typing import Literal
from nf.core import App, AppCheckContext, Bind, BindContext, LifecycleRules, Output, Stack, Teardown
from nf.providers.hashicorp.aws import AwsProvider, DynamodbTable, SqsQueue
@dataclass
class EnvConfig:
env: Literal["dev", "staging", "prod"] = "dev"
region: str = "us-east-1"
workers: int = 2
keep_data: Teardown[bool] = False
change_ticket: Teardown[str] = ""
def prod_requires_ticket(ctx: AppCheckContext) -> str | None:
if ctx.inputs["env"] == "prod" and not ctx.inputs["change_ticket"]:
return "prod runs require ++change_ticket=CHG-1234"
return None
def prod_no_accidental_deletes(ctx: AppCheckContext) -> str | None:
if ctx.inputs["env"] != "prod":
return None
doomed = [c.address for c in ctx.changes_of(actions=("delete", "replace"))]
return f"prod blocks routine deletes/replaces: {', '.join(doomed)}" if doomed else None
def orders(cfg: EnvConfig) -> App:
is_prod = cfg.env == "prod"
with App(nf_id="orders", recovery_strategy="ROLLBACK") as app, Stack(nf_id=f"orders-{cfg.env}"), BindContext:
aws: Bind = AwsProvider(
region=cfg.region,
default_tags=[{"tags": {"App": "orders", "Env": cfg.env}}],
)
table: Bind = DynamodbTable(
nf_provider=aws,
name=f"orders-{cfg.env}",
billing_mode="PAY_PER_REQUEST",
hash_key="order_id",
attribute=[{"name": "order_id", "type": "S"}],
point_in_time_recovery=[{"enabled": is_prod}],
lifecycle=LifecycleRules(
prevent_destroy=is_prod and not cfg.keep_data,
retain="order history outlives teardown" if cfg.keep_data else None,
),
)
for index in range(cfg.workers):
SqsQueue(
nf_id=f"worker-{index}",
nf_provider=aws,
name=f"orders-{cfg.env}-worker-{index}",
lifecycle=LifecycleRules(retain=False),
)
table_name: Bind = Output(value=table.output.name)
app.check(prod_requires_ticket, phase="prerequisites")
app.check(prod_no_accidental_deletes, phase="plan")
# Distinct state key per environment
app.enable_hybrid_execution_local(path=f".nf.out/orders-{cfg.env}.tfstate")
return app
Store per-environment defaults in envs/dev.toml, envs/staging.toml, and envs/prod.toml:
# envs/prod.toml
env = "prod"
region = "eu-west-1"
workers = 6
Register apps = ["infra.orders:orders"] in pyproject.toml, then inspect and promote:
# Inspect resolved inputs before running anything
nf vars --app orders ++@envs/prod.toml
# 1. Deploy to dev
nf plan --app orders ++@envs/dev.toml --out dev.plan.json
nf apply --app orders --plan-file dev.plan.json
# 2. Promote the exact same Git commit to staging, then prod
nf plan --app orders ++@envs/staging.toml --out staging.plan.json
nf apply --app orders --plan-file staging.plan.json
nf plan --app orders ++@envs/prod.toml ++change_ticket=CHG-901 --out prod.plan.json
nf apply --app orders --plan-file prod.plan.json
Choosing state isolation across environments
| Scale | Recommended state setup |
|---|---|
| Local experiments | app.enable_hybrid_execution_local(path=f".nf.out/{app_name}-{env}.tfstate") |
| Shared AWS environments | app.enable_hybrid_execution_s3(bucket=f"acme-state-{env}", key=f"{app_name}/nf.tfstate", region=region) (separate account/bucket for prod) |
| Shared PostgreSQL graph state | Distinct state_id=f"{app_name}-{env}" (or separate PostgreSQL database per environment) |
Operational guidelines
- Pin identical provider versions across all environments so
devandstagingtest the exact provider binaryprodwill run. - Keep environment differences small and declarative: size, replica count, retention, and region should vary; architecture and stack names should stay consistent.
- Use
Teardown[T]for run-only switches (change_ticket,keep_data) so they are never baked into deployment state and are guaranteed not to alter deployed resources.