for_each and dynamic blocks using the TerraformIterator class.
TerraformIterator
ATerraformIterator represents a collection that Terraform will iterate over at runtime. You can create an iterator from a list or a map.
Properties and Methods
Iterators provide access to the current element’s key and value:iterator.key: The key (for maps) or index (for lists).iterator.value: The value of the current element.
iterator.getString(path)iterator.getNumber(path)iterator.getBoolean(path)iterator.getList(path)
iterator.keys() and iterator.values().
Resource for_each
To use an iterator with a resource, pass it to theforEach property.
Dynamic Blocks
Dynamic blocks allow you to generate multiple nested blocks based on a collection. Use theiterator.dynamic() method to define the block structure.
TerraformCount
For simple numeric loops, useTerraformCount.
count.index property provides the current iteration index (0, 1, 2, …).