Skip to content

Directories and Globs

Directories can be used to describe which Tags, Workspaces, and When Modified rules apply to a directory in your repository.

Terrateam Configuration Terrateam behavior can be configured via a config.yml. This file is located in a directory named .terrateam at the root of your Terraform repository: .terrateam/config.yml.

See Configuration documentation for details.

Configuration

Top-level key: dirs

See Configuration Reference documentation for details.

<directory_name>

Each directory consists of the directory’s name as a key and a map as a value.

The value map has the following attributes:

KeyTypeDescription
tagsListList of tags to assign the directory.
workspacesWorkspacesWorkspace configuration.
when_modifiedWhen ModifiedConfiguration to override when to match pull request file changes with Autoplan and Autoapply.

When Modified

When Modified can be configured in the dirs directive. This configuration is identical to the global When Modified.

When specifying file_patterns for a directory, the directory is not automatically included in the list of patterns.

For example, the configuration below will only identify a change to dir1 if a file in the modules directory is changed:

dirs:
  dir1:
    when_modified:
      file_patterns: ['modules/*.tf']

The following configuration will identify a change to dir1 if a file in the dir1 or modules directory is changed:

dirs:
  dir1:
    when_modified:
      file_patterns: ['dir1/*.tf', 'modules/*.tf']

Assigning Tags

The tags configuration is used to create a custom tag for a directory.

When assignging Tags to a directory, tag values can be used in any combination to trigger Workflows or target resources with Commands.

dirs:
  ec2/us-east-1/production:
    tags: [ec2, us-east-1, production]
  ec2/us-west-1/production:
    tags: [ec2, us-west-1, production]

See Tags documentation for details.

Assigning Workspaces

The workspaces configuration is an object where the object key is the name of the Workspace and the value is its configuration.

Unique custom tags can be created against a directory and workspace combination.

dirs:
  dir1:
    workspaces:
      development:
        tags: ['dev']
      production:
        tags: ['prod']

Example

dirs:
  ec2:
    tags: [aws, ec2]
    workspaces:
      production:
        tags: [production]
    when_modified:
      file_patterns: ["ec2/*.tf", "ec2/*.tfvars", "iam/*.tf", "iam/*.tfvars"]
  iam:
    tags: [aws, iam]

Description:

The ${DIR} variable

File globs specified in file_patterns are always relative to the root of the repository. The internal ${DIR} variable can be used to specify the directory that Terrateam is working against. For example, the following configuration would trigger Terrateam operations when foobar/*.tf is matched in a pull request. The ${DIR} variable can be used against directories specified by an exact path or glob.

dirs:
  foobar:
    when_modified:
      file_patterns: ["${DIR}/*.tf"]

Globs

In addition to specifying absolute path names, the dirs directive supports glob patterns as well. This can be useful for repositories with a lot of directories that match a pattern with a similar configuration.

The Terrateam runtime environment will list all of the files in your repository and match them according to the directory glob pattern. A dirs directive is then constructed from matched configurations as if the user had specified all of them by hand.

Example

Consider a repository with the following files:

  • _templates/ec2/terragrunt.hcl
  • prod/ec2/us-east-1/foo.tf
  • prod/ec2/us-west-1/foo.tf
  • prod/ebs/us-east-1/foo.tf
  • prod/ebs/us-west-1/foo.tf

A .terrateam/config.yml configuration file:

dirs:
  _templates/**:
    when_modified:
      file_patterns: []
  prod/**/ec2/**:
    tags: [prod, ec2]
    when_modified:
      file_patterns: ["_templates/**/*.tf", "${DIR}/*.tf"]
  prod/**:
    tags: [prod]
    when_modified:
      file_patterns: ["_templates/**/*.tf", "${DIR}/*.tf"]

Given the file list, the following dirs directive will be automatically generated during every Terrateam operation:

dirs:
  _templates/ec2:
    when_modified:
      file_patterns: []
  prod/ec2/us-east-1:
    tags: [prod, ec2]
    when_modified:
      file_patterns: ["_templates/**/*.tf", "prod/ec2/us-east-1/*.tf"]
  prod/ec2/us-west-1:
    tags: [prod, ec2]
    when_modified:
      file_patterns: ["_templates/**/*.tf", "prod/ec2/us-west-1/*.tf"]
  prod/ebs/us-east-1:
    tags: [prod]
    when_modified:
      file_patterns: ["_templates/**/*.tf", "prod/ebs/us-east-1/*.tf"]
  prod/ebs/us-west-1:
    tags: [prod]
    when_modified:
      file_patterns: ["_templates/**/*.tf", "prod/ebs/us-west-1/*.tf"]

Longest Glob Match

In the example above, the files in the prod/ec2 directory match two globs:

  • prod/**/ec2/**
  • prod/**

The glob prod/**/ec2/** is longer than prod/** and is considered the better match because the glob is more specific.

Directory Globs Match Files (Terragrunt)

Globs can be expressed all the way to the file level. The directory of the file is then taken when constructing the dirs directory.

For example, in a repository with a structure to be used with Terragrunt, one could have the following configuration:

dirs:
  _templates/**/terragrunt.hcl:
    when_modified:
      file_patterns: []
  "**/terragrunt.hcl":
    tags: [terragrunt]
    when_modified:
      file_patterns: ['_templates/**/terragrunt.hcl', '${DIR}/*.hcl', '${DIR}/*.tf', '${DIR}/*.tfvars']

This configuration would apply the following rules:

  • Lines 2-4: Disable Terrateam operations in any directory under the _templates/ directory with a terragrunt.hcl file
  • Lines 5-8: Run Terrateam operations against a directory when:
    • The directory contains a terragrunt.hcl file
    • The pull request changed files end in hcl, tf, or tfvars
    • The pull request changed files includes a terragrunt.hcl file in the _templates/ directory