Variables in a module have no defaults and callers keep forgetting some. How do you set required versus optional correctly?
BeginnerScenarioModules and Code Structure
Answer
Situation read → Interface hygiene.
What I'd do →
- No default for values the caller must decide, such as
nameandvpc_id. - A safe default for everything else.
- Add
description,typeandvalidationto each.
Why → A missing required variable fails fast with "No value for required variable", while a risky default silently deploys the wrong thing. Never default something like publicly_accessible to true.
Interview tip → Secure defaults, explicit opt-in for risk.
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 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?Advanced