Configure Terraform workspaces for multi-environment management
✓Works with OpenClaudeYou are a Terraform infrastructure engineer. The user wants to configure and manage Terraform workspaces for multi-environment deployments (dev, staging, prod).
What to check first
- Run
terraform versionto confirm Terraform is installed (v0.10.0+) - Check
terraform workspace listto see existing workspaces in your current backend - Verify your backend configuration in
main.tforbackend.tf— workspaces require a persistent backend (S3, Terraform Cloud, etc.)
Steps
- Initialize your Terraform backend with
terraform initif not already done — this sets up remote state storage - Create a new workspace with
terraform workspace new dev(replacedevwith environment name) - Switch to the workspace using
terraform workspace select dev - Create a
terraform.tfvars.devfile with environment-specific variables (instance count, instance type, etc.) - Reference the current workspace in your code using the
terraform.workspacebuilt-in variable - Use conditional logic in your resources to apply different configurations per workspace
- Plan changes with
terraform plan -var-file="terraform.tfvars.${terraform workspace show}"to preview environment-specific deployment - Apply with
terraform apply— Terraform will automatically use the selected workspace's state file
Code
# backend.tf - Configure S3 backend with workspace support
terraform {
required_version = ">= 1.0"
backend "s3" {
bucket = "my-terraform-state"
key = "terraform.tfstate"
region = "us-east-1"
encrypt = true
dynamodb_table = "terraform-locks"
}
}
# main.tf - Use workspaces for environment-specific config
locals {
env_config = {
dev = {
instance_type = "t3.micro"
instance_count = 1
enable_monitoring = false
}
staging = {
instance_type = "t3.small"
instance_count = 2
enable_monitoring = true
}
prod = {
instance_type = "t3.medium"
instance_count = 3
enable_monitoring = true
}
}
current_env = terraform.workspace
config = local.env_config[local.current_env]
}
provider "aws" {
region = "us-east-1"
default_tags {
tags = {
Environment = local.current_env
ManagedBy = "Terraform"
}
}
}
resource "aws_instance" "app" {
count = local.config.instance_count
ami = data.aws_ami.ubuntu.id
instance_type = local.config.instance_type
tags = {
Name = "${local.current_env}-app-${count.index
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Terraform Skills
Other Claude Code skills in the same category — free to download.
Terraform Module
Create reusable Terraform modules with variables and outputs
Terraform State
Manage Terraform state with remote backends (S3, Azure, GCS)
Terraform Provider
Write custom Terraform providers with Go
Terraform Import
Import existing infrastructure into Terraform state
Terraform Testing
Write Terraform tests with Terratest and terraform test
Terraform CI/CD
Set up Terraform CI/CD with GitHub Actions and Atlantis
Terraform Security
Scan Terraform for security issues with tfsec and Checkov
Want a Terraform skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.