Interview Prep/Terraform/Modules and Code Structure

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.

All Terraform questions Roadmap: Terraform

More on Modules and Code Structure