Implicit Stacks
The simplest way to create a stack is to organize your units in a directory structure in your repository. When you have multiple units in a directory, Terragrunt automatically treats that directory as a stack for the purposes of commands like terragrunt run --all apply.
Converting Terraform Modules to Units
Section titled “Converting Terraform Modules to Units”Let’s say your infrastructure is defined across multiple OpenTofu/Terraform root modules:
Directoryroot
Directorybackend-app
- main.tf
Directoryfrontend-app
- main.tf
Directorymysql
- main.tf
Directoryvalkey
- main.tf
Directoryvpc
- main.tf
To convert these to Terragrunt units, simply add a terragrunt.hcl file to each directory:
Directoryroot
Directorybackend-app
- main.tf
- terragrunt.hcl
Directoryfrontend-app
- main.tf
- terragrunt.hcl
Directorymysql
- main.tf
- terragrunt.hcl
Directoryvalkey
- main.tf
- terragrunt.hcl
Directoryvpc
- main.tf
- terragrunt.hcl
Now you have an implicit stack! The root directory contains all your units and can be managed as a single stack.
Working with Implicit Stacks
Section titled “Working with Implicit Stacks”Use the --all flag to run an OpenTofu/Terraform command on all units in the implicit stack in the current working directory:
# Deploy all units discovered in the current working directoryterragrunt run --all apply
# Plan changes across all units discovered in the current working directoryterragrunt run --all plan
# Destroy all units discovered in the current working directoryterragrunt run --all destroy
# View outputs from all units discovered in the current working directoryterragrunt run --all outputYou can also use the --graph flag to run an OpenTofu/Terraform command on all units in the DAG of the unit in the current working directory.
# Run an OpenTofu/Terraform command on all units in the DAG of the unit in the current working directoryterragrunt run --graph applyAdvantages of Implicit Stacks
Section titled “Advantages of Implicit Stacks”- Simple: Just organize units in directory trees.
- Familiar: Organized following best practices for OpenTofu/Terraform repository structures.
- Flexible: Easy to add/remove units by creating/deleting directories.
- Version Control Friendly: Each unit is a separate directory with its own history.
- Backwards Compatible: This has been the default way to work with Terragrunt for over eight years, and the majority of existing Terragrunt configurations use this approach.
Limitations of Implicit Stacks
Section titled “Limitations of Implicit Stacks”- Manual Management: Each unit must be manually created and configured.
- No Reusability: Patterns can’t be easily shared across environments.
- Repetitive: Similar configurations must be duplicated or referenced from includes.