Skip to content

validate

Usage

Recursively find HashiCorp Configuration Language (HCL) files and validate them.

Examples

Discover all HCL files in the current directory, and validate them.

Terminal window
terragrunt hcl validate

Flags

--filteroptional(list(string))

Filter configurations using a flexible query language

The --filter flag provides a sophisticated querying syntax for targeting specific units and stacks in Terragrunt commands.

Usage

Terminal window
terragrunt find --filter 'app*'
terragrunt list --filter './prod/** | type=unit'
terragrunt run --all --filter './prod/**' -- plan
terragrunt hcl fmt --filter './prod/**'
terragrunt hcl validate --filter 'type=unit'

Name-Based Filtering

Match configurations by their name using exact matches or glob patterns:

Terminal window
# Exact match
terragrunt find --filter app1
# Glob pattern
terragrunt find --filter 'app*'

Path-Based Filtering

Match configurations by their file system path:

Terminal window
# Relative paths with globs
terragrunt find --filter './envs/prod/**'
# Absolute paths
terragrunt find --filter '/absolute/path/to/envs/dev/apps/*'

Attribute-Based Filtering

Match configurations by their configuration attributes:

Terminal window
# Filter by type
terragrunt find --filter 'type=unit'
terragrunt find --filter 'type=stack'
# Filter by external dependency status
terragrunt find --filter 'external=false'
# Filter by files read
terragrunt find --filter 'reading=shared.hcl'
terragrunt find --filter 'reading=common/*.hcl' # Globs supported!
terragrunt find --filter 'reading=config/**' # Double-wildcard globs are required filtering on files nested in subdirectories.
terragrunt find --filter 'reading=config/vars.tfvars'

Negation

Exclude configurations using the ! prefix:

Terminal window
# Exclude by name
terragrunt find --filter '!app1'
# Exclude by path
terragrunt find --filter '!./prod/**'

Intersection (Refinement)

Use the | operator to refine results:

Terminal window
# Find all units in prod directory
terragrunt find --filter './prod/** | type=unit'
# Chain multiple filters
terragrunt find --filter './dev/** | type=unit | !name=unit1'

Git-Based Filtering

Filter configurations based on changes between Git references. For the common use case of comparing the default branch with HEAD, you can use the --filter-affected flag as a convenient shorthand:

Terminal window
# Find components affected by changes between main and HEAD
terragrunt find --filter-affected

For more control, use Git-based filter expressions directly:

Terminal window
# Compare between two references
terragrunt find --filter '[main...HEAD]'
# Shorthand: compare reference to HEAD
terragrunt find --filter '[main]'
# Compare between specific commits
terragrunt find --filter '[abc123...def456]'

For more details and examples, see the Filters feature documentation.

Graph-Based Filtering

Filter configurations based on dependency relationships using graph traversal. Use ellipsis (...) to traverse the dependency graph and caret (^) to exclude the target from results.

Syntax variants:

  • foo... - Include target and all dependencies (things it depends on)
  • ...foo - Include target and all dependents (things that depend on it)
  • ...foo... - Include target, dependencies, and dependents
  • ^foo... - Include only dependencies (exclude target)
  • ...^foo - Include only dependents (exclude target)
  • ...^foo... - Include dependencies and dependents (exclude target)
Terminal window
# Find 'service' and everything it depends on
terragrunt find --filter 'service...'
# Find 'vpc' and everything that depends on it
terragrunt find --filter '...vpc'
# Find complete dependency graph for 'db'
terragrunt find --filter '...db...'
# Find dependencies of 'service' but exclude 'service' itself
terragrunt find --filter '^service...'
# Combine graph traversal with path filters (note the use of braces to escape the path)
terragrunt find --filter '{./apps/service}...'
# Combine graph traversal with attribute filters
terragrunt find --filter '...type=unit'
# Refine graph results with intersection
terragrunt find --filter 'service... | external=false'

Union (Multiple Filters)

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

Terminal window
# Find components named 'unit1' OR 'stack1'
terragrunt find --filter unit1 --filter stack1

The filters file

Instead of specifying filters on the command line, you can store filter queries in a file. By default, Terragrunt automatically reads filter queries from the .terragrunt-filters file in your current working directory if it exists.

Terminal window
# Automatically reads .terragrunt-filters (no flag needed)
terragrunt find
# Use a custom filters file
terragrunt find --filters-file custom-filters.txt
# Disable automatic file reading
terragrunt find --no-filters-file

The filters file should contain one filter query per line. Empty lines and lines starting with # are ignored:

# Production environment filters
type=unit
./prod/**
# Exclude test units
!name=test-*

Supported Commands

Learn More

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

Environment Variables:

  • TG_FILTER
--discovery-boundaryoptional(string)

Directory that encloses graph discovery for filters, instead of the git repository root

Filter expressions that traverse the dependency graph reach beyond the working directory. Dependents (...{unit}) can live anywhere, so Terragrunt searches from the working directory up to the git repository root; dependencies ({unit}...) are declared by path and can point anywhere.

The --discovery-boundary flag is a single enclosure for that traversal: it replaces the git repository root as the outer limit, and a configuration that traversal reaches outside it is not returned, so find does not list it and run --all does not run it. Configurations inside the boundary are discovered as usual.

The boundary decides which configurations a command acts on, not which ones Terragrunt may read. A unit inside the boundary can declare a dependency outside it, and that dependency is still read and parsed: Terragrunt needs its configuration to fetch the outputs the dependent unit consumes, and it needs the dependency edge to order the units that do run. What the boundary withholds is the unit itself, which is left for a separate run to apply, the same way a dependency outside the working directory is treated today.

This matters in monorepos where environments are isolated from each other. When sibling environments cannot be parsed independently, the default search fails or wastes work reaching into them:

environments/
staging/
production/
test/
root.hcl

Bounding the traversal to the current environment keeps discovery inside it:

Terminal window
cd environments/staging
terragrunt find --filter '...{vpc}' --discovery-boundary .

Usage

Terminal window
# Enclose discovery within the working directory
terragrunt find --filter '...{vpc}' --discovery-boundary .
# Enclose discovery within a parent directory
terragrunt run --all plan --filter '...{vpc}' --discovery-boundary ..
# Equivalent, via environment variable
TG_DISCOVERY_BOUNDARY=. terragrunt find --filter '...{vpc}'
# From the repository root, keep app's dependencies within prod
terragrunt find --filter '{./prod/app}...' --discovery-boundary ./prod

The boundary must be an existing directory. Relative paths are resolved against the working directory.

The boundary applies to what traversal reaches, so a command that names no filter keeps every configuration under the working directory even when the boundary sits below it. Narrow the working directory itself, with --working-dir, to change where discovery starts.

Where the boundary may sit depends on the directions the filters traverse. Dependent traversal searches from the working directory, so filters that traverse dependents require the boundary to be the working directory, one of its parents, or a directory inside it. A boundary inside the working directory starts the search there. Terragrunt rejects any other boundary, because it can never take effect. Dependency traversal follows declared paths outward from the units a filter matched and never consults the working directory, so filters that traverse only dependencies accept any directory, including one below the working directory. The inline (dir) operand follows the same rule.

Git-based expressions are the exception. For the units Terragrunt finds in the compared commits, a relative boundary resolves against the repository root, whether it comes from an inline (dir) operand or from --discovery-boundary. It can be any directory in the repository, and it must be a directory in at least one of the compared commits. A boundary at or above the repository root covers the whole commit. The flag only narrows which changed units Terragrunt finds when the filter traverses dependents. The working-directory walk still resolves the flag against the working directory, so run from the repository root to make both mean the same directory.

Environment Variables:

  • TG_DISCOVERY_BOUNDARY
--jsonoptional(bool)

Output the result in JSON format.

When enabled, Terragrunt will output the validation results in JSON format, making it easier to parse and process programmatically.

Example:

Terminal window
terragrunt hcl validate --json

Environment Variables:

  • TG_JSON
--show-config-pathoptional(bool)

Show a list of files with invalid configuration.

When enabled, Terragrunt will display the full paths of files that contain invalid HCL configurations. This is particularly useful when validating multiple files to quickly identify which files need attention.

Example:

Terminal window
terragrunt hcl validate --show-config-path

Environment Variables:

  • TG_SHOW_CONFIG_PATH
--inputsoptional(bool)

Validate that all variables a module requires are set.

When enabled, Terragrunt will validate that all variables a module (provisioned by a unit) requires are set.

Example:

Terminal window
terragrunt hcl validate --inputs

Environment Variables:

  • TG_INPUTS
--outputsoptional(bool)

Check the outputs each unit declares against its module and against what its dependents read.

When enabled, Terragrunt checks the outputs block of every unit: each declared output has to exist in the OpenTofu/Terraform module the unit runs, and each dependency.<name>.outputs.<output> reference has to name an output its dependency declares. The check fetches the terraform.source of a unit that declares outputs, so it takes longer than a plain hcl validate.

Example:

Terminal window
terragrunt hcl validate --outputs

Environment Variables:

  • TG_OUTPUTS
--strictoptional(bool)

Throw an error if any inputs are set that are not defined in the module that a unit provisions.

When enabled, Terragrunt will throw an error if any inputs are set that are not defined in the module that a unit provisions.

Example:

Terminal window
terragrunt hcl validate --inputs --strict

Environment Variables:

  • TG_STRICT
--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