Skip to content

Dirs

Dirs can be used to describe which tag queries, workspaces, and when modified rules apply to a directory in your repository.

Configuration

Directories are configured in the .terrateam/config.yml file under the dirs key. Here’s the default configuration:

dirs:

Directory configuration

Each directory consists of the directory’s name as a key and a map as a value. For example, the directory foobar would have the following configuration:

dirs:
foobar:

The value map has the following attributes:

KeyTypeDescription
create_and_select_workspaceBooleanSelect and create the workspace defined in the workspaces configuration. Default is true.
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.

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']

When modified

The when_modified configuration can be set 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 assigning tags to a directory, tag values can be used in any combination to trigger workflows or target resources with a tag query.

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

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:

  • Tag ec2 with aws, ec2
  • Use workspace production when a tag query includes aws, ec2, production for a workflow or command.
  • Override when modified file patterns for directory ec2

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

Conclusion

By leveraging the dirs directive in your Terrateam configuration, you can:

  • Assign tags to specific directories
  • Configure workspaces for specific directories
  • Override When Modified rules for specific directories
  • Use globs to match multiple directories with similar configurations
  • Use the ${DIR} variable to reference the current directory in file patterns

This allows you to fine-tune the behavior of Terrateam for different parts of your repository, making it easier to manage complex Terraform setups with multiple directories and workspaces.

We use cookies and similar technologies to provide certain features, enhance the user experience and deliver content that is relevant to your interests. Depending on their purpose, analysis and marketing cookies may be used in addition to technically necessary cookies. By clicking on "Agree and continue", you declare your consent to the use of the aforementioned cookies. Here you can make detailed settings or revoke your consent (in part if necessary) with effect for the future. For further information, please refer to our Privacy Policy .