Skip to content

Multiple Environments

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.

There are many ways to organize your Terraform repository for use with multiple environments. In this tutorial, we’ll cover the use of separate directory and workspace combinations along with separate .tfvars files for each environment.

Multiple environments per directory and workspace

Create directory and workspace Tags to be used with Tag Queries.

dirs:
ec2:
tags: [ec2]
workspaces:
development:
tags: [development]
production:
tags: [production]

There are many ways to organize your Terraform repository for use with multiple environments. In this tutorial, we’ll cover the use of separate .tfvars file for each environment.

Consider the following scenario:

  • A single repository named terraform with Terraform code
  • Two environments: qa and production

Multiple environments with tfvars

Shared Terraform state

Trigger Terrateam operations against each environment using a shared Terraform state file.

Terraform directory structure

Terminal window
.
└── aws
├── main.tf
├── production.tfvars
└── qa.tfvars
1 directory, 3 files

Backend configuration

Define a single Terraform state file with the local backend.

main.tf

terraform {
backend "local" {
path = "terraform.tfstate"
}
}
resource "null_resource" "foobar" {
}

Terrateam configuration file

.terrateam/config.yml

when_modified:
autoplan: false
dirs:
aws:
create_and_select_workspace: false
tags: [aws]
workspaces:
qa:
tags: [qa]
production:
tags: [production]
workflows:
- tag_query: aws qa
plan:
- type: init
- type: plan
extra_args: ["-var-file=qa.tfvars"]
- tag_query: aws production
plan:
- type: init
- type: plan
extra_args: ["-var-file=production.tfvars"]

Pull Request

With the above configuration, when creating a new pull request with a Terraform change against the aws directory, Terrateam won’t take any action because autoplan is set to false.

Triggers

Plan and Apply qa

To trigger a Plan for the qa environment against the aws directory, comment the following:

terrateam plan aws qa

To trigger an Apply for the qa environment against the aws directory, comment the following:

terrateam apply aws qa
Plan and Apply production

To trigger a Plan for the production environment against the aws directory, comment the following:

terrateam plan aws production

To trigger an Apply for the production environment against the aws directory, comment the following:

terrateam apply aws production

Separate Terraform state

Trigger Terrateam operations against each environment using separate Terraform state files.

Terraform directory structure

Terminal window
.
└── aws
├── backend-production.conf
├── backend-qa.conf
├── main.tf
├── production.tfvars
└── qa.tfvars
1 directory, 5 files

Backend configuration

Define Terraform state with separate backend configuration files to be merged with what is in the main.tf backend block. This merge happens on a terraform init execution during Terrateam operations.

main.tf

terraform {
backend "local" {
}
}
resource "null_resource" "foobar" {
}

backend-qa.conf

path = "qa.tfstate"

backend-production.conf

path = "production.tfstate"

Terrateam configuration file

.terrateam/config.yml

when_modified:
autoplan: true
dirs:
aws:
create_and_select_workspace: false
tags: [aws]
workspaces:
qa:
tags: [qa]
prod:
tags: [production]
workflows:
- tag_query: aws qa
plan:
- type: init
extra_args: ["-backend-config=backend-qa.conf"]
- type: plan
extra_args: ["-var-file=qa.tfvars"]
apply:
- type: init
extra_args: ["-backend-config=backend-qa.conf"]
- type: apply
- tag_query: aws production
plan:
- type: init
extra_args: ["-backend-config=backend-production.conf"]
- type: plan
extra_args: ["-var-file=production.tfvars"]
apply:
- type: init
extra_args: ["-backend-config=backend-production.conf"]
- type: apply

Pull Request

With the above configuration, when creating a new pull request with a Terraform change against the aws directory, Terrateam will automatically trigger two Plan operations. One for the qa environment and one for the production environment. Both with their respective backend-config and var-file arguments.

Triggers

Plan qa and production

To trigger a Plan for the qa environment against the aws directory, comment the following:

terrateam plan aws qa

To trigger a Plan for the production environment against the aws directory, comment the following:

terrateam plan aws production

To trigger a Plan for the qa and production environment against the aws directory, comment the following:

terrateam plan aws
Apply qa and production

To trigger an Apply for the qa environment against the aws directory, comment the following:

terrateam apply aws qa

To trigger an Apply for the production environment against the aws directory, comment the following:

terrateam apply aws production
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 .