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 →
- Put reusable code under
modules/. - Create
envs/dev,envs/stage,envs/prod, each a small root module that calls the shared modules with its own variables and backend key. - 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.
More on Modules and Code Structure
When do you turn a block of resources into a module, and when do you leave it alone?BeginnerHow do you consume a community module like `terraform-aws-modules/vpc/aws` responsibly?BeginnerAn ECS service module needs the VPC and subnets created by a network module. How do you wire them together?IntermediateHow do you release a shared module so consumers can upgrade at their own pace?IntermediateA module needs to deploy to a second region. Why does putting a provider block inside the module cause trouble?AdvancedTeams complain a "VPC module" has 90 variables and still doesn't do what they need. How do you fix the design?Intermediate