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.
Filter Syntax Overview
Section titled “Filter Syntax Overview”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:
$ terragrunt find --filter './prod/** | name=web'prod/services/webWhere the result of find above might have been:
$ terragrunt findprod/services/webprod/services/apiprod/data/dbdev/services/webdev/services/apidev/data/dbFor 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
Filter Expressions
Section titled “Filter Expressions”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 Type | Description |
|---|---|
| Name | Match units and stacks by their name. |
| Path | Match units and stacks by their file system path. |
| Attribute | Match units and stacks by their configuration attributes. |
| Negated | Exclude units and stacks using the ! prefix. |
| Intersection | Use the | operator to refine results. |
| Union | Combine filter results using multiple --filter flags. |
| Graph | Filter units based on their dependency relationships using graph traversal operators. |
| Git | Filter units and stacks based on Git diffs using Git expressions. |
Usage with Commands
Section titled “Usage with Commands”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).
Comparison with Queue Control Flags
Section titled “Comparison with Queue Control Flags”The --filter flag provides a unified alternative to multiple queue control flags.
| Legacy Flag | Filter 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' |
Special Interactions
Section titled “Special Interactions”Certain commands have special interactions with the --filter flag that are worth noting.
hcl fmt
Section titled “hcl fmt”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:
# Supported: Path-based expressionsterragrunt hcl fmt --filter './prod/**/*.hcl'
# Not supported: Attribute-based expressionsterragrunt hcl fmt --filter 'type=unit' # This will not workstack generate
Section titled “stack generate”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.
# 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 workThe 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.
# 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