Skip to content

Providers and runtime values

Two ideas explain almost all of Netherframe's authoring behavior:

  1. Provider configurations define where and how resources execute.
  2. Runtime values represent infrastructure outputs that do not exist yet when Python builds the construct tree.

Providers

A provider connects resources in your Python code to a Terraform provider binary (and its target cloud or service API). Typically a provider carries operational identity—region, account, endpoint, credentials profile, or alias.

  • Use one provider per stack when every resource in that stack belongs to the same region and account.
  • Use multiple aliased providers (with with provider: contexts or nf_provider=...) when a stack coordinates across regions or accounts, such as cross-region replication or DNS delegation.
  • Prefer generated typed provider packages (nf.providers.hashicorp.aws) for day-to-day authoring; use raw Provider(nf_type=...) and Resource(nf_type=...) when working with an ungenerated provider.

In hybrid execution, a provider's own configuration can even reference outputs of resources created in the same apply (for example, configuring a Kubernetes provider from a brand-new EKS cluster). See Deferred provider configuration.


Runtime values

When your Python app function runs, cloud resources have not been created yet. Accessing an output attribute such as bucket.output.id or vpc.output.arn does not return a plain string—it returns a RuntimeValue.

from nf.core import App, Output, Stack
from nf.providers.hashicorp.aws import AwsProvider, S3Bucket, S3BucketVersioning

app = App(nf_id="platform")
stack = Stack(nf_scope=app, nf_id="storage")

AwsProvider(nf_scope=stack, nf_id="aws", region="us-east-1")

bucket = S3Bucket(
    nf_scope=stack,
    nf_id="assets",
    bucket="replace-with-a-globally-unique-name",
)

S3BucketVersioning(
    nf_scope=stack,
    nf_id="assets_versioning",
    bucket=bucket.output.id,
    versioning_configuration={"status": "Enabled"},
)

Output(nf_scope=stack, nf_id="bucket_id", value=bucket.output.id)

Keeping bucket.output.id as a RuntimeValue lets Netherframe:

  • Record the dependency edge (assets_versioning depends on assets) automatically.
  • Send UNKNOWN to providers during planning when assets does not exist yet.
  • Resolve the real value during hybrid apply once assets is created (or emit ${aws_s3_bucket.assets.id} during Terraform synthesis).

What belongs in Python vs. in runtime values

A clean mental boundary prevents most authoring surprises:

Keep in ordinary Python (known before planning) Keep as runtime values (resolved by the engine)
Typed app inputs (env, region, replicas) Resource IDs, ARNs, endpoints, generated names
Which stacks or resources to instantiate (if, for) Cross-resource and cross-stack output references
Backend choice and recovery strategy Data source and ephemeral resource attributes
Reusable construct composition Values transformed by @hybrid_runtime during apply

Runtime values in f-strings and JSON

Formatting a RuntimeValue in an f-string preserves its token and dependency edge:

from nf.core import App, Resource, Stack

app = App(nf_id="platform")
stack = Stack(nf_scope=app, nf_id="network")
vpc = Resource(nf_scope=stack, nf_id="main", nf_type="aws_vpc", cidr_block="10.0.0.0/16")
Resource(
    nf_scope=stack,
    nf_id="a",
    nf_type="aws_subnet",
    tags={"Name": f"subnet-{vpc.output.id}"},
)
  • Format specs are preserved too: f"{port:05d}" becomes ${format("%05d", ...)}.
  • Inside JSON policy documents, use json_nf_dumps from nf.core.serialization: json_nf_dumps({"Resource": bucket.output.arn + "/*"}) preserves the dependency and evaluates cleanly in both hybrid execution and synthesis.
  • Calling str(vpc.output.id) explicitly extracts the raw interpolation string (${aws_vpc.main.id}) without the runtime token wrapper—avoid doing this unless you specifically want literal text.

Expressions on runtime values

Standard Python operators on runtime values build expression ASTs evaluated identically in hybrid execution and Terraform synthesis:

Python expression Equivalent Terraform expression
port + 1, (port + 1) * 2, -port ${...port + 1}, ${(...port + 1) * 2}, ${-...port}
port // 2, port ** 2, abs(port) ${floor(... / 2)}, ${pow(..., 2)}, ${abs(...)}
port > 80, port.eq(80), name.ne("x") ${... > 80}, ${... == 80}, ${... != "x"}
(port > 80) & enabled, a \| b, ~enabled ${... > 80 && ...}, ${... \|\| ...}, ${!...}
"arn:" + bucket.output.arn + "/*" arn:${aws_s3_bucket.logs.arn}/*
ids[0], ids[-1], ids[1:], tags["Name"] ${...ids[0]}, ${...ids[length(...ids) - 1]}, ${slice(...ids, 1, length(...ids))}, ${...tags["Name"]}
ids + ["x"], tags \| {"a": "b"}, runtime_len(ids) ${concat(...)}, ${merge(...)}, ${length(...)}
name.call_method("upper") (lower, strip, replace, split, startswith, endswith, removeprefix, removesuffix) ${upper(...)} (lower, trimspace, replace, split, startswith, endswith, trimprefix, trimsuffix)
tags.get("k", "d"), ConstantValue(",").call_method("join", ids) ${lookup(..., "k", "d")}, ${join(",", ...)}

Why truthiness and iteration fail fast

Python operations that require an immediate answer while building the construct tree—such as if port > 80:, bool(expr), in, or for x in ids:—raise TypeError immediately:

  • Expression.__bool__ raises TypeError so an unknown runtime condition never silently evaluates to True in a Python if statement.
  • Use .eq() and .ne() for value equality (== and != compare Python AST objects), runtime_contains() for membership tests, and @hybrid_runtime functions when you need arbitrary Python logic to run during hybrid apply once values are resolved.