- TechInsightNeuron
- Posts
- Terraform Workspaces: Managing Multiple Environments the Right Way
Terraform Workspaces: Managing Multiple Environments the Right Way
Learn what Terraform workspaces really are, how they isolate infrastructure state, when to use them, and when to avoid them in favor of cleaner alternatives.

π Hey there, Iβm Dheeraj Choudhary an AI/ML educator, cloud enthusiast, and content creator on a mission to simplify tech for the world.
After years of building on YouTube and LinkedIn, Iβve finally launched TechInsight Neuron a no-fluff, insight-packed newsletter where I break down the latest in AI, Machine Learning, DevOps, and Cloud.
π― What to expect: actionable tutorials, tool breakdowns, industry trends, and career insights all crafted for engineers, builders, and the curious.
π§ If you're someone who learns by doing and wants to stay ahead in the tech game you're in the right place.

What Are Terraform Workspaces?

A Terraform workspace is a named instance of state. That's it.
When you run terraform init
, Terraform creates a default workspace named default
. If you create a new workspace called staging
, Terraform keeps a separate state file for it.
This means:
Same code
Different state
Different resources in the cloud
Fully isolated environments
Creating and Using Workspaces
π§ Create a new workspace:
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod
π Switch between them:
terraform workspace select staging
π List all workspaces:
terraform workspace list
π§½ Delete a workspace:
terraform workspace delete dev
When you switch workspaces, Terraform automatically uses a separate state backend under the hood (e.g., terraform.tfstate.d/staging/terraform.tfstate
locally).
Using Workspaces in Code
You can access the active workspace using:
terraform.workspace
Example:
resource "aws_s3_bucket" "logs" {
bucket = "logs-${terraform.workspace}"
}
This will create:
logs-dev
in thedev
workspacelogs-prod
in theprod
workspace
Workspace Gotchas and Anti-Patterns
Workspaces can be powerful β but theyβre often misused.
β Common Anti-Patterns:
Using workspaces as your main environment management strategy
Trying to manage non-identical infrastructure (e.g., dev has no RDS, prod does)
Running workspaces in CI/CD pipelines that expect clean state control
Workspaces are not a replacement for separate codebases or modules when your environments have meaningful infrastructure differences.
Alternative Approach: Folder-Based Environments

Many teams choose to create one folder per environment, each with its own:
.tfvars
fileBackend configuration
Provider config
Module inputs
environments/
βββ dev/
β βββ main.tf
β βββ variables.tf
β βββ terraform.tfvars
βββ staging/
βββ prod/
This approach provides stronger separation, better CI/CD integration, and more flexibility.
When to Use Workspaces
β
For lightweight, identical environments (e.g., dev/staging with same infra)
β
When you want quick local switching without duplicating code
β
When infrastructure structure stays the same across all workspaces
β
In conjunction with a remote state backend that supports isolation
When NOT to Use Workspaces
β When environments require different resources or structure
β When you're deploying via CI/CD pipelines that don't support context switching
β When you want stronger boundaries between teams or regions
β If you need to manage secrets or providers differently per env
Hands-On: Deploying Two Environments with Workspaces

Step 1: Create Workspace
terraform workspace new dev
Step 2: Apply Config
resource "aws_s3_bucket" "logs" {
bucket = "logs-${terraform.workspace}"
}
terraform apply
Step 3: Switch and Repeat
terraform workspace new prod
terraform apply
You now have two buckets:
logs-dev
logs-prod
Same code, separate state, cleanly isolated.
π‘ Tip of the Day:
Workspaces manage state not code.
Donβt use them as a substitute for truly separate infrastructure logic. Think of them as βnamed state buckets,β not βenvironments with brains.β
π Resources & References
1οΈβ£ Terraform Workspaces Docs
π Docs
Official documentation on workspace usage, state behavior, and lifecycle.
2οΈβ£ Managing Environments in Terraform
π Learn
Tutorial comparing workspaces vs folders for environment management.
3οΈβ£ Using terraform.workspace Variable
π Docs
Reference for dynamic workspace access in config.
4οΈβ£ State File Management per Workspace
π Deep Dive
Understand how workspaces isolate state and resources.
πLetβs Stay Connected
π± Join Our WhatsApp Community
Get early access to AI/ML, Cloud & Devops resources, behind-the-scenes updates, and connect with like-minded learners.
β‘οΈ Join the WhatsApp Group
β
Follow Me for Daily Tech Insights
β‘οΈ LinkedIN
β‘οΈ YouTube
β‘οΈ X (Twitter)
β‘οΈ Website
Conclusion
Terraform Workspaces are a powerful but often misunderstood feature. When used correctly, they offer a clean way to isolate environments using the same codebase particularly useful for small teams or lightweight deployments.
But as your infrastructure evolves with environment-specific configurations, secrets, or pipelines consider if folders, modules, or even separate Terraform projects serve you better.
Know when to use workspaces. Know when to walk away.