Skip to main content
In this lesson, we’ll dive into Terragrunt’s built-in function 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.
When you invoke:
Terragrunt will:
  1. Parse the specified HCL file.
  2. Serialize its contents into a map.
  3. 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

The image outlines best practices for "read_terragrunt_config," highlighting the need for resources to adapt dynamically and access input/output configurations.
Use clear naming conventions in your common.hcl to avoid confusion when referencing nested attributes.

Example: Sharing Common Variables

Create a root-level common.hcl with project-wide variables:
In the vpc/ directory, import these inputs using read_terragrunt_config:
What happens under the hood:
  1. Terragrunt reads ../common.hcl.
  2. local.common_vars.inputs contains:
    • project: "KodeKloud"
    • environment: "dev"
  3. The VPC module’s name input 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 the vpc/ directory:
Expected output:
After initialization, run terragrunt plan to preview changes based on your dynamic inputs.

Watch Video