Back to Terragrunt

What Is The Difference Between A Unit And A Stack

docs/src/data/faq/what-is-the-difference-between-a-unit-and-a-stack.mdx

1.1.21.9 KB
Original Source

Unit

A unit is a single instance of infrastructure that Terragrunt manages: one VPC, one database, one cluster. A directory containing a terragrunt.hcl file is a unit, and each unit gets its own state file. Units are the target of a single run, so terragrunt apply inside a unit directory applies exactly that unit.

Stack

A stack is a collection of units managed together, such as every unit that makes up one environment or one region. Units in a stack can depend on each other, and Terragrunt orders them using the DAG built from their dependency blocks.

Stacks come in two forms:

  • Implicit stacks are just a directory tree of units. Nothing declares the stack; it is the set of units Terragrunt discovers under the directory you run from.
  • Explicit stacks are declared in a terragrunt.stack.hcl file, which generates units into a .terragrunt-stack directory from sources you point at. This lets you define a pattern once and instantiate it repeatedly with different values.

A single directory cannot be both. Putting a terragrunt.hcl and a terragrunt.stack.hcl side by side in the same directory is an error.

Which commands apply to which

CommandOperates on
terragrunt plan, terragrunt applyThe unit in the current directory
terragrunt run --all <cmd>Every unit discovered under the working directory
terragrunt stack generateThe terragrunt.stack.hcl in the current directory
terragrunt stack run <cmd>The units generated by that terragrunt.stack.hcl
terragrunt stack outputOutputs of the units in the generated stack

Both run --all and stack run generate explicit stacks before they run, so you do not need a separate stack generate step. Pass --no-stack-generate to skip it.