terraform understanding for_each vs for
Maybe one of the most often used construct in terraform are the for_each and for loop. Just to clarify these 2 constructs. In Terraform, for and for_each sound almost identical, but they serve entirely different purposes. Here is the golden rule to tell them apart: for_each is a meta-argument used to duplicate resources or modules (it creates multiple infrastructure objects). for is a looping expression used to transform values within a resource attribute (it outputs a new list or map). Feature for_each (Meta-argument) for (Expression) What it does Multiplies resources/modules. Transforms data structures. Where it lives Directly inside a resource or module block. Inside an attribute assignment (e.g., tags = ... ). Accepted Inputs set of strings, or a map . (Lists not allowed directly). list , set , tuple , map . Output Multiple cloud resources. A single list or map value. 1. The for_each Meta-argument Use for_each when you want to spin up multiple copies of the sa...