Skip to main content
tfts provides a type-safe way to work with Terraform’s for_each and dynamic blocks using the TerraformIterator class.

TerraformIterator

A TerraformIterator 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.
To access specific types from the value, use the getter methods:
  • iterator.getString(path)
  • iterator.getNumber(path)
  • iterator.getBoolean(path)
  • iterator.getList(path)
You can also retrieve all keys or values using iterator.keys() and iterator.values().

Resource for_each

To use an iterator with a resource, pass it to the forEach property.

Dynamic Blocks

Dynamic blocks allow you to generate multiple nested blocks based on a collection. Use the iterator.dynamic() method to define the block structure.

TerraformCount

For simple numeric loops, use TerraformCount.
The count.index property provides the current iteration index (0, 1, 2, …).