Understanding generators in Infrahub
Generators are plugins that query infrastructure data and automatically create or update objects based on the results. They transform abstract service definitions into concrete infrastructure components, enabling service-oriented workflows where high-level intent creates detailed implementations automatically.Why generators matter
Infrastructure teams often think in terms of services—“deploy a web application” or “create a BGP peering”—but implementation requires creating dozens of objects: devices, interfaces, IP addresses, routing configs. Manually creating these objects is error-prone and tedious. Generators automate this translation from service intent to infrastructure implementation: Service abstraction: Define high-level services (web application, database cluster) without specifying every implementation detail Automated object creation: Generators create required infrastructure objects based on service definitions and policies Consistency: Generated objects follow organizational standards encoded in generator logic Lifecycle management: Generators update or remove objects when service definitions change Pattern reuse: Encode infrastructure patterns once, apply them to multiple servicesCore concepts
Generator definition
A generator definition (CoreGeneratorDefinition) specifies:
Targets group: Objects that trigger generator execution. When a target object is created or modified, the generator runs:
Generator instance
Each time a generator runs for a specific target object, Infrahub creates a generator instance (CoreGeneratorInstance). The instance:
- Tracks which objects the generator created
- Manages lifecycle of generated objects
- Enables cleanup when target is deleted
- Records execution status and errors
Object tracking
Generators use the SDK’s tracking feature to manage object lifecycle. When a generator creates objects, the SDK tracks them:Execution contexts
Generators run in several contexts: Development: Useinfrahubctl generator to test locally:
Architecture and implementation
High-level design
Generators follow this flow:- Target identification: Determine which objects (group members) need generator execution
- Query execution: Run the GraphQL query for each target, collecting required data
- Generation: Execute generator class logic, creating/updating/deleting objects
- Tracking: Record which objects were created for lifecycle management
- Result reporting: Report success/failure and created objects
Groups as targets
Generator targets are defined as groups (CoreGeneratorGroup). Groups can contain any schema objects—services, devices, contracts, or custom types.
When a generator runs:
- Query the target group for current members
- For each member, execute the generator
- Track results per member
Query groups
Generators also create GraphQL query groups (CoreGraphQLQueryGroup). These groups contain objects identified by the generator’s query—objects whose changes affect generator output.
During Proposed Change pipeline runs, Infrahub:
- Determines which objects changed in the branch
- Finds query groups containing those objects
- Identifies generators using those queries
- Executes affected generators
Branch awareness
Generators are branch-aware objects. This means: Different generators per branch: Feature branches can have modified generator logic Branch-local instances: Generator instances exist only in their branch Branch-specific objects: Generated objects follow normal branch semantics—they’re local to the branch until merged Testing a new generator in a branch doesn’t affect production. Only when you merge the branch do the generator and its results reach main.Implementation examples
Service catalog pattern
A common pattern is a service catalog where abstract service objects generate concrete infrastructure:IP allocation pattern
Generators can interact with Resource Manager to allocate resources:Configuration generation pattern
Generators can create artifact definitions dynamically:Use cases and workflows
Service factory
Turn Infrahub into a service factory where teams request services through simple objects:- Create service schema (WebService, DatabaseService, etc.)
- Build generators that implement each service
- Teams create service objects with required parameters
- Generators automatically create all infrastructure
- Generated artifacts configure actual systems
Contract-based workflows
Use generators to implement contracts between teams:- Network team creates ContractNetwork schema
- Application team creates contract specifying requirements
- Generator creates network resources (VLANs, subnets, firewall rules)
- Application team uses generated resources
- Changes to contract trigger generator updates
Multi-cloud provisioning
Generators can create objects for multiple cloud providers:Design trade-offs
Imperative vs. declarative
Generators are imperative—they contain procedural logic that creates objects. An alternative would be purely declarative templates. Trade-offs: Imperative (current):- Full programming flexibility
- Complex logic (conditionals, loops, calculations)
- Harder to validate statically
- May be harder to understand
- Easier to validate
- Clearer intent
- Limited expressiveness
- May require complex template language
Tracking granularity
Generators track objects at the generator instance level. An alternative would be tracking at the target level (all objects created for a target). Trade-offs: Instance-level (current):- Fine-grained lifecycle control
- Clear ownership
- More metadata overhead
- Simpler model
- Less metadata
- Less precise cleanup
Execution timing
Generators can execute automatically (Proposed Changes, events) or manually (UI, CLI). Automatic execution provides convenience but may surprise users when objects appear unexpectedly. Manual execution provides control but requires remembering to run generators. Infrahub defaults to automatic execution in Proposed Changes but allows disabling per generator. This balances automation with control.Known limitations
- Target deletion: Deleting a generator target object does not automatically delete created objects (Issue 3289). Workaround: Manually delete generated objects or re-run the generator.
- Generator errors: If a generator fails partway through, some objects may be created while others are not. Generators should be idempotent to handle re-execution.
- Cross-branch dependencies: Generators in one branch cannot create objects in another branch. Generated objects always live in the branch where the generator executes.
Related topics
- Transformations - Converting data into artifacts
- Groups - Organizing target objects
- Artifacts - Generating configuration files
- Resource Manager - Allocating IPs and resources
- Python SDK Tracking - Object lifecycle management