In the previous subchapter, we saw that the directory-per-environment strategy is excellent, but it has a small drawback: some repetition between environments. When you have many environments or many components, that repetition grows. Terragrunt is a community tool that solves exactly that, helping you keep your code DRY. Let’s see what it is and when it’s worth it.

The DRY Principle

DRY stands for "Don't Repeat Yourself". It’s a fundamental programming principle: each piece of information or logic should exist in one single place. Repeating code is bad because, if you need to change something, you have to change it in many places (and it’s easy to forget one).

Repeated (bad):       the same configuration code in dev, stg, prod
DRY (good):           the common configuration in ONE place, each environment only its differences

In subchapter 19.2, we saw that the directory strategy leaves some repetition in the main.tf of each environment (backend configuration, calls to similar modules...). Terragrunt tackles that repetition.

What is Terragrunt

Terragrunt is a tool that wraps Terraform (a "wrapper"), created by the company Gruntwork. It does not replace Terraform: it complements it, adding features to better manage multiple environments and reduce repetition.

   Terragrunt  ──(uses underneath)──►  Terraform
   (reduces repetition,                (does the real work)
    manages environments)

Analogy: if Terraform is the engine, Terragrunt is a driver assistance system that sits on top: the engine still does the work, but driving becomes more comfortable and you make fewer mistakes, especially on long and repetitive journeys (many environments).

What Problems Does Terragrunt Solve

  1. Backend Configuration Without Repetition

Remember that each environment needs to configure its state backend (Chapter 11, subchapter 20.1), and that is repeated in each folder. With Terragrunt, you define the backend configuration only once and each environment inherits it, automatically generating the part that changes (for example, the state path for each environment).

  1. Common Values in One Place

Values shared by all environments (region, common tags, accounts...) are defined once and inherited, instead of being copied into each environment.

  1. Less Repeated Code Per Environment

Each environment is reduced to a small file that says "use this module with these specific values," while all the common machinery (backend, providers, configuration) is centralized. The files for each environment are minimal: only their differences.

Typical structure with Terragrunt:
 terragrunt.hcl              ← common configuration (backend, region...) ONCE
 environments/
  ├── dev/terragrunt.hcl     ← only: "use module X with these dev values"
  ├── stg/terragrunt.hcl     ← only the stg differences
  └── prod/terragrunt.hcl    ← only the prod differences

  1. Managing Dependencies Between Components

Terragrunt also helps orchestrate multiple infrastructure components (network, database, application) and their dependencies, applying them in order, which is very useful in large projects.

When to Use Terragrunt (and When Not To)

Like any tool, Terragrunt adds its own complexity (another tool to learn and maintain). The decision to adopt it depends on the size of your project:

Situation Terragrunt?
Small project, 1-2 environments ❌ Not needed; directory strategy is enough
You’re starting with Terraform ❌ Learn Terraform first
Many environments and/or many components ✅ Reduces a lot of repetition
Repeated configuration that hurts ✅ Its strong point
Large team with complex infrastructure ✅ Brings order and consistency

Important tip: don’t adopt Terragrunt from day one. Start with Terraform and the directory strategy from subchapter 19.2. Only when you feel that repetition between environments becomes a real problem, consider Terragrunt. Adopting it too soon adds unnecessary complexity (remember the lesson from Chapter 17 about not choosing complex tools just because they’re trendy).

A Note on Alternatives

It’s worth knowing that Terragrunt is not the only option. Terraform itself has been adding improvements, and platforms like Terraform Cloud / HCP Terraform (which we’ll see in Chapter 22) also help manage environments. Terragrunt is still very popular, but the ecosystem evolves; choose according to what your team needs and knows.

What You Should Remember

  • DRY ("Don't Repeat Yourself") is the principle of not repeating code: each thing should be in one single place, so you don’t have to change it in many places.
  • Terragrunt is a tool that wraps Terraform (does not replace it) to reduce repetition when managing multiple environments. Like a "driver assistant" on top of the Terraform engine.
  • It solves: backend configuration without repetition, common values in one place, less code per environment (only the differences), and orchestration of dependencies between components.
  • Don’t adopt it from the start: begin with Terraform + directories per environment (subchap. 19.2), and switch to Terragrunt only if repetition becomes a real problem. Don’t add complexity just because it’s trendy.

In the last subchapter of the chapter, we’ll see how to manage values for each environment in detail: environment variables and .tfvars files.

Cloud, AWS & Terraform — From Zero to Expert

Chapter 1 · What is cloud computing

Chapter 2 · The cloud market and major providers

Chapter 3 · Regions, availability zones and edge

Chapter 4 · Compute: EC2

Chapter 5 · Storage: S3

Chapter 6 · Networking: VPC

Chapter 7 · Identity and access: IAM

Chapter 8 · Managed databases

Chapter 9 · Why Infrastructure as Code

Chapter 10 · HCL: the Terraform language

Chapter 11 · Providers and state

Chapter 12 · Your first real infrastructure in Terraform

Chapter 13 · Load balancing and auto scaling

Chapter 14 · Serverless with Lambda

Chapter 15 · Messaging and events

Chapter 16 · Content delivery and DNS

Chapter 17 · Containers on AWS

Chapter 18 · Modules: reuse and composition

Chapter 19 · Workspaces and environment management

Chapter 20 · Remote backends and locking

Chapter 21 · Infrastructure testing

Chapter 22 · Terraform in CI/CD

Chapter 23 · Defense in depth

Chapter 24 · Observability: logs, metrics and traces

Chapter 25 · Cost optimization

Chapter 26 · High availability and disaster recovery

Chapter 27 · AWS Well-Architected Framework

Chapter 28 · Serverless architectures at scale

Chapter 29 · Data platforms on AWS

Chapter 30 · Multi-account and landing zones

Chapter 31 · Platform Engineering and Internal Developer Platform

Chapter 32 · Relevant AWS certifications

Chapter 33 · Projects to consolidate what you've learned

Chapter 34 · Resources and community

© Copyright 2024. All rights reserved