Overview
Terragrunt configuration is defined in HCL files. This uses the same HCL syntax as OpenTofu/Terraform itself.
A unit is configured in a terragrunt.hcl file, and a stack in a terragrunt.stack.hcl file.
Refer to the following pages for a complete reference of supported features in the terragrunt configuration file:
Configuration parsing order
Section titled “Configuration parsing order”It is important to be aware of the terragrunt configuration parsing order when using features like locals and dependency outputs, where you can reference attributes of other blocks in the config in your inputs. For example, because locals are evaluated before dependency blocks, you cannot bind outputs from dependency into locals. On the other hand, for the same reason, you can use locals in the dependency blocks.
Currently terragrunt parses the config in the following order:
-
includeblock -
localsblock -
Evaluation of values for
iam_role,iam_assume_role_duration,iam_assume_role_session_name, andiam_web_identity_tokenattributes, if defined -
dependenciesblock -
dependencyblocks, including callingterragrunt outputon the dependent units to retrieve the outputs -
Everything else
-
The config referenced by
include -
A merge operation between the config referenced by
includeand the current config.
Blocks that are parsed earlier in the process will be made available for use in the parsing of later blocks. Similarly, you cannot use blocks that are parsed later earlier in the process (e.g you can’t reference dependency in locals, include, or dependencies blocks).
Note that the parsing order is slightly different when using the --all flag of the run command. When using the --all flag, Terragrunt parses the configuration twice. In the first pass, it follows the following parsing order:
-
includeblock of all configurations in the tree -
localsblock of all configurations in the tree -
dependencyblocks of all configurations in the tree, but does NOT retrieve the outputs -
terraformblock of all configurations in the tree -
dependenciesblock of all configurations in the tree
The results of this pass are then used to build the dependency graph of the units in the stack. Once the graph is constructed, Terragrunt will loop through the units and run the specified command. It will then revert to the single configuration parsing order specified above for each unit as it runs the command.
This allows Terragrunt to avoid resolving dependency on units that haven’t been applied yet when doing a clean deployment from scratch with run --all apply.
Included Configurations
Section titled “Included Configurations”When configurations are included via the include configuration block, Terragrunt expects configurations to be valid unit configurations.
Generally speaking, any HCL file found in a Terragrunt project that isn’t named terragrunt.hcl, terragrunt.stack.hcl or .terraform.lock.hcl is expected to be partial unit configurations that will be included by a Terragrunt unit.
Formatting HCL files
Section titled “Formatting HCL files”You can rewrite the HCL files to a canonical format using the hcl fmt command built into terragrunt. Similar to tofu fmt, this command applies a subset of the OpenTofu/Terraform language style conventions, along with other minor adjustments for readability.
By default, this command will recursively search for hcl files and format all of them for a given stack. Consider the following file structure:
Directoryroot
- root.hcl
Directoryprod
- terragrunt.hcl
Directorydev
- terragrunt.hcl
Directoryqa
- terragrunt.hcl
Directoryservices
- services.hcl
Directoryservice01
- terragrunt.hcl
If you run terragrunt hcl fmt at the root, this will update:
-
root/root.hcl -
root/prod/terragrunt.hcl -
root/dev/terragrunt.hcl -
root/qa/terragrunt.hcl -
root/qa/services/services.hcl -
root/qa/services/service01/terragrunt.hcl
To print the changes as a unified diff, pass --diff.
To check formatting without rewriting any file, pass --check. Terragrunt exits with status 1 if any file is unformatted, and 0 otherwise.
To format only some files, pass --filter with a path expression. A ! prefix excludes the paths it matches, e.g. to skip qa/services:
terragrunt hcl fmt --filter '!./qa/services/**'To format a single file, filter on its path:
terragrunt hcl fmt --filter './qa/services/services.hcl'