Using Workspaces in Terraform: A Simple Guide to Managing Multiple Environments
Managing multiple environments (e.g., dev, staging, production) in Terraform can be challenging. Without a proper strategy, you might end up duplicating code, mixing configurations, or even accidental
Managing multiple environments (e.g., dev, staging, production) in Terraform can be challenging. Without a proper strategy, you might end up duplicating code, mixing configurations, or even accidentally overwriting resources.
This is where Terraform workspaces come in. Workspaces allow you to manage multiple environments within the same Terraform configuration, making it easier to maintain consistency and avoid mistakes. In this article, we’ll explore what workspaces are, why they’re useful, and how to use them effectively.
What are Terraform Workspaces?
A Terraform workspace is a separate instance of state data within the same Terraform configuration. Think of it as a way to create multiple “versions” of your infrastructure, each with its state file.
For example, you can use workspaces to manage:
Different environments: dev, staging, production.
Different regions: us-east-1, eu-west-1.
Different feature branches: feature-x, feature-y.
Workspaces help you avoid duplicating code and keep your configurations clean and organized.
Why Use Workspaces?
Here are some key benefits of using Terraform workspaces:
Avoid Code Duplication: Use the same configuration for multiple environments.
Isolate State Files: Each workspace has its state file, preventing conflicts.
Simplify Management: Switch between environments with a single command.
Improve Consistency: Ensure all environments are created the same way.
How to Use Terraform Workspaces
Let’s walk through an example of using workspaces to manage multiple environments (dev, staging, production) for an AWS EC2 instance.
Step 1: Create a Terraform Configuration
Start by creating a simple Terraform configuration for an EC2 instance:
main.tf
provider "aws" {
region = "us-east-1"
}
resource "aws_instance" "example" {
ami = var.ami_id
instance_type = var.instance_type
tags = {
Name = "${terraform.workspace}-server"
}
}
variable "ami_id" {
description = "The AMI ID for the EC2 instance"
type = string
}
variable "instance_type" {
description = "The instance type for the EC2 instance"
type = string
default = "t2.micro"
}Step 2: Create Workspaces
Terraform starts with a default workspace called default. Let’s create new workspaces for dev, staging, and production:
terraform workspace new dev
terraform workspace new staging
terraform workspace new productionTo list all workspaces, use:
terraform workspace listTo switch between workspaces, use:
terraform workspace select devStep 3: Apply Configuration in Each Workspace
Now, apply the configuration in each workspace. Terraform will create a separate state file for each workspace.
For the dev workspace:
terraform workspace select dev
terraform apply -var="ami_id=ami-1234567890"For the staging workspace:
terraform workspace select staging
terraform apply -var="ami_id=ami-1234567890"For the production workspace:
terraform workspace select production
terraform apply -var="ami_id=ami-1234567890"Each workspace will create a separate EC2 instance with a unique name tag (e.g., dev-server, staging-server, production-server).
Real-World Example: Using Workspaces with Variables
You can use Terraform’s terraform.workspace variable to customize configurations for each workspace. For example, you can use different instance types for different environments:
main.tf
locals {
instance_type = {
dev = "t2.micro"
staging = "t2.medium"
production = "t2.large"
}
}
resource "aws_instance" "example" {
ami = var.ami_id
instance_type = local.instance_type[terraform.workspace]
tags = {
Name = "${terraform.workspace}-server"
}
}In this example:
The
devthe workspace will use at2.microinstance.The
stagingthe workspace will use at2.mediuminstance.The
productionthe workspace will use at2.largeinstance.
Best Practices for Using Workspaces
Use Workspaces for Environments: Workspaces are ideal for managing dev, staging, and production environments.
Avoid Overloading Workspaces: Don’t use workspaces for unrelated purposes (e.g., mixing environments and regions).
Leverage
terraform.workspace: Use theterraform.workspacevariable to customize configurations for each workspace.Backup State Files: Each workspace has its state file, so ensure you back them up regularly.
Document Workspace Usage: Clearly document which workspaces are used for which purposes.
When Not to Use Workspaces
While workspaces are powerful, they’re not always the best solution. Avoid using workspaces if:
You need completely different configurations for each environment.
You’re managing multiple cloud providers or regions (use separate configurations instead).
Conclusion
Terraform workspaces are a simple yet powerful way to manage multiple environments within the same configuration. By isolating state files and leveraging the terraform.workspace variable, you can simplify your workflows, reduce duplication, and ensure consistency across environments.
Whether you’re managing a small project or a large-scale infrastructure, Terraform workspaces are a must-have tool in your IaC toolkit.
👉 Follow me for more insights on DevOps, Terraform, and cloud-native technologies:
Twitter: https://x.com/arun7pulse
YouTube: https://www.youtube.com/arun7pulse
LinkedIn: https://www.linkedin.com/in/arun7pulse/
Medium: https://medium.com/@arun7pulse
Newsletter: https://pulsetechops.substack.com/subscribe
#TechOps #DevOps #SRE #InfrastructureAsCode #Terraform

