Skip to content

Refactoring Live Infrastructure Without Downtime

Every Netherframe resource is tracked in state by its address: <stack_id>.<resource_type>.<nf_id>. Refactoring Python code without updating state can accidentally plan a - delete and + create (recreating live databases, buckets, or VPCs).

This cookbook gives safe, zero-downtime recipes for the six most common live refactors. Always verify every refactor with nf plan --app <app> --diff until it reports zero changes before applying anything else.


Recipe 1: Rename a resource's nf_id (or Python Bind variable)

Suppose you rename old_vpc: Bind = Vpc(...) to main: Bind = Vpc(...) inside stack network:

  1. Update the Python code (old_vpc → main).
  2. Move the state entry to the new address:
    nf state mv --app platform network.aws_vpc.old_vpc network.aws_vpc.main
    
    (Netherframe rewrites dependency references in other state entries automatically in a single conditional write.)
  3. Confirm zero changes:
    nf plan --app platform
    

Recipe 2: Move a resource to another stack in the same App

Moving S3Bucket(nf_id="logs") from Stack(nf_id="core") into Stack(nf_id="audit"):

  1. Move the Python declaration into the audit stack.
  2. Move the state entry across stacks:
    nf state mv --app platform core.aws_s3_bucket.logs audit.aws_s3_bucket.logs
    
  3. Verify zero changes:
    nf plan --app platform
    

Recipe 3: Refactor inline resources into a reusable Stack or Construct class

Because Netherframe addresses are <stack_id>.<resource_type>.<nf_id> (intermediate Construct wrapper classes do not alter resource addresses unless you change nf_id), extracting inline code into a helper function or Construct subclass produces zero plan diff as long as you keep the same stack ID and resource nf_id values:

# coding: nf_autoid

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


class LoggingBucket(Construct):
    def __init__(self, *, nf_scope: Construct | None = None, nf_id: str | None = None, bucket_name: str) -> None:
        super().__init__(nf_scope=nf_scope, nf_id=nf_id)
        # Explicit nf_id="logs" preserves the exact state address storage.aws_s3_bucket.logs
        self.bucket = S3Bucket(nf_id="logs", bucket=bucket_name)


with App(nf_id="platform") as app, Stack() as storage, BindContext:
    aws: Bind = AwsProvider(region="us-east-1")
    logging: Bind = LoggingBucket(bucket_name="acme-logs")

Run nf plan --app platform to verify 0 to create, 0 to destroy.


Recipe 4: Split one large App into two independent Apps

To move a storage stack out of monolith (state A) into its own storage_app (state B) without recreating the live resources:

  1. Record live IDs of the resources you are moving (from state A, outputs, or cloud console).
  2. Create storage_app with its own state backend, declaring those resources with .import_from(...), LifecycleRules(adopt="required"), or an --adopt-from manifest.
  3. Adopt into storage_app first (state B):
    nf plan --app storage_app --diff --adopt-missing error --out adopt.plan.json
    nf apply --app storage_app --plan-file adopt.plan.json
    nf plan --app storage_app      # verify 0 changes
    
  4. Detach from monolith (state A) without deleting cloud objects:
    nf state rm --app monolith storage.aws_s3_bucket.assets
    
  5. Remove the storage stack from monolith's Python code and confirm both apps plan cleanly with zero changes:
    nf plan --app monolith
    nf plan --app storage_app
    

Tip: Always adopt into the new app before removing from the old app so there is never a window where the resource is unmanaged.


Recipe 5: Detaching (nf state rm) vs. Retaining (LifecycleRules(retain=...))

Goal Use Result
Hand a resource off to another app/team permanently nf state rm --app <app> <address> + remove from Python Cloud object stays live; Netherframe stops managing it.
Keep a resource alive across nf destroy while continuing to manage and verify it LifecycleRules(retain="reason") + @retained_check Cloud object stays live and stays in state; post-destroy checks verify its health.

Recipe 6: Upgrading provider versions safely

Provider upgrades can change schema defaults or mark fields as ForceNew:

  1. Upgrade on a branch and regenerate bindings with --clean (failed regenerations keep your previous package intact).
  2. Back up remote state or verify checkpoints exist.
  3. Run nf plan --app <app> --diff with zero Python edits so provider-schema diffs are reviewed separately from application changes.
  4. See Upgrading Netherframe and Providers.