Skip to content

Hybrid Execution Advanced Features

Beyond basic plan/apply/destroy, HybridExecutionMode exposes several higher-level provider operations.

Data Sources

Data sources in a stack (generated ...DataSource classes) work as in Terraform: plan reads each one with ReadDataSource when its configuration is known, and apply reads the others (whose configuration refers to values known only after apply, or whose depends_on names a resource not created yet) right after what they depend on; the plan summary lists those under deferred_reads. They are never created, updated or deleted. References to them resolve to what was read, and state keeps each result as <stack>.data.<type>.<name> (Terraform's mode "data") until the data source is removed or the stack destroyed.

To read data sources on their own, outside a plan, with the app's provider configurations:

from nf.core import App

app = App(nf_id="demo")


def read_ami() -> None:
    hybrid = app.enable_hybrid_execution()
    results = hybrid.read_data_sources(
        [
            {
                "data_source_type": "aws_ami",
                "data_source_id": "latest_ami",
                "config": {
                    "most_recent": True,
                    "owners": ["amazon"],
                },
            }
        ]
    )
    ami_id = results["data_sources"]["latest_ami"]["id"]

Each entry may also name a provider configuration (aws.west) or source address and the stack_id declaring it. A type name alone does not say which provider implements it (google-beta's types start with google_), so an entry without provider uses Terraform's implied provider, the configuration named after the type's first word (aws for aws_ami). When stacks configure it differently the entry fails with an error asking for its stack_id instead of reading with the first, and when the app has no such configuration the error asks for the entry's provider. The result is {"success", "data_sources", "failed"}; nothing is written to state.

For lower-level use, the provider client also exposes:

  • validate_data_resource_config(...)
  • read_data_source(...)

Importing Existing Infrastructure

import_resources adopts objects that already exist, as terraform import does. Each target is the state address (<stack>.<type>.<name>) of a managed resource the app declares, so the next plan manages the object instead of creating one:

from nf.core import App

app = App(nf_id="demo")


def import_existing() -> None:
    hybrid = app.enable_hybrid_execution()
    result = hybrid.import_resources(
        [
            {"address": "network.aws_vpc.main", "import_id": "vpc-0a1b2c3d"},
            # or by the provider's resource identity
            {"address": "storage.aws_s3_bucket.logs", "identity": {"bucket": "my-logs"}},
        ],
        auto_approve=True,
    )

Each object is imported with its resource's provider configuration (ImportResourceState), read (ReadResource, with the private data and identity the import returned) and stored at its address with what the read returned. As in Terraform, an ID the read finds nothing for is not imported, and a target already in state is refused. The result is {"success", "imported", "failed"}; imports that succeed are kept when others fail. The next plan shows how the object differs from its configuration.

Moving Resources In State

move_resources renames state addresses, as terraform state mv and moved blocks do, for example after renaming a resource's nf_id. nf state mv SOURCE TARGET is the same, for one move:

from nf.core import App

app = App(nf_id="demo")


def rename_instance() -> None:
    hybrid = app.enable_hybrid_execution()
    hybrid.move_resources(
        [{"from": "network.aws_instance.web", "to": "network.aws_instance.frontend"}],
        auto_approve=True,
    )

Moves stay within a stack and apply in order. A move to another resource type asks the target's provider (the target must be declared in the app) to convert the stored state with MoveResourceState. The entries that depend on a moved one are rewritten to its new address. State changes only when every move succeeds, in one write: a graph state commits it as one operation, any other backend in one conditional write (StateEngine.move_entries, which every backend's contract tests cover).

forget_resources (nf state rm) removes resources and outputs from state without deleting the objects, in one write as well. It refuses to leave an entry depending on a removed one: name those too, or apply a configuration without the dependency first.

Provider Functions

from nf.core import App

app = App(nf_id="demo")


def parse_arn() -> None:
    hybrid = app.enable_hybrid_execution()
    functions = hybrid.get_provider_metadata("hashicorp/aws").get("functions", {})
    value = hybrid.call_provider_function(
        provider="hashicorp/aws",
        function_name="arn_parse",
        arguments=["arn:aws:s3:::example"],
    )

Provider Metadata And Config Validation

from nf.core import App

app = App(nf_id="demo")


def check_provider() -> None:
    hybrid = app.enable_hybrid_execution()
    metadata = hybrid.get_provider_metadata("hashicorp/aws")
    errors = hybrid.validate_provider_config("hashicorp/aws", {"region": "us-east-1"})

Resource Listing

from nf.core import App

app = App(nf_id="demo")


def list_instances() -> None:
    hybrid = app.enable_hybrid_execution()
    resources = hybrid.list_resources("aws_instance")
    print(resources["total_count"])

    # Name the provider configuration (or source) and the stack declaring it
    beta = hybrid.list_resources("google_compute_instance", provider="google-beta")
    west = hybrid.list_resources("aws_instance", provider="aws.west", stack_id="network")

A type name alone does not say which provider implements it (google-beta's types start with google_), so without provider the listing uses Terraform's implied provider: the app's default configuration named after the type's first word (aws for aws_instance), the one stack_id declares when given. When stacks configure that provider differently, or the app declares it only under aliases (aws.west), the call raises a ProviderError asking for the provider or stack instead of picking one. A provider the app does not declare runs with an empty configuration from the source its installed generated package declares (keycloak/keycloak), else hashicorp/<name>; two installed packages declaring the name with different sources are an error.

Ephemeral Resources

Ephemeral resources in a stack (generated ...EphemeralResource classes) work as in Terraform: hybrid execution opens them while it plans and again while it applies, renews them when the provider asks, and closes them at the end. They are never planned, stored in state or kept in a saved plan. Their values can go to other ephemeral resources and to write-only arguments of managed resources (for example password_wo); any other argument is refused, because the provider would store the value in state.

Each is opened at its place in the plan's or apply's walk of the app's graph, once what it depends on (its references and depends_on) is planned or applied, so it may use a data source's result or an attribute of a resource created earlier in the same apply. As Terraform does, a plan leaves one closed while its configuration holds a value known only after apply, or while a resource its depends_on names has a planned change: what refers to it plans its values as unknown, and apply opens it once those exist. Applying one stack opens the other stacks' ephemeral resources it refers to. A provider configured from an ephemeral value is refused; use Terraform synthesis for that.

List resources belong to terraform query, so plan and apply skip them (use list_resources below to list existing resources). Stacks with a state store or an action are refused before anything starts: hybrid execution does not read or write state through provider state stores, nor plan or invoke actions, yet.

An ephemeral resource only lives within one plan or apply, which closes it at the end, so there is no API to open one on its own.

At the provider-client level there are also methods for:

  • validate_ephemeral_resource_config(...)
  • open_ephemeral_resource(...)
  • renew_ephemeral_resource(...)
  • close_ephemeral_resource(...)

State Upgrade And Identity Helpers

Provider clients expose schema-upgrade helpers for advanced tooling:

  • upgrade_resource_state(...)
  • get_resource_identity_schemas()
  • upgrade_resource_identity(...)

These are primarily useful for migration, inspection, and compatibility tooling rather than day-to-day app code.