Terraform Workspaces

Hamza RehmanHamza Rehman
3 min read

Terraform workspaces provide a convenient way to manage multiple environments or configurations within the same Terraform codebase. This article will introduce the concept of workspaces and explain how to use them for multi-environment deployments.

Introduction to Workspaces

A workspace in Terraform is an isolated state environment that allows you to manage different states for the same configuration. By default, Terraform creates a single workspace called default. However, you can create additional workspaces to manage different environments such as development, staging, and production.

Terraform Modules and Workspaces

Key Benefits of Workspaces:

  • Isolation: Each workspace maintains its own state file, ensuring that changes in one environment do not affect others.

  • Simplicity: Workspaces allow you to use the same configuration files across multiple environments without duplication.

  • Flexibility: Easily switch between environments to manage different stages of your infrastructure lifecycle.

Real-World Scenario: Imagine you have a web application that you want to deploy in development, staging, and production environments. Using workspaces, you can manage these environments separately while using the same Terraform configuration files.

Using Workspaces for Multi-Environment Deployments

Using workspaces for multi-environment deployments is straightforward. Here’s a step-by-step guide to help you get started.

Step 1: Initialize Your Configuration

Before creating workspaces, ensure your Terraform configuration is initialized:

terraform init

Step 2: Create Workspaces

You can create new workspaces using the terraform workspace new command:

terraform workspace new development
terraform workspace new staging
terraform workspace new production

Step 3: Switch Between Workspaces

Switch to a specific workspace using the terraform workspace select command:

terraform workspace select development

Step 4: Apply Configuration to Workspaces

When you apply your Terraform configuration, the changes are applied to the current workspace's state:

terraform apply

Repeat this process for each workspace (e.g., development, staging, production) to deploy your infrastructure in different environments.

Example Configuration:

Here’s a simple example to illustrate how you can use workspaces:

Step 5: Managing Variables Across Workspaces

You can manage variables specific to each workspace using different *.tfvars files. For example, create development.tfvars, staging.tfvars, and production.tfvars files to specify different variable values for each environment:

development.tfvars:

region = "us-west-1"

staging.tfvars:

region = "us-west-2"

production.tfvars:

region = "us-east-1"

Apply Configuration with Specific Variables:

terraform workspace select development
terraform apply -var-file="development.tfvars"

terraform workspace select staging
terraform apply -var-file="staging.tfvars"

terraform workspace select production
terraform apply -var-file="production.tfvars"

Real-World Scenario: Suppose you are managing an e-commerce platform that requires separate environments for development, testing, and production. By using Terraform workspaces, you can ensure that changes made in the development environment do not affect the production environment. Each environment can have different configurations, such as instance sizes or network settings, tailored to its specific needs.

Understanding Terraform Workspace: Features, Benefits, Drawbacks, etc. -  Modern Technologist

Conclusion

Terraform workspaces provide a powerful mechanism for managing multiple environments with the same set of configuration files. By isolating states and allowing easy switching between environments, workspaces simplify the deployment and management of infrastructure across different stages of the development lifecycle. As you adopt Terraform workspaces, you'll find it easier to maintain consistency, reduce duplication, and enhance collaboration within your team.

1
Subscribe to my newsletter

Read articles from Hamza Rehman directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hamza Rehman
Hamza Rehman

My name is Hamza Rehman. I'm a passionate DevOps enthusiast. With a deep interest in open-source technologies and automation, I enjoys to share my knowledge and insights with the community.