How to Manage Multiple Environments in Terraform Easily
To manage multiple environments in
Terraform, use separate workspaces or distinct folders with environment-specific variable files. This keeps configurations isolated and reusable, allowing you to deploy infrastructure for dev, staging, and production safely.Syntax
Terraform supports multiple environments mainly through workspaces and folder structures. Workspaces isolate state files, while folders separate configuration files and variables.
terraform workspace new <name>: Create a new workspace.terraform workspace select <name>: Switch to an existing workspace.- Use
terraform.tfvarsor<env>.tfvarsfiles to set environment-specific variables.
bash
terraform workspace new dev terraform workspace select dev terraform apply -var-file=dev.tfvars
Output
Created and switched to workspace "dev".
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Example
This example shows how to use workspaces and variable files to manage dev and prod environments with the same Terraform code.
hcl
variable "instance_count" { type = number default = 1 } provider "aws" { region = "us-east-1" } resource "aws_instance" "example" { count = var.instance_count ami = "ami-0c55b159cbfafe1f0" instance_type = "t2.micro" } # Commands to switch environments and apply # terraform workspace new dev # terraform workspace new prod # terraform workspace select dev # terraform apply -var-file=dev.tfvars # terraform workspace select prod # terraform apply -var-file=prod.tfvars
Output
Workspace "dev" selected.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Workspace "prod" selected.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Common Pitfalls
Common mistakes when managing multiple environments include:
- Using the same state file for all environments, causing resource conflicts.
- Not isolating variables, leading to accidental resource changes.
- Forgetting to switch workspaces before applying changes.
Always verify your current workspace with terraform workspace show and use environment-specific variable files.
bash
## Wrong: Applying without switching workspace terraform apply -var-file=prod.tfvars ## Right: Switch workspace first terraform workspace select prod terraform apply -var-file=prod.tfvars
Quick Reference
| Command | Purpose |
|---|---|
| terraform workspace new | Create a new environment workspace |
| terraform workspace select | Switch to an environment workspace |
| terraform workspace show | Show current workspace |
| terraform apply -var-file= | Apply config with environment variables |
| Use separate folders | Organize environment configs and variables |
Key Takeaways
Use Terraform workspaces to isolate environment states safely.
Keep environment-specific variables in separate .tfvars files.
Always switch to the correct workspace before applying changes.
Organize environments using folders for clearer structure.
Verify your current workspace with 'terraform workspace show' to avoid mistakes.