Skip to content

Overview

The --filter flag provides a sophisticated querying syntax for targeting specific units and stacks in Terragrunt commands. This unified approach offers powerful filtering capabilities using a flexible query language.

The filter syntax allows you to target units and stacks using several different approaches. Usage of the filter flag in a command can look like this:

Terminal window
$ terragrunt find --filter './prod/** | name=web'
prod/services/web

Where the result of find above might have been:

Terminal window
$ terragrunt find
prod/services/web
prod/services/api
prod/data/db
dev/services/web
dev/services/api
dev/data/db

For the following file tree:

  • Directoryprod
    • Directoryservices
      • Directoryweb <— Matched by the filter
        • terragrunt.hcl
      • Directoryapi
        • terragrunt.hcl
    • Directorydata
      • Directorydb
        • terragrunt.hcl
  • Directorydev
    • Directoryservices
      • Directoryweb
        • terragrunt.hcl
      • Directoryapi
        • terragrunt.hcl
    • Directorydata
      • Directorydb
        • terragrunt.hcl

There are several different types of filter expressions, and particular ways in which they can be combined to achieve different results. You can learn more about that below.

Filter TypeDescription
NameMatch units and stacks by their name.
PathMatch units and stacks by their file system path.
AttributeMatch units and stacks by their configuration attributes.
NegatedExclude units and stacks using the ! prefix.
IntersectionUse the | operator to refine results.
UnionCombine filter results using multiple --filter flags.
GraphFilter units based on their dependency relationships using graph traversal operators.
GitFilter units and stacks based on Git diffs using Git expressions.

The following commands all support the --filter flag using the same filtering syntax (note the section below on special interactions):

This flag is intended to be a flexible way to target specific infrastructure that allows you to dry-run infrastructure targeting using discovery commands (like find and list) before running a command that actually affects infrastructure (like run).

The --filter flag provides a unified alternative to multiple queue control flags.

Legacy FlagFilter Equivalent
--queue-include-dir=./path--filter='./path'
--queue-exclude-dir=./path--filter='!./path'
--queue-include-external--filter='{./**}...'
--queue-include-units-reading=shared.hcl--filter='reading=shared.hcl'

Certain commands have special interactions with the --filter flag that are worth noting.

Unlike when used for most commands, the --filter flag is used to filter on individual HCL files when used with the hcl fmt.

All other commands use --filter to filter on units and/or stacks (which are directories). As a result, only path-based filter expressions are supported. Attribute-based filters like type=unit or name=my-app are not applicable to file-level operations.

Example:

Terminal window
# Supported: Path-based expressions
terragrunt hcl fmt --filter './prod/**/*.hcl'
# Not supported: Attribute-based expressions
terragrunt hcl fmt --filter 'type=unit' # This will not work

When using --filter with stack generate, filter expressions will only be recognized if they explicitly target stacks. This is to ensure that filters are not over-applied, preventing any stack generation from occurring.

Terminal window
# Supported: Only generate the stacks that match the filter, as we are explicitly indicating that we are targeting stacks.
terragrunt stack generate --filter 'name=prod | type=stack'
# Not supported: This filter will be ignored, as we are not explicitly indicating that we are targeting stacks.
terragrunt stack generate --filter 'name=prod' # This will not work

The reason for this is that stack generation can also be done automatically as part of other commands, like run, and thus we need to make it clear that we’re trying to control stack generation rather than run behavior.

Terminal window
# This will run any unit named 'vpc'
terragrunt run --all --filter 'vpc' -- plan
# This will run any unit named 'vpc', and prevent stack generation in any stack not named 'dev' (including any stacks named 'vpc')
terragrunt stack run --filter 'vpc' --filter 'name=dev | type=stack' -- apply