Workspaces vs. Directory-Based Environments
Architect Key Takeaways
Master multi-environment Terraform isolation: Terraform CLI Workspaces vs Directory-Based separation, Terragrunt patterns, and blast radius control.
Workspaces vs. Directory-Based Multi-Environment Architecture
Section titled βWorkspaces vs. Directory-Based Multi-Environment ArchitectureβOne of the most fiercely debated topics in the DevOps community is: How do you separate Development, Staging, and Production environments safely in Terraform?
Getting this wrong means an accidental typo in dev can destroy your prod database.
1. The Two Primary Architectural Paradigms
Section titled β1. The Two Primary Architectural ParadigmsβAPPROACH A: CLI WORKSPACES APPROACH B: DIRECTORY ISOLATIONβββ main.tf βββ environments/βββ variables.tf β βββ dev/βββ (terraform.tfstate per workspace) β β βββ main.tf (s3://.../dev/state) βββ default β β βββ terraform.tfvars βββ dev β βββ prod/ βββ prod β βββ main.tf (s3://.../prod/state) β βββ terraform.tfvars2. Head-to-Head Comparison
Section titled β2. Head-to-Head Comparisonβ| Feature | CLI Workspaces (terraform workspace) |
Directory-Based Isolation (environments/) |
|---|---|---|
| Code Duplication | Minimal (single codebase) | Slightly higher or eliminated via Terragrunt |
| Blast Radius | β οΈ High: Shared root config; human error can target prod |
π‘οΈ Zero: Strict IAM credentials per directory |
| Access Control (RBAC) | Hard to isolate IAM access per workspace | Easy: CI/CD jobs use separate AWS accounts per directory |
| Different Architectures | Hard: Conditionals (count = env == "prod" ? 3 : 1) pollute code |
Easy: dev can use single NAT GW; prod uses 3 |
| Enterprise Consensus | Great for ephemeral PR previews and branch testing | Gold Standard for multi-account enterprise cloud |
3. The Enterprise Standard: Multi-Account Directory Isolation
Section titled β3. The Enterprise Standard: Multi-Account Directory IsolationβModern cloud security relies on AWS Organizations where dev, staging, and prod reside in completely separate AWS Accounts.
infrastructure-live/βββ environments/β βββ dev/β β βββ backend.tf # Points to Dev Account S3 Bucketβ β βββ main.tf # Calls VPC & Compute child modulesβ β βββ dev.tfvars # Single NAT, t3.small instancesβ βββ prod/β βββ backend.tf # Points to Prod Account S3 Bucketβ βββ main.tf # Calls VPC & Compute child modulesβ βββ prod.tfvars # Triple Multi-AZ NATs, m6i.xlarge, Multi-AZ RDSWhy Directory Isolation Wins in Enterprise DevSecOps:
Section titled βWhy Directory Isolation Wins in Enterprise DevSecOps:β- Isolated Blast Radius: A developer running
terraform applyindevphysically lacks AWS IAM credentials to mutateprod. - Distinct State Buckets: If the
devstate file gets corrupted during testing,prodremains 100% operational and isolated. - Branch Protection & Approvals:
environments/prod/can require 2 Senior Architect approvals on GitHub, whileenvironments/dev/merges instantly.
4. Reading Cross-Stack Outputs with terraform_remote_state
Section titled β4. Reading Cross-Stack Outputs with terraform_remote_stateβWhen your compute layer needs to discover the vpc_id created by the networking layer without combining them into one monolithic state:
data "terraform_remote_state" "vpc" { backend = "s3" config = { bucket = "corp-terraform-state-prod" key = "networking/vpc/terraform.tfstate" region = "us-east-1" }}
# Access the exported VPC output cleanly:resource "aws_security_group" "app_sg" { name = "app-service-sg" vpc_id = data.terraform_remote_state.vpc.outputs.vpc_id}