For_each with list(number)

On the document for for_each it says

The for_each meta-argument accepts a map or a set of strings, and creates an instance for each item in that map or set. Each instance has a distinct infrastructure object associated with it (as described above in Resource Behavior), and each is separately created, updated, or destroyed when the configuration is applied.

Is there a way to convert a list(number) to list(string).
Even if I declare a variable. as type list(string) and pass in a list of numbers (ports for example), it seems that it still knows that it’s a list of numbers and fails.

Hi @h3adache,

As the documentation you quoted mentions, this argument requires either a map(string) or a set(string) value. A list(string) value is not acceptable, because if we want to track by elements in a list then it’s better to use count for that.

If the natural keys for your resources are port numbers then it should work to convert them to strings using a for expression, like this:

  for_each = toset([for v in var.set_of_numbers : tostring(v)])

This will result in instance keys like "80" rather than like 80, but the effect would still be the same: adding a new port number to the set would cause a new instance to be created, and removing one would remove one.

Thanks @apparentlymart! the for solution is actually what I went with before I posted this lol