Skip to content

Graph Expressions

Filter units and stacks based on their dependency relationships using graph traversal operators. This allows you to find components that depend on a target, or components that a target depends on.

Graph-based expressions use the ellipsis (...) operator to indicate graph traversal direction and the caret (^) operator to exclude the target from results.

Use ... after a target expression to include the target and all of its dependencies:

Terminal window
# Find 'service' and everything it depends on
terragrunt find --filter 'service...'
  • Directory.
    • Directoryservice <— Matched (target)
      • terragrunt.hcl (depends on: db, cache, vpc)
    • Directorydb <— Matched (dependency of service)
      • terragrunt.hcl (depends on: vpc)
    • Directorycache <— Matched (dependency of service)
      • terragrunt.hcl (depends on: vpc)
    • Directoryvpc <— Matched (dependency of service, db, cache)
      • terragrunt.hcl

Use ... before a target expression to include the target and all components that depend on it:

Terminal window
# Find 'vpc' and everything that depends on it
terragrunt find --filter '...vpc'
  • Directory.
    • Directoryvpc <— Matched (target)
      • terragrunt.hcl
    • Directorydb <— Matched (depends on vpc)
      • terragrunt.hcl (depends on: vpc)
    • Directorycache <— Matched (depends on vpc)
      • terragrunt.hcl (depends on: vpc)
    • Directoryservice <— Matched (depends on vpc via db and cache)
      • terragrunt.hcl (depends on: db, cache)

Use ... before and after a target expression to include a target, all its dependencies, and all its dependents:

Terminal window
# Find 'db' and its complete dependency graph
terragrunt find --filter '...db...'
  • Directory.
    • Directoryvpc <— Matched (dependency of db)
      • terragrunt.hcl
    • Directorydb <— Matched (target)
      • terragrunt.hcl (depends on: vpc)
    • Directoryservice <— Matched (depends on db)
      • terragrunt.hcl (depends on: db, cache)

Use ^ before a target expression to exclude the target from results. This is useful when you want only the dependencies or dependents, but not the target itself:

Terminal window
# Find all dependents of 'vpc' but exclude 'vpc' itself
terragrunt find --filter '...^vpc'
  • Directory.
    • Directoryvpc <— Not matched (due to ’^’ operator)
      • terragrunt.hcl
    • Directorydb <— Matched (depends on vpc)
      • terragrunt.hcl (depends on: vpc)
    • Directorycache <— Matched (depends on vpc)
      • terragrunt.hcl (depends on: vpc)
    • Directoryservice <— Matched (depends on vpc via db and cache)
      • terragrunt.hcl (depends on: db, cache)

You can limit how many levels of dependencies or dependents to traverse by adding a numeric depth before or after the ellipsis (...) operator. This is useful when you only want immediate or nearby relationships rather than the full transitive closure.

Terminal window
# Find 'service' and only its direct dependencies (1 level deep)
terragrunt find --filter 'service...1'
# Find 'vpc' and only components that directly depend on it (1 level)
terragrunt find --filter '1...vpc'
# Find 'db' with 2 levels of dependencies and 1 level of dependents
terragrunt find --filter '1...db...2'

Given this dependency graph where service depends on db and cache, which both depend on vpc:

  • Directory.
    • Directoryvpc
      • terragrunt.hcl
    • Directorydb
      • terragrunt.hcl (depends on: vpc)
    • Directorycache
      • terragrunt.hcl (depends on: vpc)
    • Directoryservice
      • terragrunt.hcl (depends on: db, cache)

Using service...1 (dependencies with depth 1):

  • Directory.
    • Directoryvpc <— Not matched (2 hops away, beyond depth limit)
      • terragrunt.hcl
    • Directorydb <— Matched (1 hop from service)
      • terragrunt.hcl (depends on: vpc)
    • Directorycache <— Matched (1 hop from service)
      • terragrunt.hcl (depends on: vpc)
    • Directoryservice <— Matched (target)
      • terragrunt.hcl (depends on: db, cache)

The search for dependents runs from the working directory up to the Git repository root, and dependencies are followed wherever their config_path points. To keep either direction inside a directory, write that directory in parentheses where a depth would go:

Terminal window
# Find 'vpc' and its dependents inside the working directory
terragrunt find --filter '(.)...vpc'
# Find 'app' and its dependencies inside the working directory
terragrunt find --filter 'app...(.)'
# Bound each direction separately
terragrunt find --filter '(..)...vpc...(.)'

Given this layout, with environments/staging as the working directory:

  • Directoryenvironments
    • Directorystaging
      • Directoryvpc
        • terragrunt.hcl
      • Directoryapp
        • terragrunt.hcl (depends on: vpc)
    • Directoryproduction
      • Directoryvpc
        • terragrunt.hcl
      • Directoryapp
        • terragrunt.hcl (depends on: vpc)

Using (.)...vpc:

  • Directoryenvironments
    • Directorystaging
      • Directoryvpc <— Matched (target)
        • terragrunt.hcl
      • Directoryapp <— Matched (depends on vpc)
        • terragrunt.hcl (depends on: vpc)
    • Directoryproduction
      • Directoryvpc <— Not parsed (outside the boundary)
        • terragrunt.hcl
      • Directoryapp <— Not parsed (outside the boundary)
        • terragrunt.hcl (depends on: vpc)

Without the boundary, the search for dependents would climb to the repository root and parse production/vpc and production/app to check whether either depends on staging/vpc. With (.), Terragrunt reads nothing under production.

The boundary must be an existing directory. Terragrunt resolves a relative path against the working directory. A dependent-side boundary must be the working directory, one of its parents, or a directory inside it. The search for dependents starts at the working directory, or at the boundary when the boundary is inside the working directory. A dependency-side boundary can be any directory.

For Git-based expressions, Terragrunt resolves a relative boundary against the repository root instead. (./live/staging)...[main...HEAD] means live/staging from any directory. The boundary does not have to contain the working directory. It has to exist in at least one of the compared commits, or Terragrunt returns an error. Terragrunt ignores a changed unit outside a dependent-side boundary in both commits. A dependency-side boundary only limits dependency traversal, so it never hides a changed unit.

Traversal stops at the boundary, so a unit inside the boundary that is reachable only through a unit outside it is never reached. Here, foo depends on bar, and bar depends on baz. Using foo...(.) with inside as the working directory:

  • Directoryinside
    • Directoryfoo <— Matched (target)
      • terragrunt.hcl (depends on: ../../outside/bar)
    • Directorybaz <— Not matched (reachable only through bar)
      • terragrunt.hcl
  • Directoryoutside
    • Directorybar <— Not matched (outside the boundary)
      • terragrunt.hcl (depends on: ../../inside/baz)

The operand reserves ( and ), so wrap a name or path that contains them in braces (e.g. {./weird(name)}).

The --discovery-boundary flag sets one boundary for every --filter expression on the command, in both directions. A (dir) operand overrides the flag for the expression it appears in:

Terminal window
# Dependents of 'vpc' stay inside the working directory
terragrunt find --filter '...vpc' --discovery-boundary .
# The operand extends this expression's search for dependents to production
terragrunt find --filter '(..)...vpc' --discovery-boundary .