path_relative_to_include function. This built-in helper calculates the relative path between the current .hcl file and the file specified in its include block. Use it to:
- Organize and modularize your configuration files
- Construct dynamic file paths
- Segregate remote state keys per module
Why Use path_relative_to_include?
| Benefit | Description |
|---|---|
| Dynamic Configuration | Adjust resource definitions based on their position in the directory hierarchy. |
| Modular Structures | Build reusable, adaptable modules that survive folder reorganizations. |
| Remote State Segregation | Generate unique backend keys for each module, keeping your state isolated. |
When used without arguments,
path_relative_to_include() returns the relative path from the current folder to where common.hcl (or your included file) lives.Typical Terragrunt Setup
Imagine you have a sharedcommon.hcl in a parent directory and multiple child modules that include it. You can tag each module’s remote state with its relative path:
terragrunt plan:
Path tag reflects this module’s folder relative to common.hcl. By doing so, each module’s Terraform state is stored under a unique key in your remote backend, enhancing clarity and maintainability.