When do you turn a block of resources into a module, and when do you leave it alone?
BeginnerScenarioModules and Code Structure
Answer
Situation read → Modules add indirection; they should earn their keep.
What I'd do →
- Create a module when the same pattern is used in 2–3 places, or when a team needs a safe, opinionated way to build something (VPC, ECS service, RDS).
- Keep one-off resources in the root module.
- Give the module a small, typed interface and meaningful outputs.
Why → A module with 40 pass-through variables is just a worse version of the resource. A good module hides complexity and enforces standards.
Interview tip → "A module should model a concept, not wrap a single resource."
More on Modules and Code Structure
How 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 would you lay out a repo that deploys the same stack to dev, stage and prod?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