Docs
Common Use Cases
Multi-Environment
Multi-Environment by Tfvars

Multi-Environment by Tfvars

It's possible to manage multiple environments using separate Tfvars files.

Consider the following scenario:

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

Shared Terraform state

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

Terraform directory structure

.
└── 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" {
}

This is just an example. The local backend is primarily used for development use cases.

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

.
└── 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"

This is just an example. The local backend is primarily used for development use cases.

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"]
  - tag_query: aws production
    plan:
      - type: init
        extra_args: ["-backend-config=backend-production.conf"]
      - 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 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