Terragrunt and keeping config DRY

Intermediate · 17 min

Terragrunt generates repeated backend and provider blocks, wires unit outputs together and runs a tree in dependency order. What it costs and when it pays.

Before thisEnvironments with directories and tfvars

Josh Geissler · 17 min

Terragrunt and keeping config DRY

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Josh GeisslerReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Terragrunt is a separate wrapper tool that reads terragrunt.hcl files and runs Terraform or OpenTofu for each unit.
  • include and remote_state with path_relative_to_include() define the backend once and give every unit its own state key.
  • dependency blocks pass outputs between units and let run --all apply a tree in order; mock_outputs lets plan work before the dependency exists.
  • It adds a tool and a config language, so it pays when many units repeat the same boilerplate, not for three small environments.

Forty copies of the same backend block

A directory per environment and per component is the layout that scales. It is also forty directories, and each one needs a backend.tf whose only difference is its key, a provider.tf that is the same everywhere, and a module call that passes the same twelve values. Change the state bucket’s name and you edit forty files. Forget one and it silently keeps writing state to the old bucket.

Terraform has no way to say “this block, everywhere, with the key filled in from the path.” Backend blocks cannot even use variables. Terragrunt, from Gruntwork, is a wrapper that sits in front of Terraform or OpenTofu and fills that gap. It is a separate tool you install, not part of Terraform.

What it adds

A directory with a terragrunt.hcl is a unit. Terragrunt reads that file, writes generated files such as backend.tf, and then runs the underlying terraform or tofu command. The pieces, from the Terragrunt docs:

# live/prod/app/terragrunt.hcl
include "root" {
  path = find_in_parent_folders("root.hcl")
}

terraform {
  source = "../../../modules//web-service"
}

dependency "vpc" {
  config_path = "../vpc"
}

inputs = {
  instance_type = "m7g.large"
  subnet_ids    = dependency.vpc.outputs.private_subnet_ids
}
# live/root.hcl
remote_state {
  backend  = "s3"
  generate = { path = "backend.tf", if_exists = "overwrite_terragrunt" }
  config = {
    bucket       = "acme-tf-state"
    key          = "${path_relative_to_include()}/terraform.tfstate"
    region       = "eu-west-1"
    use_lockfile = true
  }
}

I wrote both from the Terragrunt documentation. I did not run Terragrunt, since neither it nor Terraform is installed here.

Three things happen. include pulls the shared root file in, so the backend is defined once. path_relative_to_include() turns the directory path into the state key, which makes the copy-paste collision from the layouts lesson impossible. And dependency reads another unit’s outputs and tells Terragrunt the order to run in.

The graph it builds

Because units declare dependencies, terragrunt run --all apply can walk a whole tree in the right order, applying dependencies first, and run --all destroy goes in reverse. The docs show the 1.x commands as terragrunt run --all plan|apply|destroy. Older tutorials use terragrunt run-all. I did not check when that spelling was retired, so if a video uses it, compare with the current docs.

I wrote a Python stand-in for the ordering part: it finds config_path lines in each terragrunt.hcl and groups units into waves with the standard library’s topological sorter. Three units, app depends on vpc and db, db depends on vpc:

$ python3 -I order.py live/prod
wave 1: ['vpc']
wave 2: ['db']
wave 3: ['app']

Units in the same wave can run in parallel. When I changed db to depend on app, creating a loop, the sorter raised CycleError: nodes are in a cycle. That message is Python’s. I did not run Terragrunt, so I do not know how it words the same mistake.

One more gotcha from the docs: dependency needs the other unit’s outputs, which do not exist before it is applied. mock_outputs, limited with mock_outputs_allowed_terraform_commands = ["plan"], gives plan something to read on a fresh tree.

Stacks, and where Terraform is heading

Terragrunt’s newer stacks let you define a set of units once and generate them per environment, so “prod” is a few lines instead of a directory tree. The docs list terragrunt stack generate, stack run, stack output and stack clean. I read the CLI list but not the stack file format, so I do not describe the file here. When I checked on 28 November 2026, Terragrunt’s latest release was v1.2.3, from 25 November.

Terraform itself has “Terraform Stacks” in HCP Terraform, a different feature with a similar name and a similar goal, covered in HCP Terraform and managed platforms. Do not mix the two up when you read a tutorial.

Should you use it

Terragrunt solves a real problem, and it adds a second tool, a second config language to learn and a second place for bugs. Some teams outgrow plain directories, some stop at them. Ned in the Cloud’s “Is Terragrunt Worth It?” in deeper asks that question by its title. I have not watched it, so here is only what I would weigh:

  • You have many environments and components with the same backend and provider boilerplate.
  • You need ordered applies across many root modules, and a shell script is no longer enough.
  • Your CI already runs Terraform per directory, and you would rather it ran one command.

If you have three environments with four components each, a small script that prints each directory’s key may be enough.

About the video

The embed is Josh Geissler’s “How To Use Terragrunt to Manage Terraform At Scale”, 17 minutes 12 seconds, March 2025. Its description says it covers what Terragrunt is and how it reduces duplication, and links Gruntwork’s terragrunt-infrastructure-live-example repository. I did not watch it all, and I judged it English from the title, description, channel and its English auto-caption track. It is from March 2025, so it predates parts of what the docs now show, including newer stack commands, and the 1.x command line may differ from what it shows. Ned in the Cloud’s video in deeper is 42 minutes long.

Where this fits

Boilerplate is one cost of many states. The other is deciding where to cut them. Splitting state is how to choose the cuts.

Check yourself

  1. 01What problem does remote_state with path_relative_to_include() solve?

    It writes the backend once and derives each unit's state key from its directory, so no copied directory can share another's key.

  2. 02How does Terragrunt know which unit to apply first?

    dependency blocks declare config_path references. run --all builds a graph from them and applies dependencies before dependents.

  3. 03Why use mock_outputs on a dependency?

    On a fresh tree the dependency has no outputs yet. Mocks restricted to plan let the plan run before that unit is applied.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark