Skip to content

Tutorial 2: Inputs, stacks, and safe teardown

In this tutorial you will build a multi-stack app parameterized by typed Python inputs, connect resources across stacks, protect important data during nf destroy using LifecycleRules(retain=...), and restrict teardown-only switches with Teardown[bool].

Everything runs locally with hashicorp/random—no cloud account required.

Before you start: Complete Tutorial 1: Run your first hybrid app so you have an active environment with netherframe.cli and the hashicorp/random bindings installed.


Step 1: Write a multi-stack app with typed inputs

In your tutorial directory, create shop.py:

# coding: nf_autoid

from typing import Literal

from nf.core import App, Bind, BindContext, LifecycleRules, Output, Stack, Teardown
from nf.providers.hashicorp.random import Id, Pet, RandomProvider


def shop(
    env: Literal["dev", "prod"] = "dev",
    workers: int = 2,
    keep_orders: Teardown[bool] = False,
) -> App:
    with App(nf_id="shop") as app:
        with Stack(nf_id=f"data-{env}"), BindContext:
            random_data: Bind = RandomProvider()
            orders: Bind = Pet(
                nf_provider=random_data,
                length=3,
                lifecycle=LifecycleRules(retain="order history outlives the environment") if keep_orders else None,
            )
            store_id: Bind = Output(value=orders.output.id)

        with Stack(nf_id=f"compute-{env}"), BindContext:
            random_compute: Bind = RandomProvider()
            for index in range(workers):
                Id(
                    nf_id=f"worker-{index}",
                    nf_provider=random_compute,
                    byte_length=4,
                    keepers={"orders": orders.output.id},
                    lifecycle=LifecycleRules(retain=False),
                )

    app.enable_hybrid_execution_local(path=".nf.out/shop.tfstate")
    return app

Register shop in pyproject.toml:

[tool.nf]
apps = ["shop:shop"]

Notice three things in shop.py:

  • shop(...) is a function rather than a module-level App. Its parameters become typed CLI inputs (++env=, ++workers=, ++keep_orders).
  • The compute stack references orders.output.id from the data stack directly in Python.
  • A normal for index in range(workers): loop creates one resource construct per worker—no count or for_each meta-arguments.

Step 2: Inspect inputs with nf vars

See the inputs Netherframe discovered from the function signature and what values it would use:

nf vars --app shop
nf vars --app shop ++env=prod ++workers=3

Try passing an invalid environment value to see type checking reject it before any provider starts:

nf vars --app shop ++env=qa

Netherframe reports that qa is not one of dev, prod.


Step 3: Apply the prod environment

Plan and apply with env=prod and workers=3:

nf plan --app shop ++env=prod ++workers=3
nf apply --app shop ++env=prod ++workers=3

Approve the prompt with yes. Netherframe creates data-prod.random_pet.orders first, then creates all three compute-prod.random_id.worker-* resources using the resolved order store ID, and records the deployment inputs (env=prod, workers=3) in state.


Step 4: See destroy guard against the wrong environment

Try destroying with ++env=dev:

nf destroy --app shop ++env=dev

Netherframe refuses immediately:

Error: shop: the last apply was made with env=prod, not dev (++env)

Because env and workers are deployment inputs recorded at apply time, nf destroy rebuilds the app with the applied values automatically and refuses conflicting overrides.


Step 5: Tear down workers while retaining the order store

Now destroy the environment without passing ++env or ++workers, and pass the teardown switch ++keep_orders:

nf destroy --app shop ++keep_orders

Review the destroy preview carefully:

  • All three compute-prod.random_id.worker-* resources are marked - delete (because they explicitly opted out of cascading retention with LifecycleRules(retain=False)).
  • data-prod.random_pet.orders is listed as retained with its reason (order history outlives the environment).

Type yes to proceed. Netherframe deletes the three workers, reads orders back from the provider to verify it still exists, and leaves orders managed in state.

Confirm that only data-prod.random_pet.orders remains managed in state by re-running nf plan (it will propose re-creating the 3 deleted workers while leaving orders unchanged):

nf plan --app shop ++env=prod ++workers=3

Step 6: Finish cleanup

To destroy the retained order store as well, run nf destroy without ++keep_orders:

nf destroy --app shop

Approve with yes. State is now empty.


What you just learned

  • How an app factory's type-annotated parameters become validated CLI inputs (++name=value).
  • How stacks inside one app share references and plan/apply in a single dependency graph.
  • How recorded deployment inputs protect nf destroy from targeting the wrong environment.
  • How LifecycleRules(retain=...) and Teardown[bool] let you keep critical resources managed and verified across a teardown.

Next steps