Lesson 3: Terragrunt & DRY Infrastructure
🧠 The Concept (Explain Like I'm 5)
If you build 10 Lego houses (10 environments), you don't want to rewrite the entire instruction manual 10 times. Terragrunt is a wrapper for Terraform. It lets you write the instruction manual once (Terraform Module) and just pass in a tiny sticky note (Terragrunt configuration) that says "This one is Blue (Dev)" or "This one is Red (Prod)".
🏢 The Enterprise Context
- DRY (Don't Repeat Yourself): In pure Terraform, you must copy/paste the remote backend configuration (S3/DynamoDB) into every single folder. Terragrunt allows you to define the backend once at the root level and all child folders inherit it.
- Dependency Management: Terragrunt can explicitly orchestrate deployments, ensuring the VPC module deploys before the EKS cluster module.
🗺️ Visual Architecture: Terragrunt Inheritance
flowchart TD
subgraph GitRepo ["GitOps Infrastructure Repo"]
Root["terragrunt.hcl<br/>(Root: Defines S3 Backend & AWS Provider)"]
subgraph Dev ["/dev"]
DevTG["terragrunt.hcl<br/>(Includes root, inputs: env=dev)"]
end
subgraph Prod ["/prod"]
ProdTG["terragrunt.hcl<br/>(Includes root, inputs: env=prod)"]
end
end
Root -.->|Inherited by| DevTG
Root -.->|Inherited by| ProdTG
DevTG -->|Calls| Module["Terraform EKS Module<br/>(Git Tag v1.2.0)"]
ProdTG -->|Calls| Module