DevOps Classroomnotes 30/Dec/2021

Terraform Workspaces

  • Generally when we write terraform configuration, we would like to create multiple environments such as
  • Developer
  • System Test
  • Performance Test
  • Staging
  • Production
  • For all the environments our infrastructure is same, the arguments might differ, There might be some additional resources in some environments
  • To handle this we would not creat multiple copies of infrastructure and create different backend configurations.
  • To deal with single template and multi environments terraform introduced a concept called workspaces. Refer Here for the official docs
  • Now lets explore terraform workspace cli
  • Lets try to create two workspaces
  • ST => System Test
  • UAT => Staging
  • Refer Here for some of the changes done
  • Refer Here for the fixes done to the output variable
  • Select the current workspace as ST
terraform workspace select ST
terraform apply -var-file="env/ST.tfvars"
  • Now lets select the current workspace as UAT
terraform workspace select UAT
terraform apply -var-file="env/UAT.tfvars"
  • Refer Here for some of the changes done to accomodate multi environments
  • To set the current workspace we are using terraform workspace select, the other way of doing SET TF_WORKSPACE to the active environment
  • Other environmental variables of terraform Refer Here
  • Exercise: Try to create all the tag with with env-<name>
tags = {
    Name = format("%s-WebSg", terraform.workspace),
    Env = terraform.workspace
  • From CI/CD Engines when we need manage Terraform infra creation. From the Jenkins/Azure DevOps pipelines, You will have an agent/node where terraform is installed and credentials configured/passed
# if the terraform code is available in some git try to configure the git to clone 
terraform init
terraform workspace select <build-parameter>
terraform apply -var-file=<varfile> -auto-approve
  • Next Steps:
  • Reusable Templates => Modules => Registry
  • Creating a Module and using community module

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

About continuous learner

devops & cloud enthusiastic learner