Skip to main content
šŸŽ¤ Luca Berton is speaking at Red Hat Summit & KubeCon EU 2026!Learn more →
Back to Blog

Terraform Workspaces for Environments

Use Terraform workspaces for dev, staging, and production. Practical patterns, trade-offs, and best practices for multi-env IaC.

Luca BertonApril 12, 20261 min read

What Are Workspaces?

Terraform workspaces let you maintain separate state files for the same configuration. One codebase, multiple environments.

bash
# List workspaces
terraform workspace list

# Create and switch
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod

# Switch between them
terraform workspace select prod

Use in Configuration

Access the current workspace with terraform.workspace:

hcl
locals {
  environment = terraform.workspace

  instance_count = {
    dev     = 1
    staging = 2
    prod    = 3
  }

  instance_type = {
    dev     = "t3.micro"
    staging = "t3.small"
    prod    = "t3.large"
  }
}

resource "aws_instance" "web" {
  count         = local.instance_count[local.environment]
  ami           = data.aws_ami.ubuntu.id
  instance_type = local.instance_type[local.environment]

  tags = {
    Name        = "web-${local.environment}-${count.index}"
    Environment = local.environment
  }
}

Resource Naming

Prefix resources with the workspace name to avoid conflicts:

hcl
resource "aws_s3_bucket" "data" {
  bucket = "myapp-${terraform.workspace}-data"
}

resource "aws_db_instance" "main" {
  identifier = "myapp-${terraform.workspace}-db"
  # ...
}
Related Course

Master this topic with hands-on labs

Go beyond reading — build real projects in sandboxed environments with expert video guidance.

Browse Courses →

Remote State with Workspaces

Each workspace gets its own state file. With S3 backend:

hcl
terraform {
  backend "s3" {
    bucket = "my-terraform-state"
    key    = "app/terraform.tfstate"
    region = "eu-west-1"
  }
}

State files are stored as:

s3://my-terraform-state/env:/dev/app/terraform.tfstate
s3://my-terraform-state/env:/staging/app/terraform.tfstate
s3://my-terraform-state/env:/prod/app/terraform.tfstate

Workspace-Specific Variables

Combine workspaces with tfvars for full flexibility:

bash
# Apply with workspace-specific variables
terraform workspace select prod
terraform apply -var-file="envs/${terraform.workspace}.tfvars"

Or use a wrapper script:

bash
#!/bin/bash
ENV=$1
terraform workspace select "$ENV" || terraform workspace new "$ENV"
terraform apply -var-file="envs/${ENV}.tfvars"

When NOT to Use Workspaces

Workspaces work best for identical infrastructure across environments. Consider separate directories if:

  • Environments have fundamentally different architectures
  • Different teams manage different environments
  • You need different providers or backends per environment

The alternative — directory-based layout:

environments/
ā”œā”€ā”€ dev/
│   ā”œā”€ā”€ main.tf
│   └── terraform.tfvars
ā”œā”€ā”€ staging/
│   ā”œā”€ā”€ main.tf
│   └── terraform.tfvars
└── prod/
    ā”œā”€ā”€ main.tf
    └── terraform.tfvars
Stay Updated

Get weekly IT automation tips

Docker, Ansible, Terraform, MLOps — curated insights delivered to your inbox. No spam.

Subscribe Free →

Workspaces in CI/CD

yaml
# GitHub Actions
- name: Deploy to production
  env:
    TF_WORKSPACE: prod
  run: |
    terraform init
    terraform plan -var-file="envs/prod.tfvars" -out=tfplan
    terraform apply tfplan

Delete a Workspace

bash
# Switch away first
terraform workspace select default

# Delete (must have empty state)
terraform workspace delete dev

---

Ready to go deeper? Check out our hands-on course: Terraform for Beginners — practical exercises you can follow along on your own machine.

Ready to learn by doing?

Stop reading tutorials — start building. Expert video courses with hands-on labs in real sandboxed environments.

Share this article
LB
Luca Berton

Docker Captain, IT automation expert, Red Hat Summit & KubeCon speaker. Building hands-on education for DevOps engineers at CopyPasteLearn.

Related Articles

Explore topics

Browse more articles on the topics covered here.