0

Here is the challenge:

I have an iterated for-each loop to create a bunch of elements in a list:

test_count = 90
test_items = [for i in range(local.test_count) : format("test%02s", i+1)]

What I need to do is to dynimcally create a local tuple that looks like the below:

custom_items = {
    test01 = {
      name      = concat("test01", "shared value")
    }
    test02 = {
      name      = concat("test02", "shared value")
    }
  }

This is because the test01 component is used as a key later on for calling dynamically created modules.

However, I am not sure if or how one can dynamically create a list.

Obviously there will be a for_each loop or a for i in etc. in it, but I am not sure how to create the list, to be able to reference it later on.

1
  • 1
    FWIW, the example value you included in your second code block is an object, rather than a tuple. It seems like you got the answer you needed anyway, but hopefully learning the correct terminology is useful for researching similar questions in future! 😀 Commented Mar 21 at 21:22

1 Answer 1

2

Iterate over test_items to create the desired object:

locals {
  test_count = 90
  test_items = [for i in range(local.test_count) : format("test%02s", i + 1)]
  custom_items = {
    for item in local.test_items :
    item => {
      name = format("%s%s", item, "shared value")
    }
  }
}
> local.custom_items
{
  "test01" = {
    "name" = "test01shared value"
  }
  "test02" = {
    "name" = "test02shared value"
  }
  "test03" = {
    "name" = "test03shared value"
  }
  "test04" = {
    "name" = "test04shared value"
  }
  ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.