Automatic Terragrunt Discovery with Config Builder
For large Terragrunt monorepos with hundreds of modules, manually configuring each module in .terrateam/config.yml is tedious and error-prone. Terrateam includes a built-in config builder that automatically discovers all terragrunt.hcl files and generates configuration with proper dependency tracking—similar to how terragrunt-atlantis-config works for Atlantis.
When to Use This
Section titled “When to Use This”The Terragrunt config builder is ideal for:
- Large monorepos with many Terragrunt modules
- Repositories using Terragrunt’s
dependencyblocks for module relationships - Teams migrating from Atlantis with terragrunt-atlantis-config
- Avoiding manual maintenance of hundreds of
dirsentries
If you only have a handful of modules, manual configuration may be simpler. For everything else, the config builder automates the process.
How It Works
Section titled “How It Works”The config builder:
- Scans the repository for all
terragrunt.hclfiles - Parses each file to extract:
dependencyblocks → Createsdepends_onrelationshipsdependenciesblocks → Multiple dependenciesincludeblocks → Parent config filesterraform.source→ Local Terraform modules (remote sources ignored)locals.extra_atlantis_dependencies→ Custom dependencies
- Generates configuration with
when_modifiedpatterns for each module - Creates dependency chains using Terrateam’s layered runs
What Gets Generated
Section titled “What Gets Generated”For each Terragrunt module, the config builder creates a dirs entry with:
tags:['terragrunt']for easy filteringwhen_modified.file_patterns: List of files that trigger this modulewhen_modified.depends_on: Direct dependencies for layered execution
Quick Start
Section titled “Quick Start”-
Enable the config builder in your
.terrateam/config.yml:engine:name: terragruntversion: 0.69.3config_builder:enabled: truescript: terragrunt-config-builderwhen_modified:autoplan: true -
Commit and push your changes to a branch
-
Create a pull request
-
View the generated configuration by commenting on the PR:
terrateam repo-config -
Watch autoplan run for all discovered modules
Configuration
Section titled “Configuration”Basic Setup
Section titled “Basic Setup”The minimum configuration to enable automatic Terragrunt discovery:
engine: name: terragrunt
config_builder: enabled: true script: terragrunt-config-builderengine: name: terragrunt version: 0.69.3
config_builder: enabled: true script: terragrunt-config-builder# Enable Terragrunt engineengine: name: terragrunt version: 0.69.3 tf_cmd: tofu # or "terraform" tf_version: "1.10.7"
# Enable automatic discoveryconfig_builder: enabled: true script: terragrunt-config-builderHow the Script is Called
Section titled “How the Script is Called”The config builder:
- Receives the current configuration as JSON via stdin
- Processes all terragrunt.hcl files
- Outputs the modified configuration as JSON to stdout
The script runs in the GitHub Action environment with full access to your repository.
Example Output
Section titled “Example Output”Repository Structure
Section titled “Repository Structure”non-prod/├── us-east-1/│ ├── qa/│ │ ├── mysql/│ │ │ └── terragrunt.hcl│ │ └── webserver-cluster/│ │ └── terragrunt.hcl # depends on mysql│ └── stage/│ ├── mysql/│ │ └── terragrunt.hcl│ └── webserver-cluster/│ └── terragrunt.hcl # depends on mysqlprod/├── us-east-1/│ └── prod/│ ├── mysql/│ │ └── terragrunt.hcl│ └── webserver-cluster/│ └── terragrunt.hcl # depends on mysqlrepo.hclaws.hclGenerated Configuration
Section titled “Generated Configuration”Running terrateam repo-config shows:
{ "version": "1", "dirs": { "non-prod/us-east-1/qa/mysql": { "tags": ["terragrunt"], "when_modified": { "file_patterns": [ "${DIR}/terragrunt.hcl", "${DIR}/*.tf", "${DIR}/*.tfvars" ] } }, "non-prod/us-east-1/qa/webserver-cluster": { "tags": ["terragrunt"], "when_modified": { "file_patterns": [ "${DIR}/terragrunt.hcl", "${DIR}/*.tf", "${DIR}/*.tfvars", "non-prod/us-east-1/qa/mysql/terragrunt.hcl" ], "depends_on": "dir:non-prod/us-east-1/qa/mysql" } } // ... similar entries for stage and prod }}Key Points:
- Each module has a
dirsentry - MySQL modules have no
depends_on(no dependencies) - Webserver-cluster modules
depends_ontheir corresponding MySQL - File patterns include the dependency’s terragrunt.hcl
See Also
Section titled “See Also”- Terragrunt Integration - Basic Terragrunt setup
- Config Builder Reference - Config builder configuration options
- Layered Runs - Understanding depends_on and execution order
- Dynamic Configuration - How config_builder fits into the config merge process