From the course: Advanced Terraform

Unlock the full course today

Join today to access over 23,100 courses taught by industry experts.

Looping with for_each

Looping with for_each - Terraform Tutorial

From the course: Advanced Terraform

Looping with for_each

- [Instructor] So far, we've seen one example of looping that uses a number as the incrementer. Count uses a number as a counter to create multiple resources. Now let's look at a data-driven looping technique, for-each. Execute git checkout 02_06, and we'll take a look at a change to main.tf. So we'll scroll down here and we have some buckets. A Google storage bucket is an object store for files. For this example, let's suppose we want to create a bucket for each environment. We already have a list of environments here, in the environment_list variable. So what this will do here, is create one bucket for every member of this list. Here we have for each equals, and then we're using the two set built-in function to convert the list to a set. for-each can only work with sets and maps, so we have to convert the list to a set. When using for-each, the set becomes the indexer, whereas count uses count.index to access the…

Contents