Terraform State Explained - What It Is and How to Manage It
Complete guide to Terraform state: what it is, why it matters, remote backends, state locking, essential commands, and best practices for teams.
Guides
How to use Terraform workspaces to manage dev, staging, and production environments with one configuration. Commands, patterns, and when to avoid workspaces.
Terraform workspaces let you manage multiple state files (dev, staging, prod) from the same configuration. Each workspace has isolated state. Use terraform workspace new dev to create, terraform workspace select prod to switch.
# List workspaces (* = current)
terraform workspace list
# Create a new workspace
terraform workspace new dev
terraform workspace new staging
terraform workspace new prod
# Switch workspace
terraform workspace select prod
# Show current workspace
terraform workspace show
# Delete a workspace (must switch away first)
terraform workspace select default
terraform workspace delete dev# Access current workspace name
locals {
environment = terraform.workspace
is_prod = terraform.workspace == "prod"
}
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = local.is_prod ? "t3.large" : "t3.micro"
tags = {
Name = "web-${local.environment}"
Environment = local.environment
}
}
resource "aws_s3_bucket" "data" {
bucket = "myapp-${local.environment}-data"
}variable "instance_count" {
type = map(number)
default = {
dev = 1
staging = 2
prod = 3
}
}
variable "instance_type" {
type = map(string)
default = {
dev = "t3.micro"
staging = "t3.small"
prod = "t3.large"
}
}
resource "aws_instance" "web" {
count = lookup(var.instance_count, terraform.workspace, 1)
instance_type = lookup(var.instance_type, terraform.workspace, "t3.micro")
ami = data.aws_ami.ubuntu.id
}# Local backend — separate directories
terraform.tfstate.d/
├── dev/
│ └── terraform.tfstate
├── staging/
│ └── terraform.tfstate
└── prod/
└── terraform.tfstate
# S3 backend — separate keys
s3://my-state-bucket/
├── env:/dev/terraform.tfstate
├── env:/staging/terraform.tfstate
└── env:/prod/terraform.tfstate| Feature | Workspaces | Separate Directories |
|---|---|---|
| Config duplication | None — shared | Some — separate main.tf per env |
| State isolation | ✅ Separate state per workspace | ✅ Separate state per directory |
| Different infrastructure | ❌ Same config | ✅ Can differ completely |
| Different providers | ❌ Same providers | ✅ Different providers per env |
| Visibility | Easy to forget which workspace | Explicit directory = clear |
| CI/CD | Needs workspace select step | Just cd into directory |
environments/
├── dev/
│ ├── main.tf
│ ├── terraform.tfvars
│ └── backend.tf
├── staging/
│ ├── main.tf
│ ├── terraform.tfvars
│ └── backend.tf
└── prod/
├── main.tf
├── terraform.tfvars
└── backend.tf
modules/
├── networking/
├── compute/
└── database/Workspaces work best for simple multi-environment setups where infrastructure is identical except for sizing. For anything more complex — different infra per environment, different access controls, large teams — use separate directories with shared modules instead.
Complete guide to Terraform state: what it is, why it matters, remote backends, state locking, essential commands, and best practices for teams.
Practical Terraform patterns to reduce AWS costs: right-sizing, spot instances, scheduling, and reserved capacity. Step-by-step guide with code examples and ...
How to achieve zero-downtime deployments with Terraform using blue-green, rolling updates, and create_before_destroy. Step-by-step guide with code examples a...
Complete guide to testing Terraform configurations with terraform test, Terratest, and validation rules. Step-by-step guide with code examples and best pract...