0

I want to create multiple resources using this:

resource "group" "list" {
 g_name      = group_name
 p_name      = project_name
}

The input local variable is nested:

> local.input
{
  "group" = [
    {
      "name" = "group1"
      "projects" = [
        "project1",
        "project3",
      ]
    },
    {
      "name" = "group2"
      "projects" = [
        "project18",
        "project31",
      ]
    },
  ]

I am able to transform it into a better form:

> flatten([for grp_key, group in local.input.group: [for project in group.projects: { group_name=group.name, project_name=project} ]])
[
  {
    "group_name" = "group1"
    "project_name" = "project1"
  },
  {
    "group_name" = "group1"
    "project_name" = "project3"
  },
  {
    "group_name" = "group2"
    "project_name" = "project18"
  },
  {
    "group_name" = "group2"
    "project_name" = "project31"
  },
]

I can not use for_each because it is not map neither set.

How could I create multiple resources using the for like:

resource "group" "list" {
  for item in ... : 
    g_name=item.group_name
    p_name=item.project_name
}
0

you need to use count, and with it you should be able to access item by index: https://www.terraform.io/docs/configuration/resources.html#the-count-object

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy