What Are Workspaces?
Terraform workspaces let you maintain separate state files for the same configuration. One codebase, multiple environments.
# 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 prodUse in Configuration
Access the current workspace with terraform.workspace:
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:
resource "aws_s3_bucket" "data" {
bucket = "myapp-${terraform.workspace}-data"
}
resource "aws_db_instance" "main" {
identifier = "myapp-${terraform.workspace}-db"
# ...
}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:
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.tfstateWorkspace-Specific Variables
Combine workspaces with tfvars for full flexibility:
# Apply with workspace-specific variables
terraform workspace select prod
terraform apply -var-file="envs/${terraform.workspace}.tfvars"Or use a wrapper script:
#!/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.tfvarsGet weekly IT automation tips
Docker, Ansible, Terraform, MLOps ā curated insights delivered to your inbox. No spam.
Subscribe Free āWorkspaces in CI/CD
# GitHub Actions
- name: Deploy to production
env:
TF_WORKSPACE: prod
run: |
terraform init
terraform plan -var-file="envs/prod.tfvars" -out=tfplan
terraform apply tfplanDelete a Workspace
# Switch away first
terraform workspace select default
# Delete (must have empty state)
terraform workspace delete devRelated Posts
- Terraform for Beginners Guide for getting started
- Terraform Variables and Outputs for variable management
- Terraform State Management for remote state
- Terraform CI/CD Pipelines for automation
---
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.
Related Articles
Terraform Workspaces Multi-Env
Manage environments with Terraform workspaces. CLI workspaces, Terraform Cloud workspaces, directory isolation, and trade-offs.
GitHub Actions CI/CD for Terraform
Automate Terraform with GitHub Actions. Plan on PR, apply on merge, remote state locking, and secure secrets for IaC pipelines.
Terraform Ansible Kubernetes Stack
Combine Terraform, Ansible, and Kubernetes for complete DevOps infrastructure. Provision, configure, and deploy end to end.
Terragrunt DRY Terraform Guide
Terragrunt eliminates copy-paste in multi-environment Terraform setups. Learn how to use Terragrunt for DRY configuration, remote state management.
Testkube Kubernetes Testing Guide
Testkube runs tests natively on Kubernetes using any testing framework. Learn how to run integration tests, load tests, and API tests inside your cluster.
Thanos Long-Term Prometheus Storage
Thanos extends Prometheus with unlimited retention, global querying across clusters, and downsampling. Learn how to deploy Thanos Sidecar and Store Gateway.
Explore topics
Browse more articles on the topics covered here.