Sitemap
Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Terraform Tips & Tricks

How to do a Terraform Nested for loop for Resources

The Guide

5 min readJul 10, 2022

--

Press enter or click to view image in full size
Image created by the author | Using elements from Canva.com | Sec 3 & 5 License Agreement

Have you ever gone through Terraform for cloud deployment for any of your projects? Be it an ML Ops, data science, or app deployment. Like many, you’ve probably encountered the issue where terraform cannot do nested for loops for their resource blocks. What if you’ve got a map within a map and terraform just isn’t flexible enough. Thankfully, we have for loops that we can create within a locals block.

So let’s cover what we’re trying to achieve: Terraform’s current meta argument “for_each” cannot be nested. As an example, we are using the snowflake resource block for schema granting. Where you can only enter privileges once. There isn’t an area where we can pass in a list for both.

The resource block in question:

Press enter or click to view image in full size
Example resource block screenshot taken from: https://registry.terraform.io/providers/Snowflake-Labs/snowflake/latest/docs/resources/schema_grant

Before going through the problem itself, if you’re not familiar with the for each meta argument, please read this great article!

--

--

Adrian Causby
Adrian Causby

Written by Adrian Causby

Software Engineer | Data Enthusiast | Certified AWS SA | Terraform Associate | Certified Tableau DA| Meetup Organiser https://www.linkedin.com/in/adriancausby/

Responses (2)