Terraform

The following resource tries to use for_each loop on a list of strings but it fails, why?

resource “google_compute_instance” “instances” {

  for_each = var.names
  name = each.value
}

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

for_each can applied only on collections like maps or sets so the list should be converted to a set like this: for_each = toset(var.names)