In this lesson, you’ll learn how to leverage theDocumentation Index
Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
Use this file to discover all available pages before exploring further.
locals block in Terragrunt to define reusable variables and expressions directly within your configuration. By centralizing complex values, you can simplify your HCL, reduce duplication, and improve maintainability.
Benefits of the locals Block
Encapsulating expressions or static values into named local variables offers clear advantages:| Benefit | Description |
|---|---|
| Code readability | Improves comprehension by giving meaningful names to expressions |
| Code reusability | Follows DRY (Don’t Repeat Yourself) by reusing values in multiple places |

Considerations
Local variables are strictly confined to the configuration where they’re declared. They won’t be shared across sibling or parent Terragrunt files.locals in Terragrunt are not global. You cannot reference a local variable defined in one directory from another unless explicitly passed through inputs or shared via a common config.
Best Practices
- Group related expressions under a single
localsblock to keep your configuration tidy. - Name variables clearly to convey their purpose (e.g.,
project_name,environment_cidr). - Avoid overusing locals for trivial values; reserve them for expressions or values reused multiple times.
- Document complex locals with inline comments for future maintainers.

Example Usage
1. Defining a local variable
2. Referencing locals from a shared config (common.hcl)
3. Adding file-specific locals
You can define additionallocals in any Terragrunt file for values unique to that directory.
cidr variable is available only within the current VPC configuration, ensuring isolation from other modules.