Interview Prep/Terraform/Dependencies, Cycles and Providers

`Error: Module is incompatible with count, for_each, and depends_on` — `The module at module.legacy is a legacy module which contains its own local provider configurations`.

IntermediateTroubleshootingDependencies, Cycles and Providers
Answer

Likely causes → The child module has provider "aws" { ... } blocks inside it. Modules with their own provider blocks can't be used with count/for_each/depends_on.

Diagnose → grep -rn '^provider ' modules/legacy/

Fix → Move provider configuration to the root, declare configuration_aliases in the module if it needs multiple, and pass providers in:

module "legacy" {
  source    = "./modules/legacy"
  for_each  = var.regions
  providers = { aws = aws.mumbai }
}

Note: you still can't choose a provider per for_each instance dynamically; one module call per provider is needed.

Prevent → Module guideline: no provider blocks in reusable modules, only required_providers.

All Terraform questions Roadmap: Terraform

More on Dependencies, Cycles and Providers