You need one module instance per team, and adding or removing a team must not touch others.
AdvancedScenarioModules and Code Structure
Answer
Situation read → Repeated module instances keyed by a stable identity.
What I'd do → Use for_each on the module call with team names as keys.
module "team_sandbox" {
source = "./modules/team-sandbox"
for_each = var.teams # map keyed by team name
name = each.key
budget = each.value.budget
}
Why → State addresses become module.team_sandbox["payments"]..., so removing one key destroys only that team's resources. Keys must be known at plan time.
Interview tip → for_each on modules requires providers passed from the root, not defined inside.
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