Terragrunt
Terragrunt is a thin wrapper that provides extra tools for keeping your configurations “Don’t Repeat Yourself” (DRY), working with multiple Terraform modules, and managing remote states.
Running Terragrunt with OpenTofu / Terraform
The env0 platform supports running Terragrunt with either OpenTofu or Terraform.
By default the selected TF binary is OpenTofu. In order to change that, you should open the Advanced
settings and select which TF binary to use:
You can check Terragrunt's Terraform and OpenTofu version compatibility table here.
Run-all Terragrunt command checkbox
Within Terragrunt template configuration, under Advanced
settings - check Execute run-all commands on multiple modules
- Adds
run-all
to each command, read more here. - Supported from version 0.28.1.
Local Terraform State
By default, env0 stores the local state for next deployments.
In some cases, we will not be able to store the local state for your Terragrunt modules. Please configure a remote backend with your Terragrunt code Remote terraform code, otherwise your Terragrunt code may not work properly with env0.
Cross-Module Dependencies
env0's Terraform deployment flow includes a
plan
step and anapply
step that uses the plan's results (plan file/s) to ensure every change is aligned between both steps.According to the official docs, the
run-all plan -out=/path/to/plan-file
command we use will fail if there are yet-to-be-applied dependencies between modules. It will only output some of the expected plan files. This leads to the failure of therun-all apply /path/to/plan-file
command that is being executed next, since it expects all modules to have plan files.As a workaround, we expose the
ENV0_OMIT_TERRAGRUNT_RUN_ALL_PLAN_FILE
environment variable. Simply set its value totrue
and deploy. Therun-all plan
andrun-all apply
commands will be executed without passing plan files between them. This is risky and should only be done on initial deployment. On success, it is advised to remove this environment variable in order to keep theplan
andapply
steps in-sync.
Target modules
env0 supports terragrunt include dir , which is a CLI Arg for Terragrunt environment that runs in a run-all
mode by using environment variable ENV0_TERRAGRUNT_RUN_ALL_INCLUDE_DIRS
. Terragrunt will plan and apply only those modules and their dependencies when you use this variable.
Example:
ENV0_TERRAGRUNT_RUN_ALL_INCLUDE_DIRS=module1,path_to_module/module2
Smart detect Terragrunt modules changes
If you use Terragrunt run all
PR Plan feature you can use our plugin for running plan only for changed modules in the PR
Troubleshooting
There is a known issue with terragrunt run-all plan -destroy -out=.tf-plan
generating an incorrectly ordered graph. Thus, if you have dependency issues with run-all destroy, please use ENV0_OMIT_TERRAGRUNT_RUN_ALL_PLAN_FILE=true
in your pipeline to ignore the plan file.
✏️ Suggested Blog Content
Updated 8 months ago