Terraform Workspace – Manage Multiple Environments

In this article, explore how Terraform Workspaces empower you to seamlessly manage multiple environments, enhancing your infrastructure deployment workflow.

Create a MySQL RDS Database Instance with Terraform 17

What is Terraform?

1280px Terraform Logo.svg removebg preview 1
  • It is an open-source IaaC (Infrastructure as a code) software tool where you define and create resources using providers in the declarative configuration language example JSON.
  • With Terraform, You can package and reuse the code in the form of modules.
  • It supports a number of cloud infrastructure providers such as AWS, Azure, GCP, IBM Cloud, OCI, etc.

What is Terraform Workspaces?

Terraform worksapces is a very logical concept where you can have multiple states of your infrastructure configuration. To put this in simple words if you are running an infrastructure configuration in development environment then the same infrastructure can be run in the production environment.

The main benefit of terraforming workspaces we get is we can have more than one state associated with a single terraform configuration.

If you have not defined any workspace then there is always a default workspace created by terraform, so you always work in a default workspace of terraform. You can list the number of terraform workspaces by running the command terraform workspace show. Also, you can not delete the default workspace

image 14

Applying Terraform Workspaces Across Environments

  1. Development Environment:
    • Purpose: Customize modules for rapid development and testing.
    • Example: In the development environment, use smaller instance types and enable debugging features for faster iterations.
  2. Staging Environment:
    • Purpose: Test integration with realistic configurations.
    • Example: In the staging environment, adjust configurations for larger instance types and use more realistic resource settings.
  3. Production Environment:
    • Purpose: Emphasize high availability, scaling, and security.
    • Example: In the production environment, configure modules for larger instance types, auto-scaling groups, and stringent security measures.

Benefits of Using Terraform Workspaces

  1. Isolation of Environments:
    • Scenario: When managing development, testing, and production environments.
    • Benefit: Terraform Workspaces allow for the creation of isolated environments, ensuring that changes made in one workspace do not impact configurations in others. This isolation minimizes the risk of unintended consequences during development and deployment.
  2. Efficient State Management:
    • Scenario: Simultaneous development efforts across different branches or features.
    • Benefit: Each Terraform Workspace maintains a separate state file, preventing conflicts when multiple developers or teams are working concurrently. This ensures that changes in one workspace do not interfere with the state of another, facilitating parallel development and testing.
  3. Simplified Configuration Switching:
    • Scenario: Testing and validating configurations across various environments.
    • Benefit: Terraform Workspaces make it easy to switch between different environments, allowing developers and operators to quickly test and validate changes. This simplifies the process of adapting configurations for different scenarios, such as development, staging, and production.
  4. Parallel Development and Deployment:
    • Scenario: Development of features or fixes in parallel without impacting the main codebase.
    • Benefit: Workspaces enable parallel development by allowing separate instances of the same infrastructure to be managed independently. This feature is particularly valuable in scenarios where different teams or developers are working on distinct features or changes simultaneously.
  5. Customization of Configurations:
    • Scenario: Tailoring configurations for specific use cases or requirements.
    • Benefit: Each workspace can have its own set of variables, allowing for customization of configurations based on specific needs. This flexibility enables the adaptation of infrastructure settings, such as scaling parameters or resource configurations, for different environments without affecting the main codebase.

Terraform Workspaces provide a versatile and efficient approach to managing multiple environments, offering a range of benefits that contribute to a more organized, secure, and scalable infrastructure deployment process.

Potential Drawbacks of Using Terraform Workspaces

Codebase Complexity:

  • Example: Growing complexity in the Terraform codebase as the number of workspaces increases, potentially leading to difficulties in maintaining consistency.

Limited to Remote Backends:

  • Example: Dependency on remote backends for optimal functionality, limiting flexibility for teams restricted to local backends.

Potential for Misconfiguration:

  • Example: Risk of misconfigurations and unintended changes when switching between workspaces frequently.

Increased Learning Curve:

  • Example: Introduction of additional concepts may contribute to a steeper learning curve, especially for teams new to Terraform or infrastructure as code.

Limited Resource Support:

  • Example: Some resources may exhibit limitations or unexpected behavior when used across multiple workspaces, necessitating careful consideration.

State File Management Challenges:

  • Example: Managing state files becomes critical, and accidental modifications or deletions can result in issues across environments.

Dependency on Terraform Versions:

  • Example: Workspaces may behave differently across Terraform versions, requiring careful consideration when upgrading or downgrading.

Potential for Workspace Proliferation:

  • Example: Risk of creating numerous workspaces without clear guidelines, leading to confusion and increased operational overhead.

Mitigating these drawbacks involves establishing best practices, comprehensive documentation, and ongoing training to ensure effective and efficient use of Terraform Workspaces.

Terraform workspace vs. Terraform module

Terraform workspaces and Terraform modules are two different concepts that serve different purposes in the Terraform ecosystem. Workspaces allow users to manage different sets of infrastructure using the same configuration by isolating state files. Modules, on the other hand, are a logical container for multiple resources that are used together, facilitating reusability and better organization of your code.

Steps for using Terraform Workspaces

Structure of our Folder:

Screenshot 2023 12 21 141214

Step1: Create a New Terraform Workspace

To initiate a new Terraform workspace, ensure that Terraform is installed and execute the following command:

terraform workspace new dev
Screenshot 2023 12 21 132131

Points to note:

  • Utilize the keyword workspace after terraform.
  • Specify new to create a new workspace.
  • Choose a unique workspace name, as demonstrated with “dev.”

For additional workspaces, use a similar command, replacing “test” as needed:

terraform workspace new test
Screenshot 2023 12 21 132154

Step2: Listing All Terraform Workspaces

To display all existing workspaces, use the command:

terraform workspace list

The active workspace is denoted with a star.

Screenshot 2023 12 21 132215

Step3: Showing the Active Terraform Workspace

To reveal the currently active workspace, employ the command:

terraform workspace show

This confirms the active workspace, as illustrated with “test.”

Screenshot 2023 12 21 132250

Step4: Switching Between Terraform Workspaces

Switch between workspaces effortlessly with the command:

terraform workspace select dev

Replace “dev” with your desired workspace name.

Screenshot 2023 12 21 132323

Step5: Using Workspace Name in Terraform Configuration

While working with longer terraform infrastructure code it often needed to name or tag your resources based on the environment profile such as dev, test, stage, prod, etc.

With the help of ${terraform.workspace} interpolation sequence you can use the current workspace name to define terraform locals.

Here is a simple example of provisioning a simple EC2 instance –

  1. I have defined a terraform local instance_name
  2. The terraform local have a name prefixed with the current workspace name .i.e. – “${terraform.workspace}-instance”
  3. And the terraform local is used to tag the aws_instance
 provider "aws" {
   region     = "ap-south-1"
   profile = "default"

locals {
  instance_name = "${terraform.workspace}-instance" 

resource "aws_instance" "ec2_example" {

    ami = "ami-0287a05f0ef0e9d9a" 

    instance_type = var.instance_type


    tags = {
      Name = local.instance_name
Screenshot 2023 12 21 131543
variable "instance_type" {
    type = string
Screenshot 2023 12 21 131550
instance_type = "t2.nano"
Screenshot 2023 12 21 131603
instance_type = "t2.micro"
Screenshot 2023 12 21 142706

Step6: Let’s Deploy

 Let’s run our configuration and make sure everything works correctly. On the command line, run the following commands:

  • terraform init

The terraform init the command is used to initialize a new or existing Terraform configuration. This command downloads the required provider plugins and sets up the backend for storing state.

Screenshot 2023 12 21 134056
  • terraform plan -var-file=”dev.tfvars” for Dev Environment
terraform plan -var-file="dev.tfvars" 

The terraform plan the command is used to create an execution plan for the Terraform configuration. This command shows what resources Terraform will create, modify, or delete when applied.

Terraform plan initiates an infrastructure execution plan, and -var-file=”dev.tfvars” specifies variable values from “dev.tfvars” for configuration in Terraform.

Screenshot 2023 12 21 134332
Screenshot 2023 12 21 134348
Screenshot 2023 12 21 134339
  • terraform plan -var-file=”test.tfvars” for Test Environment
terraform plan -var-file="test.tfvars" 

The terraform plan the command is used to create an execution plan for the Terraform configuration. This command shows what resources Terraform will create, modify, or delete when applied.

The command terraform plan -var-file=”test.tfvars” generates an execution plan, incorporating variable values from “test.tfvars” for infrastructure changes specific to the test environment.

Screenshot 2023 12 21 134507
Screenshot 2023 12 21 134517
Screenshot 2023 12 21 134526
  • terraform apply
terraform apply -var-file="dev.tfvars"  #for Dev Environment
terraform apply -var-file="test.tfvars" #for test Environment

The terraform apply the command is used to apply the Terraform configuration and create or modify resources in the target environment.

Screenshot 2023 12 21 135623
  • terraform destroy

terraform destroy removes all resources defined in the Terraform configuration, effectively tearing down the entire infrastructure.

terraform destroy -var-file="dev.tfvars"
terraform destroy -var-file="test.tfvars"
Screenshot 2023 12 21 140633

Step7: Verify on AWS console

Dev Environment:

Screenshot 2023 12 21 135931

Test Environment:

Screenshot 2023 12 21 140227


In conclusion, Terraform Workspaces empower streamlined and efficient management of diverse environments. The isolation, state management, and customization capabilities enhance control and flexibility, offering a robust solution for managing multiple environments seamlessly.


For reference visit the official website TerraformRegistry.

Any queries pls contact us @Devopshint.

Related Articles:

Terraform State File Management | Terraform Local State File | Terraform Remote State

About Akash Bhujbal

Hey, I am Akash Bhujbal, I am an aspiring DevOps and Cloud enthusiast who is eager to embark on a journey into the world of DevOps and Cloud. With a strong passion for technology and a keen interest in DevOps and Cloud based solutions, I am driven to learn and contribute to the ever-evolving field of DevOps and Cloud.

Leave a Comment

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

Share via
Copy link