Q: How would you structure dev/staging/prod?
Enterprise architectural comparison: Directory-based separation with reusable modules vs Terraform Workspaces, multi-account AWS strategy, and code reuse.
Structuring Dev, Staging, and Prod in Terraform: Directory vs Workspaces
Watch Naveed break down how to structure dev, staging, and production in Terraform—comparing directory-based separation with workspaces, remote state keys, and multi-account blast radius isolation in 60 seconds.
- Choose directory-based separation over workspaces to isolate backend credentials, state locks, and provider accounts
- Decompose environments into modular directories (
environments/dev,staging,prod) consuming versioned modules - Deploy across dedicated AWS Accounts under AWS Organizations to guarantee zero blast-radius bleed
- Promote immutable changes sequentially from Dev to Staging to Production through gated CI/CD pipelines
Want to master this scenario in a live sandbox? KodeKloud's HashiCorp Certified Terraform Associate (003) Interactive Labs covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: For enterprise production infrastructure, the recommended pattern is Directory-Based Separation using Reusable Modules across Multi-Account AWS environments, rather than Terraform Workspaces.
Why Directory-Based Separation Over Workspaces
Understanding the fundamental tradeoff:
- Terraform Workspaces: Workspaces share the exact same backend, variables, and code. A single typo in
main.tfaffects all environments. Workspaces are suitable for testing identical ephemeral branches, but NOT for long-lived Dev/Staging/Prod. - Directory-Based Separation: Completely isolates state files, backend credentials, variables, and provider accounts. An error in Dev cannot touch Production state.
Recommended Repository Layout
Standardized modular directory structure:
terraform/ ├── modules/ # Reusable building blocks │ ├── vpc/ │ ├── eks/ │ └── rds/ └── environments/ ├── dev/ │ ├── backend.tf # S3 backend: key = dev/terraform.tfstate │ ├── main.tf # Calls modules with dev variables │ └── terraform.tfvars ├── staging/ └── prod/ ├── backend.tf # S3 backend: key = prod/terraform.tfstate ├── main.tf └── terraform.tfvars
Multi-Account AWS Strategy
Maximum security and blast radius isolation:
- Deploy each environment to a dedicated AWS Account: AWS Account Dev, AWS Account Staging, AWS Account Prod under AWS Organizations.
- Dev CI/CD runner IAM credentials only have access to the Dev AWS Account.
- Production state bucket lives in the Production AWS account with strict KMS key policies.
Version Pinning & Promotion Workflow
How code moves from Dev to Prod:
- Environments reference modules using Git tags:
source = 'git::https://github.com/org/tf-modules.git//eks?ref=v1.4.0'. - Test module
v1.5.0in Dev. - Promote to Staging and validate with integration tests.
- Promote to Prod only after Staging is verified stable.
- Architecture: Directory-based separation with reusable modules (avoid workspaces for dev/prod due to shared backend blast radius).
- Multi-Account: Separate AWS accounts for Dev, Staging, and Prod under AWS Organizations.
- State Isolation: Distinct S3 backend keys and DynamoDB locks per environment.
- Module Versioning: Environments source modules pinned to immutable Git tags (ref=v1.2.0).
- Promotion: Changes are applied to Dev first, validated in Staging, and promoted to Prod via CI/CD approval.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Executing Terraform applies without inspecting '# forces replacement' warnings in plan output when configuring Structuring Staging Prod Attributes that force resource replacement destroy and recreate critical infrastructure (databases, load balancers), inducing unannounced production outages.
- Omitting remote state locking mechanisms (DynamoDB or backend native locks) for shared state files when configuring Structuring Staging Prod Without state locking, concurrent CI/CD pipeline executions or team applies create race conditions that corrupt the state file.
- Structuring entire enterprise cloud footprints into a single monolithic Terraform root module when configuring Structuring Staging Prod Monolithic state files slow plan execution dramatically, increase blast radius during failed applies, and create severe pipeline locking contention.
- Failing to pin required_providers versions, causing breaking provider upgrades in CI pipelines when configuring Structuring Staging Prod Unpinned provider versions automatically download breaking major upgrades during CI runs, introducing unexpected schema validation failures.