When working with cloud infrastructure, we often need to manage multiple environments such as Development (Dev), Testing (Test), and Production (Prod). Instead of copying and pasting the same Terraform code multiple times, we can use Terraform Workspaces to handle everything efficiently and cleanly.
🧩 What Are Terraform Workspaces?
A Terraform Workspace is like a separate folder where Terraform stores its own state file.
Think of it as a way to keep Dev, Test, and Prod environments isolated — even though they share the same configuration files.
In short:
- Same Terraform code ✅
- Different environment states ✅
- No interference between environments ✅
🛠️ Why Use Workspaces?
Here’s why workspaces make life easier:
- Isolation: Each environment has its own state file.
- Consistency: Use the same code for all environments.
- Simplicity: Avoid managing multiple folders or repositories.
- Flexibility: Easily switch between environments during deployment.
⚙️ Setting Up Workspaces Step-by-Step
Let’s walk through how to get started.
Step 1: Initialize Terraform
First, navigate to your project folder and run:
terraform init
Step 2: Create a Workspace
Create a new workspace for the development environment:
terraform workspace new dev
Do the same for test and prod environments:
terraform workspace new test
terraform workspace new prod
Step 3: Switch Between Workspaces
To move between environments:
terraform workspace select dev
terraform workspace select test
terraform workspace select prod
Step 4: Apply Environment-Specific Variables
You can maintain different variable files for each environment.
Example folder structure:
├── main.tf
├── variables.tf
├── dev.tfvars
├── test.tfvars
└── prod.tfvars
Each .tfvars file can hold environment-specific values:
# dev.tfvars
instance_type = "t2.micro"
environment = "dev"
# prod.tfvars
instance_type = "t3.large"
environment = "prod"
Now apply your configuration for a workspace:
terraform apply -var-file="dev.tfvars"
🧠 Bonus Tip — Use Workspace Name in Resource Names
You can automatically include the workspace name in resource names to avoid naming conflicts:
resource "azurerm_resource_group" "example" {
name = "rg-${terraform.workspace}"
location = "Canada Central"
}
When deploying in dev, this will create rg-dev; in prod, it will create rg-prod.
🚀 Wrapping Up
Terraform Workspaces help you:
- Manage multiple environments easily
- Avoid duplicating code
- Keep your state files separate and safe
If you’re just starting out with Terraform, using workspaces is one of the simplest and smartest ways to manage real-world environments effectively.
💡 Next Steps
- Try creating your first few workspaces (Dev, Test, Prod).
- Experiment with variables for each environment.
- Connect your Terraform setup to a remote backend (like S3 or Azure Blob) for better collaboration.
🎁 Bonus Section: Workspaces vs. Separate Folders — What’s Best?
You might have seen people debate this — are Terraform Workspaces better than separate folders? The truth is, both approaches are valid. The choice depends on your project size, team structure, and infrastructure complexity.
Here’s a quick overview 👇
| Project Type | Recommended Approach | Why |
|---|---|---|
| Small / Learning / Demo | Workspaces | Simple and easy to manage |
| Medium (similar environments) | Workspaces with tfvars | Code reuse + isolated states |
| Large / Enterprise | Separate Folders or Repos | Strong isolation + CI/CD flexibility |
| Multi-region or multi-team setups | Hybrid | Balance between reuse and safety |
Workspaces make it easy to manage multiple environments with a single configuration, but as your project grows, separating folders gives you stronger control and governance.
💡 Want to go deeper?
Check out our detailed guide 👉
Terraform Workspaces vs Separate Folders — Which One Should You Use?