Skip to content

How to Iterate Through A List of Objects with Terraform's for_each function - sheldonhull.com #84

Closed

Description

@utterances-bot

How to Iterate Through A List of Objects with Terraform's for_each function

https://www.sheldonhull.com/blog/how-to-iterate-through-a-list-of-objects-with-terraforms-for-each-function/

Activity

  1. rtanner-tableau commented on Dec 9, 2020

    @rtanner-tableau

    local.users_config.users seems to reference a "users" object that I don't see defined anywhere.

  2. sheldonhull commented on Jan 8, 2021

    @sheldonhull
    Owner

    @rtanner-tableau my apologies. I had my email notifications missing so didn't see this until now. Change users to user per the yaml above. I think that was a typo in my code example. See if that works if you still are experimenting with it.

    Also for noting, I'll be trying this out soon when I have my next config project as I'm betting it's a lot more robust. Check this out from one of my all-time favorite terraform module creators.

    https://github.com/cloudposse/terraform-yaml-config

    cheers

  3. maximivanov commented on Feb 21, 2021

    @maximivanov

    Thanks Sheldon! for_each = {for ... trick is definitely an elegant solution to iterate over lists. Helped me to answer this SO question as well. Cheers!

  4. sheldonhull commented on Feb 21, 2021

    @sheldonhull
    Owner

    @maximivanov stoked to hear that. I write to help myself remember these things that took so much effort and were hard to find clear answers on. I'm always cheered to know some else found it helpful! Thanks for the follow-up. 👍

    There are a lot more advanced ways to manipulate input with flatten and others, but for my use case I've not needed that yet.

    I'm also excited by terraform-yaml-config. There's a lot of potential to support some really great simple yaml config based solutions with a lot less effort. It leverages a deep merge feature as well so you can do some pretty creative solutions with it. I'll probably start relying on that a lot more over my own yaml manipulation. Good luck!

  5. sheldonhull commented on Feb 21, 2021

    @sheldonhull
    Owner

    Also thanks to @maximivanov answer I have a new resource.

    https://github.com/hashicorp/hcl/blob/main/hclsyntax/spec.md#for-expressions

    One of the things I've needed was more programmer level understanding of the syntax. Well the docs provide some help I find them too high level. When you get into this type of object manipulation it's hard to figure out without more of a spec.

    I'm pinning this link here as well since it actually explains in detail the structure of those clauses. reading through this will probably help me understand it a lot better too.

  6. maximivanov commented on Feb 21, 2021

    @maximivanov

    Didn't know about the yaml config plugin. Looks useful! A topic for another blog post for you 😉

    Yep HCL spec can be useful. Till not long ago I didn't even realize myself both { a = b } and { a: b } are valid syntax. I'd just use whichever I copied from docs or other files in the project. Sometimes I wish there was a stricter version of tflint to enforce uniform code style.

  7. self-assigned this
    on Apr 13, 2021
  8. sheldonhull commented on May 19, 2021

    @sheldonhull
    Owner

    @kabirg referenced this in k8 project noticed this when researching webmentions. I haven't tackled EKS but once with Cloudposse module and gitpod. Good luck! Hopefully you survive all the yaml :-)

  9. kabirg commented on May 22, 2021

    @kabirg

    @sheldonhull lol appreciate it! Enjoyed your article too :)

  10. changed the title [-]How to Iterate Through A List of Objects with Terraform's for_each function[/-] [+]How to Iterate Through A List of Objects with Terraform's for_each function - sheldonhull.com[/+] on Jun 5, 2021
  11. Repository owner locked and limited conversation to collaborators on Aug 5, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions