Terragrunt stacks and dependency ordering
Stacks group configurations, not state
A Terragrunt stack organizes related units. Each unit still delegates infrastructure management to Terraform or OpenTofu and retains its own state identity. An implicit stack is a directory tree of separately authored units; an explicit stack describes how to generate units from reusable source configurations. Neither makes separate states a transaction. Consider a fictional application that consumes a network output and has a separate logging component.
dev/network -> dev/appdev/logging (independent)Arrow means prerequisite -> consumer.Apply order: network before app; logging can run independently.Destroy order: app before network; logging can run independently.The arrows in this course point from prerequisite to consumer. The actual engine graph within each unit is a separate graph. No native Terragrunt execution is available; the inherited 1.1.2 candidate is not verified. See https://docs.terragrunt.com/features/stacks/.
Generate a repeatable environment
A catalog stores reviewed unit templates. The stack file identifies a source directory for each template, a destination path and configuration values. Here the source paths assume live/dev/terragrunt.stack.hcl; they are illustrative and not supplied as runnable directories.
# Static example; catalog directories are illustrative.unit "network" { source = "../../catalog/units/network" path = "network" values = { environment = "dev" }}
unit "app" { source = "../../catalog/units/app" path = "app" values = { environment = "dev" }}Generation normally writes units below .terragrunt-stack. A generated network and app are ordinary units; the values are written into a generated terragrunt.values.hcl file where applicable. A catalog unit can read the supplied value.
# Static catalog fragment; values supplied by stack generation.inputs = { environment = values.environment}This fragment is not a full unit: it omits the engine source and state configuration. Review the resolved generated tree rather than editing generated files as the source of truth. Generation itself can read or fetch code and write files. See https://docs.terragrunt.com/reference/hcl/stacks/ and https://docs.terragrunt.com/reference/hcl/blocks/unit/.
Compose stacks and maintain identities
A nested stack can generate a reusable environment pattern.
# Static illustration in live/terragrunt.stack.hcl.stack "dev" { source = "../catalog/stacks/environment" path = "dev" values = { environment = "dev" }}The environment template would declare its own units and forward the environment value as needed. Keep source versions, paths and output contracts reviewable. If state keys are derived from generated paths, a changed path may point at a different state. Do not equate generation or removing generated files with state migration or destroying infrastructure. Check each backend identity independently. See https://docs.terragrunt.com/reference/hcl/blocks/stack/.
Express ordering and output contracts
The app needs the network label, so it declares an output-consuming dependency. In a generated sibling layout the relative path points to the network unit.
# Static generated app configuration fragment.dependency "network" { config_path = "../network" mock_outputs = { label = "synthetic-network" } mock_outputs_allowed_terraform_commands = ["validate"]}
inputs = { network_label = dependency.network.outputs.label}A dependencies block can express ordering without consuming outputs. A dependency block can additionally read upstream state outputs. The mock here is limited to validate; a plan or apply needs the appropriate real output. Even if the network is planned first, the plan alone does not publish an applied output to the app. A change to the output name, type or meaning requires reviewing consumers. See https://docs.terragrunt.com/reference/hcl/blocks/dependency/ and https://docs.terragrunt.com/reference/hcl/blocks/dependencies/.
Predict the queue
A directed acyclic graph (DAG) is a set of dependency edges without cycles. The wrapper schedules eligible units based on the operation: prerequisites precede consumers during normal plan or apply; destroy reverses the order. The independent logging unit may run alongside other eligible work. If the network also required the app, the resulting cycle would prevent a valid ordering.
Wave 1: network, logging (if concurrency allows)Wave 2: app (after network succeeds)Destroy: app before network; logging independentlyThe waves are an educational simplification, not a guarantee of actual scheduling or log order. A successful upstream plan is not a successful apply. Next, review the scope and failure cases.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Terragrunt stacks and dependency ordering — knowledge check
Review stack generation, dependency ordering and recovery.
What distinguishes an explicit stack from an implicit stack?
Network is a prerequisite of app. Which order respects a normal apply?
A network plan proposes a new output. What can app consume immediately?
Why review paths when changing generation layout?
Which block provides upstream outputs as well as an ordering relationship?