How to Iterate Through A List of Objects with Terraform's for_each function
What I want to do
|
|
|
|
What I want to work:
|
|
What I’ve had to do
Now to iterate through this collection, I’ve had challenges, as the only way I’ve gotten this to work would be to ensure there was a designated key in the yaml
structure.
This provides a map object with a key/value format, instead of a collection of normal objects.
This would result in a yaml format like:
|
|
This provides the “key” for each entry, allowing Terraform’s engine to correctly identify the unique entry. This is important, as without a unique key to determine the resource a plan couldn’t run in a deterministic manner by comparing correctly the previously created resource against the prospective plan.
Another Way Using Expressions
Iterating through a map has been the main way I’ve handled this, I finally ironed out how to use expressions with Terraform to allow an object list to be the source of a for_each
operation.
This makes feeding Terraform plans from yaml
or other input much easier to work with.
Most of the examples I’ve seen confused the issue by focusing on very complex flattening or other steps. From this stack overflow answer, I experimented and finally got my expression to work with only a single line.
|
|
This results in a simple yaml object list being correctly turned into something Terraform can work with, as it defines the unique key in the expression.
Webmentions
(No webmentions yet.)