Using dependency blocks helps enforce a clear execution order and guarantees that required resources are available before they’re referenced.
Key Attributes
| Attribute | Type / Default | Description |
|---|---|---|
| name | string | Identifier for the dependency block. Useful when you have multiple dependencies. |
| config_path | string | Path to the Terragrunt module or folder with the terragrunt.hcl. |
| enabled | boolean / true | Controls whether Terragrunt fetches this dependency. Set to false to skip resolution. |
| skip_outputs | boolean / false | If true, skips executing terragrunt output, reducing runtime when outputs aren’t needed. |
| mock_outputs | map | Key-value pairs to return when the target module has no real outputs. |
| mock_outputs_allowed | list | Terraform commands (e.g., plan, apply) for which mock outputs can be used. |
| mock_outputs_merge_strategy_with_state | string | Strategy for merging mock outputs with existing remote state (e.g., merge, overwrite). |
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: