Ask Your Question
2

How can I limit the application of for_each to a specific block of code in locals instead of applying it to everything?

asked 2023-06-16 14:04:49 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
3

answered 2023-06-16 14:19:02 +0000

ladyg gravatar image

You can limit the application of for_each to a specific block of code in locals by creating a sub-block using curly braces "{}" and wrapping your code inside it.

For example:

locals {
  my_instance = {
    for_each = var.instances
    my_instance_name = each.value.name
    my_instance_type = each.value.type
  }

  # This is outside the sub-block and won't be affected by for_each
  my_other_variable = "value"
}

In this example, foreach is only applied to the myinstance block and not to the myothervariable.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-16 14:04:49 +0000

Seen: 13 times

Last updated: Jun 16 '23