Home Infrastructure as Code (IaC) Getting Started with Terraform Workspaces for Managing Multiple Environments (Dev, Prod etc.)

Getting Started with Terraform Workspaces for Managing Multiple Environments (Dev, Prod etc.)

307
0

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:

  1. Isolation: Each environment has its own state file.
  2. Consistency: Use the same code for all environments.
  3. Simplicity: Avoid managing multiple folders or repositories.
  4. 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 TypeRecommended ApproachWhy
Small / Learning / DemoWorkspacesSimple and easy to manage
Medium (similar environments)Workspaces with tfvarsCode reuse + isolated states
Large / EnterpriseSeparate Folders or ReposStrong isolation + CI/CD flexibility
Multi-region or multi-team setupsHybridBalance 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?

Previous articleTerraform Workspaces vs Separate Folders — Which One Should You Use?
Next articleGetting Started with Python
Heartin Kanikathottu
As a seasoned Cloud and Security Architect, I’ve led transformative initiatives in key roles, including Vice President at Morgan Stanley, Principal Architect at Societe Generale, and Tech Lead & Cloud Security Architect at VMware, among others. I’m also an internationally published author with multiple books available on platforms like Amazon and O'Reilly. Notably, one of my books was recognized as the 8th best cloud computing book of all time in 2020, reflecting the impact of my contributions to the field. With over 15 professional certifications from providers such as Microsoft (Azure), Amazon (AWS), Oracle (Java), Pivotal (Spring), and IBM, I bring a wealth of expertise to my work. Academically, I hold dual Master’s degrees in Cloud Computing and Data Analytics. I’m passionate about sharing knowledge and mentoring others, which is why I actively speak at global technical forums such as Tech Opportunities Fest at Platform Calgary, Google's Kubernetes Meetup, Java User Group, Elasticsearch Meetup, and the Agile India Conference.

LEAVE A REPLY

Please enter your comment!
Please enter your name here