Netherframe Phases System
The phases system provides a clean, unified API for handling circular dependencies in infrastructure deployments through phased execution.
Overview
When infrastructure resources have circular dependencies, they cannot be deployed in a single pass. The phases system automatically:
- Detects circular dependencies across your infrastructure
- Plans an optimal phased deployment strategy
- Synthesizes phased export artifacts for Terraform/OpenTofu handoff or inspection
Architecture Diagram
flowchart LR
A[App graph] --> B[Dependency analyzer]
B --> C[Cycle detection and SCC analysis]
C --> D[Breaking edges and levels]
D --> E[Phase planner]
E --> F[Phase plan]
F --> G[Phased synthesis output]
G --> H[Phase 1 export/apply]
H --> I[Phase 2 export/apply]
I --> J[Later phases as needed]
Quick Start
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases.planner import PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
# Analyze your app and create a phase plan
planner = PhasePlanner()
plan = planner.create_plan(app, strategy="auto")
print(f"Deployment will use {plan.total_phases} phase(s)")
Core Concepts
Phase Plan
A phase plan breaks your infrastructure into multiple deployment phases. Resources in each phase can be deployed together, with later phases depending on earlier ones.
Circular Dependencies
Circular dependencies occur when resources reference each other in a cycle. For example: - Security Group A references Security Group B - Security Group B references Security Group A
Breaking Cycles
Each cycle (a strongly connected component of the graph) is broken at real edges whose removal leaves it acyclic: references left out until the construct they refer to exists, set by a later phase from the values an earlier phase recorded in the stack's state. A reference in a required argument is never left out, and a cycle that can only be broken at one fails synthesis with its path. See Circular Dependency Resolution.
Strategies
auto(the default) andminimal: as few phases as the cycles need, two for a cycle within a stack.level_based: a phase per dependency level within the phased stacks.
Phases are cumulative (each holds every construct of the phases before it) and levels follow dependencies: a construct is never in a phase before what it depends on.
API Reference
PhasePlanner.create_plan(app, strategy="auto")
Analyzes an application and creates a phase execution plan.
Parameters:
- app: The Netherframe application to analyze
- strategy: Planning strategy ("auto", "minimal", "level_based")
Returns: PhasePlan object
Example:
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases.planner import PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
planner = PhasePlanner()
# As few phases as the cycles need
plan = planner.create_plan(app)
# A phase per dependency level
plan = planner.create_plan(app, strategy="level_based")
Data Models
PhasePlan
Represents a complete execution plan:
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases.planner import PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
planner = PhasePlanner()
plan = planner.create_plan(app)
# Access plan details
print(f"Total phases: {plan.total_phases}")
print(f"Affected stacks: {plan.affected_stacks}")
# Check for circular dependencies
if plan.circular_dependencies:
for cd in plan.circular_dependencies:
print(f"Circular dependency: {cd.get_description()}")
# Get phases for a specific stack
stack_phases = plan.get_phases_for_stack("my-stack")
Phase
Represents a single execution phase:
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases.planner import PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
plan = PhasePlanner().create_plan(app)
# Access phases from plan
for phase in plan.phases:
print(f"Phase {phase.number} for {phase.stack_id}")
print(f" Resources: {len(phase.resources)}")
print(f" Description: {phase.description}")
# Check parallel execution settings
print(f" Parallel strategy: {phase.parallel_strategy}")
print(f" Max concurrent: {phase.max_concurrent}")
Advanced Usage
Custom Dependency Analysis
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases.analyzer import DependencyAnalyzer
from nf.core.phases.planner import PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
# Create custom analyzer with the cache enabled
analyzer = DependencyAnalyzer(cache_enabled=True)
# Create planner with custom analyzer
planner = PhasePlanner(analyzer)
# Create plan
plan = planner.create_plan(app, strategy="minimal")
Saving Plan Summaries
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
import json
import tempfile
from pathlib import Path
from nf.core.phases import PhasePlanner, phase_plan_to_json
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
plan = PhasePlanner().create_plan(app)
plan_summary = phase_plan_to_json(plan)
with tempfile.TemporaryDirectory() as directory:
# Save it where your team keeps plans, e.g. "deployment-plan.json"
(Path(directory) / "deployment-plan.json").write_text(json.dumps(plan_summary, indent=2))
A reference between constructs is written as Terraform writes it, ${aws_security_group.database.id}.
Performance Considerations
Parallel Execution
The planner records per-phase parallelization intent:
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases import ParallelStrategy, PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
plan = PhasePlanner().create_plan(app)
plan.phases[0].parallel_strategy = ParallelStrategy.CONSERVATIVE
plan.phases[0].max_concurrent = 5
Caching
The dependency analyzer uses caching to improve performance:
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases import DependencyAnalyzer
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
# Analyzer with caching enabled
analyzer = DependencyAnalyzer(cache_enabled=True)
Troubleshooting
Debugging Circular Dependencies
# coding: nf_autoid
from nf.core import App, Bind, BindContext, Stack
from nf.providers.hashicorp.aws import SecurityGroup
from nf.core.phases.planner import PhasePlanner
with App(nf_id="app") as app, Stack(nf_id="my-stack") as stack, BindContext:
web: Bind = SecurityGroup(name="web")
database: Bind = SecurityGroup(name="database")
web.ingress = [{"from_port": 443, "to_port": 443, "protocol": "tcp", "security_groups": [database.ref_id_]}]
database.ingress = [{"from_port": 5432, "to_port": 5432, "protocol": "tcp", "security_groups": [web.ref_id_]}]
planner = PhasePlanner()
plan = planner.create_plan(app)
# Print detailed circular dependency info
for cd in plan.circular_dependencies:
print(f"Cycle: {cd.get_description()}")
print(f"Cross-stack: {cd.is_cross_stack}")
print(f"Stacks involved: {cd.participating_stacks}")
# Get resources in cycle
for stack_id, resources in cd.participating_resources.items():
print(f" {stack_id}: {resources}")
Deployment
Phased synthesis currently produces explicit export artifacts rather than a normal local CLI runtime path. Use the generated manifest and README for Terraform/OpenTofu handoff, or prefer direct execution for local deployments.
Logging
The phases system uses standard Python logging:
import logging
# Enable debug logging
logging.getLogger("nf.core.phases").setLevel(logging.DEBUG)
Best Practices
- Always analyze before deploying: Use
PhasePlanner.create_plan()to understand what will happen - Save plan summaries: Save
phase_plan_to_json(plan)output for repeatability and debugging - Keep the default strategy:
auto(minimal) needs the fewest phases;level_basedadds a phase per dependency level