Interview Prep/Terraform/Modules and Code Structure

How would you lay out a repo that deploys the same stack to dev, stage and prod?

IntermediateScenarioModules and Code Structure
Answer

Situation read → You need isolation between environments and shared logic.

What I'd do →

  1. Put reusable code under modules/.
  2. Create envs/dev, envs/stage, envs/prod, each a small root module that calls the shared modules with its own variables and backend key.
  3. Version modules with Git tags so prod can lag dev.
modules/network  modules/app  modules/database
envs/dev/main.tf  envs/dev/backend.tf  envs/dev/terraform.tfvars
envs/prod/main.tf envs/prod/backend.tf envs/prod/terraform.tfvars

Why → Separate directories mean separate state and a prod apply can never be triggered by editing dev.

Interview tip → I avoid workspaces for prod separation; directories make the target environment obvious.

All Terraform questions Roadmap: Terraform

More on Modules and Code Structure