⚡ ~/naveed Interview Prep
⚡ Portfolio Home 👤 About Naveed Bio & Credentials 📫 Contact Naveed Get In Touch ✍️ Engineering Blog Deep Dives 🎯 Interview Hub 1,000+ Scenarios ☸️ Kubernetes Mastery Hub 24 Modules 🎮 DevOps Arcade & Quizzes Subnet Blitz ⚡ 🗺️ DevOps Roadmaps PDFs & Guides 🤖 Morpheus Analysis AI Quant ↗ 🛠️ Developer Tools Utilities 🧪 Labs & Experiments 📄 Interactive CV & Certs 🔗 All Links & Socials ⚡ Join The Dispatch (Weekly SRE Newsletter) →
← Back to All Terraform & IaC Interview Questions Scenario 4 of 120 in Terraform & IaC
Senior DevOps / SRE Terraform Repository Architecture Enterprise Architecture
🎯 Target Role / Context: IaC Architecture & Best Practices

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.

#Terraform #Architecture #Directory Layout #Workspaces #Multi-Account #DRY #Comparisons
🎙️ Candidate Opening & Architectural Context
"For enterprise production infrastructure, the recommended pattern is Directory-Based Separation using Reusable Modules across Multi-Account AWS environments, rather than Terraform Workspaces."
Advertisement
⚡ Recommended Practice Lab

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.

1️⃣

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.tf affects 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.
2️⃣

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
Advertisement
3️⃣

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.
4️⃣

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.0 in Dev.
  • Promote to Staging and validate with integration tests.
  • Promote to Prod only after Staging is verified stable.
💡 The Senior SRE Gold Nugget (Key Architectural Takeaway)
"Use directory-based separation with version-pinned reusable modules and separate AWS accounts per environment. Avoid workspaces for multi-environment cloud infrastructure due to shared backend risks."
⚡ 60-Second Elevator Pitch Talking Points
  • 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.
⛔ Critical Candidate Mistakes to Avoid in This Scenario

Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:

  1. 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.
  2. 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.
  3. 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.
  4. 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.
Advertisement
Want more Terraform & IaC scenarios?
Explore our complete collection of scenario-based Terraform & IaC interview runbooks.
Browse All Terraform & IaC Questions →