Difference between for_each in resource block vs for_each in Dynamic block

  1. If we use for_each in resource, it requires input as either map or set of strings. If we provide input like list of maps, it gives an error.

  2. If we use for_each under dynamic block, it accepts list of maps and other data types.

Is there a documentation that specifically states about this different behavior of for_each?

Could be wrong, but maybe it’s because blocks can be repeated while resources can’t (i.e., resources must have a unique identifier)?

Typically, if you have a list of maps, you’ll need to do a map or list comprehension to flatten it into something with a reliably unique key.

A block within a resource could itself represent one of multiple data types: an object, a set, a list or a map, so iterating over those same types makes sense. A resource must be uniquely addressable with the for_each expression providing the index, and there is no unique key to associate with a collection of maps.

If you want an ordered series of resource instances, that is what count is used for. Most of the time however users do not actually want an ordered series of resource instances which can create a difficult situation to fix later on, so the types used in resource for_each also ensure that ordering is never implied.