DEV Community

Bartłomiej Danek
Bartłomiej Danek

Posted on • Originally published at bard.sh

Terragrunt run-all

run-all

Runs a Terraform command across all units in the current directory tree, respecting dependency order.

cd environments/prod
terragrunt run-all plan
terragrunt run-all apply
terragrunt run-all destroy   # reverse order
Enter fullscreen mode Exit fullscreen mode

dependency-aware ordering

environments/prod/
├── vpc/
├── rds/          # depends on vpc
└── eks/          # depends on vpc
Enter fullscreen mode Exit fullscreen mode

run-all apply applies vpc first, then rds and eks in parallel.

real CI pipeline

# .github/workflows/deploy.yml
- name: Plan all
  run: terragrunt run-all plan --terragrunt-non-interactive
  working-directory: environments/prod

- name: Apply all
  run: terragrunt run-all apply --terragrunt-non-interactive -auto-approve
  working-directory: environments/prod
  if: github.ref == 'refs/heads/main'
Enter fullscreen mode Exit fullscreen mode

Detect drift in CI without applying:

# exit code 2 = changes detected, 0 = no changes, 1 = error
terragrunt run-all plan \
  --terragrunt-non-interactive \
  -detailed-exitcode
Enter fullscreen mode Exit fullscreen mode

filtering

# skip a unit
terragrunt run-all apply --terragrunt-exclude-dir environments/prod/rds

# target a single unit and its dependencies
terragrunt run-all apply --terragrunt-include-dir environments/prod/eks
Enter fullscreen mode Exit fullscreen mode

parallelism

# default is unlimited parallel - cap it if you hit API rate limits
terragrunt run-all apply --terragrunt-parallelism 4
Enter fullscreen mode Exit fullscreen mode

output

Each unit prefixes its own output:

[environments/prod/vpc] Apply complete! Resources: 12 added.
[environments/prod/rds] Apply complete! Resources: 4 added.
[environments/prod/eks] Apply complete! Resources: 31 added.
Enter fullscreen mode Exit fullscreen mode

Originally published at https://bard.sh/posts/terragrunt_run_all/

Top comments (0)