Skip to main content
In Infrastructure as Code (IaC), the DRY (Don’t Repeat Yourself) principle is essential for creating modular, maintainable, and scalable configurations. Terragrunt extends Terraform by enforcing DRY across your codebase. In this guide, you’ll learn how to:
  • Structure reusable modules to eliminate redundancy
  • Centralize and abstract variables for consistency
  • Inherit shared settings through a hierarchical layout
  • Streamline maintenance and promote changes safely
This article assumes basic familiarity with Terraform and HCL. For a Terraform refresher, see Terraform Overview.

1. Modular Configuration

By encapsulating common resources in Terragrunt modules, you define infrastructure blocks once and reuse them across environments. This approach reduces code duplication and speeds up delivery. Example module call in your environment folder:
The image illustrates the "Don't Repeat Yourself" (DRY) principle, highlighting modular configuration, modular and reusable components, and reduced redundancy.

2. Variable Abstraction

Terragrunt centralizes variable definitions into shared files, preventing hard-coded values and scattered overrides. Reuse a single variables.hcl across modules:
Then reference it:
The image explains the "Don't Repeat Yourself" (DRY) principle, highlighting variable abstraction to avoid code repetition and enable centralized variable management.

3. Hierarchical Configuration Inheritance

Terragrunt’s directory structure supports inheritance of configuration blocks. Define global settings at the root, then override or extend them in child folders:
The image explains the "Don't Repeat Yourself" (DRY) principle, highlighting hierarchical configuration, enabling inheritance of settings, reducing duplicate configurations, and facilitating reuse of configuration settings.

4. Simplified Maintenance and Promotion

With DRY in place, updating modules or variables in one location propagates changes everywhere they’re used. This reduces configuration drift, lowers the risk of errors, and accelerates promotions across dev, staging, and prod.
The image illustrates the "Don't Repeat Yourself" (DRY) principle, highlighting benefits like simplified maintenance, more maintainable code, and uniform updates across code.

Watch Video