You are using a dynamic block to define multiple 'tag' blocks inside an AWS resource. The dynamic block is failing because it expects a set of objects but receives a flat list. Which function best prepares your data?
Trap 1: keys()
keys() returns only the attribute names of a map, discarding the associated values, so it cannot build the object set the dynamic block requires. It is tempting when iterating map structures, but converting a flat list into objects needs a function that constructs each object, such as a for expression.
Trap 2: distinct()
distinct() removes duplicate values from a list, returning a list of unique primitives, so it cannot restructure flat strings into the object shape a dynamic block's for_each requires. It is tempting because it does manipulate collections, and would be correct when deduplicating a list of simple values before iteration.
Trap 3: compact()
compact() only strips null and empty-string elements from a list, leaving the flat structure intact, so the dynamic block still receives strings rather than objects. It is tempting because it tidies lists, and would be correct when removing blank entries from a list of strings before passing them elsewhere.
- A
keys()
Why it fails: keys() returns only the attribute names of a map, discarding the associated values, so it cannot build the object set the dynamic block requires. It is tempting when iterating map structures, but converting a flat list into objects needs a function that constructs each object, such as a for expression.
- B
distinct()
Why it fails: distinct() removes duplicate values from a list, returning a list of unique primitives, so it cannot restructure flat strings into the object shape a dynamic block's for_each requires. It is tempting because it does manipulate collections, and would be correct when deduplicating a list of simple values before iteration.
- C
compact()
Why it fails: compact() only strips null and empty-string elements from a list, leaving the flat structure intact, so the dynamic block still receives strings rather than objects. It is tempting because it tidies lists, and would be correct when removing blank entries from a list of strings before passing them elsewhere.
- D
toset()
toset() converts the flat list into a set of objects, matching the collection type the dynamic block expects for its for_each argument. The failure stems from a type mismatch, and toset() resolves it by deduplicating and producing the required set rather than a list.