Interview Prep/Terraform/Modules, Variables, Sensitive Values and Workspaces

A module call with `for_each = var.services` fails: `The "for_each" set includes values derived from resource attributes that cannot be determined until apply` only on first apply.

IntermediateTroubleshootingModules, Variables, Sensitive Values and Workspaces
Answer

Likely causes → var.services (a root variable) is fine, but you merged it with something unknown (for example merge(var.services, { for k, v in aws_ecr_repository.r : k => ... })).

Diagnose → terraform console with the plan state; trace which part is unknown.

Fix → Keep the for_each keys from static data (keys(var.services)) and pass resource values inside each element.

Prevent → Separate "which instances" (static keys) from "their config" (can be dynamic).

All Terraform questions Roadmap: Terraform

More on Modules, Variables, Sensitive Values and Workspaces