Skip to content

Generating a module block that includes for_each iteration #660

Answered by katcipis
ottramst asked this question in Q&A
Discussion options

You must be logged in to vote

You could do the for each evaluation itself using a lets block and then just reference the results on the for_each. Today it is a limitation that you can't have a obj/list comprehension when partial evaluation is involved (which happens inside the content block), but the lets block is fully evaluated (like globals). So this:

globals {
  items = [
    {
      name = "foobar"
    }
  ]
}

generate_hcl "_module.tf" {
  lets {
      item_map = {
        for item in global.items : item.name => item
      }
  }
  content {
    module "this" {
      for_each = let.item_map

      source = "some.source"
      name = each.value.name
    }
  }
}

Will generate this:

module "this" {
  for_each = {
  …

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@ottramst
Comment options

Answer selected by soerenmartius
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question Further information is requested
2 participants