Skip to content
About Me
----

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.


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.tfvars

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 RDS
  1. Isolated Blast Radius: A developer running terraform apply in dev physically lacks AWS IAM credentials to mutate prod.
  2. Distinct State Buckets: If the dev state file gets corrupted during testing, prod remains 100% operational and isolated.
  3. Branch Protection & Approvals: environments/prod/ can require 2 Senior Architect approvals on GitHub, while environments/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
}