Using dependency blocks helps enforce a clear execution order and guarantees that required resources are available before they’re referenced.
Key Attributes
Benefits of Using Dependency Blocks
- Modularization & Reusability
Break your infrastructure into discrete modules that depend on each other’s outputs. - Output References
Access values viadependency.<name>.outputs.<key>expressions. - Selective Execution
Temporarily disable dependencies using theenabledflag. - Mock Outputs for Testing
Simulate module outputs duringvalidateorplanwithout applying the dependent module. - Performance Optimization
Skip unnecessary state downloads withskip_outputsand let Terragrunt fetch only the required outputs. - Team Collaboration
Independent development and testing of modules with clearly defined input/output contracts.
Optimization Strategies
Terragrunt fetches dependencies in parallel at each root level but parses nested dependencies serially. When using remote state and optimization flags, it retrieves only the lowest-level outputs, significantly speeding up large infrastructures.Overusing mock outputs can mask real configuration issues. Always validate with real outputs before production deployments.
Best Practices
- Organize dependencies in a dedicated block at the top of your
terragrunt.hcl. - Keep
config_pathreferences relative and consistent. - Use
mock_outputsonly for CI/CD testing and validation. - Ensure remote state backends and optimization flags (
skip_outputs) are configured for faster runs. - Name your dependencies meaningfully to convey the resource relationship.
Example Usage
1. EC2 Module Without Dependencies
2. Adding a VPC Dependency
By adding adependency block, you ensure the EC2 instance uses the VPC created by another module: