Comment by zaphar
6 hours ago
You "call" a module with arguments. You can call them multiple times. In every way that matters they are just like a function call.
I don't understand why there is a distinction between for each in a standard language vs for_each in HCL2. They are both do something by iterating over something else at runtime. The syntax isn't what matters here.
I think maybe you are mistaken in your own distinction between declarative and imperative.
Imperative: Tell the computer how to do something, by instructing it what to do.
Declarative: Tell the computer what you want the result to be like, and the computer figures out how to do it.
for_each in Terraform is very much declarative, just like modules. Compare how you'd do the same thing with JS or any other (imperative) language you know, and I think it must be clear what the difference between the two is.
Those boundaries are pretty fuzzy. The complexity of the logic with conditionals and loops in a module means that you have pretty much stopped describing what it should like and instead described how to make it look the way you want it.
I have read terraform modules where I had to execute the logic to know what got produced which moves it from your imperative description to the declarative description as far as I'm concerned.