In the previous post, we got an overview of the Terraform data types — Terraform: introduction to data types — primitives and complex.
Now let’s see how these types can be used in loops.
Terraform supports three types of cycles:
count: the simplest, used with a given number or with a functionlength(); uses indexes from alistormapfor iteration- suitable for creating identical resources that will not change
for_each: has more options, used with amaporset, uses sequence key names to iterate- suitable for creating resources of the same type, but with the ability to set different parameters
for: used to filter and transform objects withlists,sets,tuplesormaps; can be used in conjunction with operators and functions lieif,join,replace,lower, orupper