Skip to content

fmt

Usage

Recursively find HashiCorp Configuration Language (HCL) files and rewrite them into a canonical format.

Examples

Recursively format all HCL files in the current directory.

Terminal window
terragrunt hcl fmt

Flags

--checkoptional(bool)

Enable check mode in the `hcl fmt` command.

When enabled, Terragrunt will check if HCL files are properly formatted without making any changes. This is useful in CI/CD pipelines to ensure consistent formatting.

The command will:

  • Exit with status code 0 if files are properly formatted
  • Exit with status code 1 if any files need formatting

Example:

Terminal window
terragrunt hcl fmt --check

Environment Variables:

  • TG_CHECK
--diffoptional(bool)

Print diff between original and modified file versions when running `hcl fmt`.

When enabled, Terragrunt will show the differences between the original and formatted versions of HCL files. This helps you understand what changes the formatter would make.

Example:

Terminal window
terragrunt hcl fmt --diff

Environment Variables:

  • TG_DIFF
--exclude-diroptional(string)

Skip HCL formatting in given directories.

Specifies directories to exclude from HCL formatting. This is useful when you want to skip formatting certain parts of your codebase.

Example:

Terminal window
terragrunt hcl fmt --exclude-dir=vendor --exclude-dir=.terragrunt-cache

Environment Variables:

  • TG_EXCLUDE_DIR
--fileoptional(string)

The path to a single HCL file for the `hcl fmt` command to format.

Specifies a single HCL file to format instead of recursively searching for files. This is useful when you want to format just one specific file.

Example:

Terminal window
terragrunt hcl fmt --file=./environments/prod/terragrunt.hcl

Environment Variables:

  • TG_FILE
--filteroptional(list(string))

Filter configurations using a flexible query language

The --filter flag provides a path-based querying syntax for targeting specific HCL files to format.

Usage

Terminal window
terragrunt hcl fmt --filter './prod/**'

Path-Based Filtering

Match HCL files by their file system path:

Terminal window
# Relative paths with globs
terragrunt hcl fmt --filter './envs/prod/**'
# Format all HCL files in a specific directory
terragrunt hcl fmt --filter './modules/**/*.hcl'
# Absolute paths
terragrunt hcl fmt --filter '/absolute/path/to/envs/dev/apps/*'

Negation

Exclude paths using the ! prefix:

Terminal window
# Exclude by path
terragrunt hcl fmt --filter '!./prod/**'
# Exclude specific directories
terragrunt hcl fmt --filter '!./test/**'

Intersection (Refinement)

Use the | operator to refine results:

Terminal window
# Format HCL files in prod directory, excluding tests
terragrunt hcl fmt --filter './prod/** | !./prod/test/**'
# Format specific file patterns
terragrunt hcl fmt --filter './**/*.hcl | !./**/test/**'

Union (Multiple Filters)

Specify multiple --filter flags to combine results using OR logic:

Terminal window
# Format files in either dev or staging directories
terragrunt hcl fmt --filter './dev/**' --filter './staging/**'

For comprehensive examples and advanced usage patterns, see the Filters feature documentation.

Environment Variables:

  • TG_FILTER
--stdinoptional(bool)

Format HCL from stdin and print result to stdout.

When enabled, Terragrunt will read HCL content from standard input, format it, and write the result to standard output. This is useful for integrating with text editors or other tools.

Example:

Terminal window
echo 'locals { foo="bar" }' | terragrunt hcl fmt --stdin

With --check or --diff, Terragrunt does not print the formatted content. --check exits with status code 1 when the input needs formatting, and --diff prints a unified diff labeled old/stdin and new/stdin.

Environment Variables:

  • TG_STDIN
--parallelismoptional(integer)

Parallelism for --all commands.

Sets the maximum number of concurrent unit runs when running commands with --all or --graph. This helps control resource usage and API rate limits when working with multiple units.

By default, there is no limit, and every unit whose dependencies have finished starts immediately.

For details on how runs are scheduled and guidance on choosing a value, see Tuning Parallelism.

Environment Variables:

  • TG_PARALLELISM
--queue-exclude-diroptional(list(string))

Unix-style glob of directories to exclude from the queue of Units to run.

Environment Variables:

  • TG_QUEUE_EXCLUDE_DIR
--queue-exclude-externaloptional(bool)

Exclude external dependencies from the queue of Units to run.

Environment Variables:

  • TG_QUEUE_EXCLUDE_EXTERNAL
--queue-excludes-fileoptional(string, "…")

Path to a file with a list of directories that need to be excluded when running `run --all` commands.

Defaults to .terragrunt-excludes.

Environment Variables:

  • TG_QUEUE_EXCLUDES_FILE
--queue-ignore-dag-orderoptional(bool)

Ignore DAG order for --all commands.

When enabled, Terragrunt will ignore the dependency order when running commands with --all. This means units will be executed in arbitrary order, which can be useful for read-only operations like plan.

Note that this can lead to errors if used with commands that modify state, as dependencies might be processed in the wrong order.

To learn more about how to use this flag, see the Stacks feature documentation.

Environment Variables:

  • TG_QUEUE_IGNORE_DAG_ORDER
--queue-ignore-errorsoptional(bool)

Continue processing Units even if a dependency fails.

When enabled, Terragrunt will continue processing remaining units even if one fails. This can be useful when you want to see all potential errors in your configuration, rather than stopping at the first failure.

Note that this may lead to incomplete or inconsistent states if used with commands that modify infrastructure.

To learn more about how to use this flag, see the Stacks feature documentation.

Environment Variables:

  • TG_QUEUE_IGNORE_ERRORS
--queue-include-diroptional(list(string))

Unix-style glob of directories to include in the queue of Units to run.

Environment Variables:

  • TG_QUEUE_INCLUDE_DIR
--queue-include-externaloptional(bool)

Include external dependencies in the queue of Units to run.

Environment Variables:

  • TG_QUEUE_INCLUDE_EXTERNAL
--queue-include-units-readingoptional(string)

If flag is set, 'run --all' will only run the command against Terragrunt units that read the specified file via an HCL function or include.

Environment Variables:

  • TG_QUEUE_INCLUDE_UNITS_READING
--queue-strict-includeoptional(bool)

Only process the directories matched by --queue-include-dir.

Environment Variables:

  • TG_QUEUE_STRICT_INCLUDE