read_terragrunt_config, which reads a Terragrunt HCL file and returns its contents as a map. This enables you to:
- Dynamically consume inputs, outputs, blocks, and attributes from another configuration.
- Adapt resources for different environments or setups.
- Promote modularity, reusability, and DRY infrastructure code.
- Parse the specified HCL file.
- Serialize its contents into a map.
- Expose all blocks and attributes under that map for referencing in your configuration.
Key Benefits
- Dynamic Configuration: Tailor resources per environment.
- Modularity: Reuse shared inputs and outputs.
- Maintainability: Avoid duplication across modules.
Best Practices
| Practice | Recommendation |
|---|---|
| Centralize common inputs | Store project-level variables in a root common.hcl. |
Use locals for mapping | Assign descriptive local names to imported values. |
| Keep shared configs lean | Include only widely used variables to reduce complexity. |
| Verify relative paths | Ensure the path passed to the function is correct. |

Use clear naming conventions in your
common.hcl to avoid confusion when referencing nested attributes.Example: Sharing Common Variables
Create a root-levelcommon.hcl with project-wide variables:
vpc/ directory, import these inputs using read_terragrunt_config:
- Terragrunt reads
../common.hcl. local.common_vars.inputscontains:project:"KodeKloud"environment:"dev"
- The VPC module’s
nameinput resolves to:
Verify the relative path to
common.hcl, or Terragrunt will error out with a file-not-found.Initializing Terragrunt
Run the following in thevpc/ directory:
After initialization, run
terragrunt plan to preview changes based on your dynamic inputs.