Introduction to Terragrunt with a Simple Example
🌍 Introduction to Terragrunt with a Simple Example
Terragrunt is a thin wrapper for Terraform that provides extra tools for keeping your configurations DRY (Don't Repeat Yourself), managing remote state, and working with multiple environments and modules more easily.
If you've worked with Terraform and struggled with:
- Copy-pasting the same backend config in multiple places
- Managing multiple environments like dev/staging/prod
- Handling dependencies between modules
Then Terragrunt can be your best friend!
🚀 Why Use Terragrunt?
- ✅ Centralized remote state management
- ✅ Keep configurations DRY with reusable components
- ✅ Manage multiple environments cleanly
- ✅ Handle module dependencies easily
🔧 How Terragrunt Works
Terragrunt uses a file named terragrunt.hcl
to:
- Define where the Terraform module source lives
- Inject input variables dynamically
- Configure remote state settings
- Establish dependencies between modules
📦 A Simple Terragrunt Example
🎯 Goal:
Provision an AWS S3 bucket using a reusable Terraform module and Terragrunt.
🗂️ Directory Structure
.
├── terraform-modules/
│ └── s3/
│ └── main.tf
└── live/
└── dev/
└── s3/
└── terragrunt.hcl
📁 terraform-modules/s3/main.tf
provider "aws" {
region = var.region
}
resource "aws_s3_bucket" "this" {
bucket = var.bucket_name
acl = "private"
}
variable "bucket_name" {}
variable "region" {}
📁 live/dev/s3/terragrunt.hcl
terraform {
source = "../../../terraform-modules/s3"
}
inputs = {
bucket_name = "my-dev-s3-bucket-123456"
region = "us-east-1"
}
▶️ Run the Code
From the live/dev/s3/
directory, run:
terragrunt init
terragrunt apply
This pulls the Terraform module, injects inputs, and applies it — all without duplicating configs.
📌 Summary
- Reduces configuration duplication
- Simplifies environment management
- Handles shared infrastructure modules cleanly
✅ Next Steps
- Try setting up remote state backend with
generate
blocks - Use
locals
anddependencies
blocks - Explore environment-specific overrides with
include
💡 Pro Tip: Keepterraform-modules
in version control and reuse across all yourlive
environments (dev, staging, prod).
No comments