Promote an app across environments
Use the same Python app in development and production with separate state and separate reviewed plans. This walkthrough uses local Random resources so you can rehearse the procedure without cloud credentials.
Before you start: complete the first hybrid app. Keep its Random bindings installed and start this walkthrough in a separate project directory or replace that demo's app after cleaning it up.
1. Make environment selection explicit
Create app.py:
import os
from nf.core import App, Output, Stack
from nf.providers.hashicorp.random import Pet, RandomProvider
environment = os.getenv("ENVIRONMENT", "dev")
if environment not in {"dev", "prod"}:
raise ValueError("ENVIRONMENT must be dev or prod")
app = App(nf_id=f"platform-{environment}", context={"environment": environment})
stack = Stack(nf_scope=app, nf_id="names")
provider = RandomProvider(nf_scope=stack, nf_id="random")
pet = Pet(
nf_scope=stack,
nf_id="example",
nf_provider=provider,
length=2,
prefix=environment,
)
Output(nf_scope=stack, nf_id="pet_name", value=pet.ref_id_)
app.enable_hybrid_execution_local(path=f".nf.out/{environment}.tfstate")
Update the existing pyproject.toml:
[tool.nf]
apps = ["app:app"]
The selector stays app. The resulting app ID, name prefix and state path vary by
environment.
2. Review and apply development
ENVIRONMENT=dev nf plan --app app --out .nf.out/dev-plan.json
ENVIRONMENT=dev nf apply --app app --plan-file .nf.out/dev-plan.json
ENVIRONMENT=dev nf plan --app app
Approve the reviewed change. The result has a dev- prefix and is recorded in
.nf.out/dev.tfstate. The next plan should have no resource changes.
3. Review production independently
ENVIRONMENT=prod nf plan --app app --out .nf.out/prod-plan.json
ENVIRONMENT=prod nf apply --app app --plan-file .nf.out/prod-plan.json
ENVIRONMENT=prod nf plan --app app
Production gets its own random resource and .nf.out/prod.tfstate. Its name starts with
prod-. Review the production plan rather than applying the development plan to another
backend.
4. Clean up the rehearsal
ENVIRONMENT=dev nf destroy --app app
ENVIRONMENT=prod nf destroy --app app
Review both destroy prompts. These resources store names locally and do not represent cloud objects.
Next: multi-environment workflows, CI/CD and remote state.
Understand promotion
Promote code, then plan for the destination
The same reviewed code revision can be used in each environment, but inputs, credentials, provider observations and state can differ. Generate a plan in the destination environment and retain that plan with its approval. Do not copy a development state file into production to create the appearance of an identical deployment.
Context is data; a backend is an execution choice
context={"environment": environment} makes the label available to app code. It does not
configure state storage or choose cloud credentials. This example configures the local
backend explicitly with enable_hybrid_execution_local and an environment-specific path.
For a shared deployment, configure distinct remote state locations and credential scopes. Use the same environment inputs when planning and applying, including in CI. The backend guide covers that transition.
App scope and stack structure solve different problems
Stacks organize resources. This hybrid example plans the app as a whole, even if more stacks are added. If you need separately approved execution units, choose app/backend boundaries accordingly. See workflow selection.
Failures remain local to their state boundary
Separate backends let you inspect and recover one environment independently. They do not reverse a failed apply's cloud mutations. Review recovery strategies and retain the run's artifacts before moving this pattern to a cloud deployment.