An ECS service module needs the VPC and subnets created by a network module. How do you wire them together?
IntermediateScenarioModules and Code Structure
Answer
Situation read → Modules communicate through outputs and inputs, never by reaching inside.
What I'd do →
module "network" {
source = "./modules/network"
cidr = "10.0.0.0/16"
}
module "service" {
source = "./modules/ecs-service"
vpc_id = module.network.vpc_id
subnet_ids = module.network.private_subnet_ids
}
Why → Passing module.network.* outputs creates an implicit dependency, so ordering is automatic, and each module stays testable on its own.
Interview tip → If a module needs 10+ outputs from another, they probably belong together.
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?BeginnerHow 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