Terragrunt for Beginners
Terragrunt Basic Concepts
What Problems Does Terragrunt Solve
Terragrunt enhances Terraform by introducing a hierarchical project layout and workflow automation. This structured approach helps you manage complexity, centralize state, eliminate duplication, and maintain consistency across environments—all while fostering better team collaboration.
At a Glance
Challenge | Impact | Terragrunt Feature |
---|---|---|
Configuration Complexity | Hard to scale and navigate large configs | Modular folder structure & nesting |
Remote State Management | State drift and conflicts | Automated backend configuration (S3/Azure/GCS) |
Code Duplication | Maintenance overhead | DRY inheritance with include & dependency |
Environment Consistency | Drift between dev/staging/prod | Environment-specific folder layouts |
Collaboration & Versioning | Merge conflicts & upgrade challenges | Isolated workflows & semantic versioning support |
1. Configuration Complexity
As Terraform codebases grow, navigating dozens of .tf
files and interdependent modules becomes challenging. Terragrunt enforces a clear directory hierarchy:
infrastructure/
├─ live/
│ ├─ dev/
│ │ └─ terragrunt.hcl
│ ├─ staging/
│ │ └─ terragrunt.hcl
│ └─ prod/
│ └─ terragrunt.hcl
└─ modules/
├─ vpc/
│ └─ main.tf
└─ app/
└─ main.tf
This layout makes it easy to locate code, define dependencies, and scale as your team grows.
2. Remote State Management
Managing Terraform state across multiple environments and engineers introduces risk of state drift. Terragrunt automates backend setup in your terragrunt.hcl
:
remote_state {
backend = "s3"
config = {
bucket = "my-terraform-state"
key = "${path_relative_to_include()}/terraform.tfstate"
region = "us-east-1"
}
}
With a centralized and versioned state, everyone works from the same source of truth.
3. Code Duplication
Repeating provider, backend, and variable definitions for each environment leads to errors and maintenance pain. Terragrunt’s DRY approach allows you to share common configuration:
# live/common.hcl
include {
path = find_in_parent_folders()
}
inputs = {
project = "my-app"
}
Then inherit in each environment:
# live/dev/terragrunt.hcl
include { path = "../common.hcl" }
inputs = { environment = "dev" }
Tip
Use dependency
blocks to pass outputs between modules, further reducing repetition.
4. Environment Consistency
Differences between dev, staging, and prod can cause unexpected behavior. By defining environment-specific variables and settings in separate folders, Terragrunt ensures each stage uses the intended configuration:
live/
└─ prod/
└─ terragrunt.hcl # Overrides common inputs with production values
5. Collaboration and Versioning
Multiple engineers working on the same Terraform code can collide on merges or apply the wrong versions of modules. Terragrunt addresses this by:
- Isolated apply/plan contexts per environment
- Support for semantic versioning of modules
- Encouraging small, incremental changes
This leads to safer upgrades and clearer audit trails.
References
Watch Video
Watch video content