Skip to content

Provider Patterns in Netherframe

This guide covers the provider-association patterns that are supported today in Netherframe.

The practical rule is simple:

  • use with provider: when a whole block should use the same provider
  • use nf_provider=... when a single resource needs to override the surrounding provider context
  • use provider.ResourceName(...) when you want a terse, one-off provider-bound factory with autocomplete support

Provider contexts are the clearest default for multi-region or multi-account stacks because the scope of each provider is visible in the source.

from nf.core import App, Output, ProviderSettings, Stack
from nf.providers.hashicorp.aws import AwsProvider, S3Bucket, Subnet, Vpc


app = App()
stack = Stack(nf_scope=app, nf_id="multi-region")

us_east = AwsProvider(
    nf_scope=stack,
    nf_id="aws_us_east_1",
    region="us-east-1",
)
us_west = AwsProvider(
    nf_scope=stack,
    nf_id="aws_us_west_2",
    region="us-west-2",
    nf_provider_settings=ProviderSettings(alias="west"),
)

with us_east:
    primary_vpc = Vpc(
        nf_scope=stack,
        nf_id="primary_vpc",
        cidr_block="10.0.0.0/16",
        tags={"Name": "provider-patterns-primary"},
    )
    Subnet(
        nf_scope=stack,
        nf_id="primary_public_subnet",
        vpc_id=primary_vpc.output.id,
        cidr_block="10.0.1.0/24",
        availability_zone="us-east-1a",
        tags={"Name": "provider-patterns-primary-public"},
    )

with us_west:
    backup_vpc = Vpc(
        nf_scope=stack,
        nf_id="backup_vpc",
        cidr_block="10.1.0.0/16",
        tags={"Name": "provider-patterns-backup"},
    )
    S3Bucket(
        nf_scope=stack,
        nf_id="backup_bucket",
        bucket="provider-patterns-backup-example",
        tags={"Name": "provider-patterns-backup-bucket"},
    )

Output(nf_scope=stack, nf_id="primary_vpc_id", value=primary_vpc.output.id)
Output(nf_scope=stack, nf_id="backup_vpc_id", value=backup_vpc.output.id)

Use this pattern when:

  • one region or account owns a coherent block of resources
  • you want provider scoping to be obvious in code review
  • you want a safe default for larger stacks

Nested Contexts

Nested provider contexts are valid when you want to temporarily switch away from the main provider and then return to it.

from nf.core import App, Output, ProviderSettings, Stack
from nf.providers.hashicorp.aws import AwsProvider, S3Bucket, Subnet, Vpc


app = App()
stack = Stack(nf_scope=app, nf_id="nested-provider-contexts")

primary = AwsProvider(
    nf_scope=stack,
    nf_id="aws_primary",
    region="us-east-1",
)
recovery = AwsProvider(
    nf_scope=stack,
    nf_id="aws_recovery",
    region="us-west-2",
    nf_provider_settings=ProviderSettings(alias="recovery"),
)

with primary:
    main_vpc = Vpc(
        nf_scope=stack,
        nf_id="main_vpc",
        cidr_block="10.20.0.0/16",
    )

    with recovery:
        backup_bucket = S3Bucket(
            nf_scope=stack,
            nf_id="cross_region_backup",
            bucket="provider-patterns-cross-region-backup",
        )

    Subnet(
        nf_scope=stack,
        nf_id="app_subnet",
        vpc_id=main_vpc.output.id,
        cidr_block="10.20.1.0/24",
        availability_zone="us-east-1a",
    )

Output(nf_scope=stack, nf_id="backup_bucket_id", value=backup_bucket.output.id)

The important detail is that the inner with recovery: block only changes the provider for that inner region of code. When the inner block exits, the outer provider context is active again.

Single-Provider Stacks

For stacks that only use one provider, a context still gives you a clean block without repeating nf_provider=... on every resource.

It is also not strictly necessary.

If a stack registers exactly one unaliased provider of a given type, resources of that type can omit both:

  • with provider:
  • nf_provider=...

In that case, Netherframe leaves the resource's explicit provider reference unset, so no provider = ... field is emitted for that resource. The generated Terraform/OpenTofu config still contains the single provider block, and that unaliased provider becomes the default for resources of that type.

That means this works for a simple stack:

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


app = App()
stack = Stack(nf_scope=app, nf_id="single-provider-implicit-default")

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

vpc = Vpc(
    nf_scope=stack,
    nf_id="main_vpc",
    cidr_block="10.42.0.0/16",
)
public_subnet = Subnet(
    nf_scope=stack,
    nf_id="public_subnet",
    vpc_id=vpc.output.id,
    cidr_block="10.42.1.0/24",
    availability_zone="us-east-1a",
)
logs_bucket = S3Bucket(
    nf_scope=stack,
    nf_id="logs_bucket",
    bucket="provider-patterns-logs-example",
)

Output(nf_scope=stack, nf_id="public_subnet_id", value=public_subnet.output.id)
Output(nf_scope=stack, nf_id="logs_bucket_id", value=logs_bucket.output.id)

Use an explicit provider context anyway when you want the provider choice to be obvious in source, or when the stack may later grow into a multi-provider case.

from nf.core import App, Output, ProviderSettings, Stack
from nf.providers.hashicorp.aws import AwsProvider, S3Bucket, Subnet, Vpc


app = App()
stack = Stack(nf_scope=app, nf_id="single-provider")

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

with provider:
    vpc = Vpc(
        nf_scope=stack,
        nf_id="main_vpc",
        cidr_block="10.42.0.0/16",
    )
    public_subnet = Subnet(
        nf_scope=stack,
        nf_id="public_subnet",
        vpc_id=vpc.output.id,
        cidr_block="10.42.1.0/24",
        availability_zone="us-east-1a",
    )
    logs_bucket = S3Bucket(
        nf_scope=stack,
        nf_id="logs_bucket",
        bucket="provider-patterns-logs-example",
    )

Output(nf_scope=stack, nf_id="public_subnet_id", value=public_subnet.output.id)
Output(nf_scope=stack, nf_id="logs_bucket_id", value=logs_bucket.output.id)

This implicit-default pattern only applies when there is a single unaliased provider for that resource type. If you register multiple providers of the same type, or if you need an aliased provider, use with provider: or nf_provider=... so the target provider is explicit.

For hybrid execution, Netherframe now follows a similarly explicit rule:

  • if a resource has an explicit provider, hybrid uses it
  • otherwise, if the stack has exactly one registered provider, hybrid uses that provider
  • otherwise, a generated resource class uses the stack's unaliased configuration of the provider it belongs to (so google-beta's google_* types use google-beta)
  • otherwise, hybrid raises instead of guessing from resource.nf_type

That makes provider attribution deterministic in mixed-provider stacks.

Hybrid execution starts one provider process per provider configuration, as Terraform does: the default configuration and each alias (and each stack's own configurations) are configured with their own settings, so a resource bound to aws.west is planned, applied, refreshed and destroyed in the west region. The process runs the provider's declared source and version (_PROVIDER_SOURCE and _PROVIDER_VERSION of the generated class, for example keycloak/keycloak), not hashicorp/<name>. State records each resource's configuration name (aws.west) and, in its provider metadata, the source, version, alias and Terraform address (provider["registry.terraform.io/hashicorp/aws"].west), so destroying resources whose provider the app no longer declares starts the same provider, with an empty configuration. An aliased configuration cannot be inferred that way and must stay declared until its resources are destroyed.

Outside hybrid planning (the orchestrator running a node that carries no plan metadata, desired state built from a stack), a resource's provider is found the same way instead of from its type's first word: its explicit provider (with its alias), else the provider its generated class belongs to (_PROVIDER_TYPE, with the source of the generated package it comes from), and only then Terraform's implied provider, the type's first word. That local name's source comes from the stack's provider classes (its required_providers), and is hashicorp/<name> only when the stack does not declare it, as in Terraform. A client is then looked up by configuration name or by source and alias; a short name never matches a different provider's source.

A provider named by a bare name (ProviderConfig(name="github")) is installed and found in the provider caches as the source that name resolves to: its required_providers entry when the configuration is known, else the installed generated provider package that declares that local name (github is integrations/github when that package is installed), else Terraform's hashicorp/<name>. Two installed packages declaring the name with different sources are an error that asks for the source address; the caches are never searched for another namespace's binary of the same name.

Explicit Provider Override

nf_provider=... is still the right tool when most of a block should use one provider but a single resource should go elsewhere.

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


app = App()
stack = Stack(nf_scope=app, nf_id="explicit-provider-override")

primary = AwsProvider(
    nf_scope=stack,
    nf_id="aws_primary",
    region="us-east-1",
)
backup = AwsProvider(
    nf_scope=stack,
    nf_id="aws_backup",
    region="us-west-2",
    nf_provider_settings=ProviderSettings(alias="backup"),
)

with primary:
    app_vpc = Vpc(
        nf_scope=stack,
        nf_id="app_vpc",
        cidr_block="10.60.0.0/16",
    )
    cross_region_backup = S3Bucket(
        nf_scope=stack,
        nf_id="cross_region_backup",
        bucket="provider-patterns-explicit-override",
        nf_provider=backup,
    )

Output(nf_scope=stack, nf_id="app_vpc_id", value=app_vpc.output.id)
Output(
    nf_scope=stack,
    nf_id="cross_region_backup_id",
    value=cross_region_backup.output.id,
)

Use explicit overrides sparingly. They are useful, but a large stack with many scattered overrides is harder to read than one organized into provider blocks.

Provider-Bound Factories

Generated providers support provider-bound factories directly:

  • provider.ResourceName(...)

That form injects nf_provider=provider before the resource is constructed, so it composes cleanly with helpers such as .import_from(...) and works well for one-off overrides.

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


app = App()
stack = Stack(nf_scope=app, nf_id="provider-bound-factory")

primary = AwsProvider(
    nf_scope=stack,
    nf_id="aws_primary",
    region="us-east-1",
)
backup = AwsProvider(
    nf_scope=stack,
    nf_id="aws_backup",
    region="us-west-2",
    nf_provider_settings=ProviderSettings(alias="backup"),
)

app_vpc = primary.Vpc(
    nf_scope=stack,
    nf_id="app_vpc",
    cidr_block="10.80.0.0/16",
)
backup_bucket = backup.S3Bucket(
    nf_scope=stack,
    nf_id="backup_bucket",
    bucket="provider-patterns-bound-factory",
)

Output(nf_scope=stack, nf_id="app_vpc_id", value=app_vpc.output.id)
Output(nf_scope=stack, nf_id="backup_bucket_id", value=backup_bucket.output.id)

Use this pattern when:

  • you want aws_east.Vpc(...) style autocomplete in editors
  • you want a terse one-off resource binding without opening a with block
  • you want bound resource creation to compose naturally with method chaining

The supported APIs today are:

  • provider contexts with with provider:
  • explicit nf_provider=... on a resource
  • provider-bound factories with provider.ResourceName(...)

Auto-Scope and Auto-ID

Once the explicit model is clear, you can reduce boilerplate with nf_autoid, Bind, BindContext, and @autoscope.

#!/usr/bin/env python3
# coding: nf_autoid

from nf.core import App, Bind, BindContext, Construct, Stack, autoscope
from nf.providers.hashicorp.aws import AwsProvider, S3Bucket, Vpc


@autoscope
class Network(Construct):
    def __init__(self, *, nf_scope: Construct | None = None, nf_id: str | None = None, cidr: str) -> None:
        super().__init__(nf_scope=nf_scope, nf_id=nf_id)
        self.vpc: Bind = Vpc(cidr_block=cidr)
        self.logs_bucket: Bind = S3Bucket(
            bucket="provider-patterns-autoscope-example",
        )


with App() as app, BindContext, Stack() as stack, BindContext:
    with AwsProvider(region="us-east-1") as aws, BindContext:
        network: Bind = Network(cidr="10.70.0.0/16")

That layer is optional. Learn the explicit provider model first, then use the ergonomic layer when you want less repeated nf_scope= and nf_id= noise.

Best Practices

  • Prefer provider contexts for groups of related resources.
  • Use explicit nf_provider=... only for targeted exceptions.
  • Use provider.ResourceName(...) for terse one-off bindings, not for large mixed-provider blocks.
  • Name providers by role or region, such as primary, backup, us_east, or prod_aws.
  • Keep stack boundaries clear; provider patterns are easier to follow when the deployment unit is already well defined.