Interview Prep/Terraform/Modules and Code Structure

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.

All Terraform questions Roadmap: Terraform

More on Modules and Code Structure