使用 terraform 将对象(地图)转换为不同的列表

Transform an object (map) into a distinct list with terraform

使用 Terraform,是否可以将对象转换为类似

locals {
  data = {
    "project1" = {
      user_assigned           = ["user1", "user2", "user3"]
     }
    "project2" = {
      user_assigned           = ["user2", "user3", "user4"]
     }
  }

输出为

user1 = ["project1"]
user2 = ["project1","project2"]
user3 = ["project1","project2"]
user4 = ["project2"]

请注意,数据是一个包含键(项目)和值(user_assigned)映射的对象

您不能动态创建完全独立的变量。相反,您可以通过几种方式创建地图。一种方法是借助 transpose and zipmap:

output "test1" {
    value = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
}

导致:

test1 = tomap({
  "user1" = tolist([
    "project1",
  ])
  "user2" = tolist([
    "project1",
    "project2",
  ])
  "user3" = tolist([
    "project1",
    "project2",
  ])
  "user4" = tolist([
    "project2",
  ])
})

这是另一种方法,我分享它只是为了以防万一它很有趣——公认的答案也是一种很好的方法。

locals {
  data = {
    "project1" = {
      user_assigned = ["user1", "user2", "user3"]
    }
    "project2" = {
      user_assigned = ["user2", "user3", "user4"]
    }
  }

  project_user = flatten([
    for proj_name, proj in local.data : [
      for username in proj.user_assigned : {
        project_name = proj_name,
        username     = username
      }
    ]
  ])
}

output "example" {
  value = {
    for pu in local.project_user :
    pu.username => pu.project_name...
  }
}
Outputs:

example = {
  "user1" = [
    "project1",
  ]
  "user2" = [
    "project1",
    "project2",
  ]
  "user3" = [
    "project1",
    "project2",
  ]
  "user4" = [
    "project2",
  ]
}

我通常使用这种方法,因为像中间 local.project_user 值这样的数据结构——它是一个列表,每个 project/user 对都有一个元素——通常最终有用声明代表这些配对的资源。

关于这些项目和用户代表什么或它们可能与哪个提供商相关的问题没有任何上下文,所以我将使用 github_teamgithub_team_membership 作为示例为了说明我的意思:

resource "github_team" "example" {
  for_each = local.data

  name = each.key
}

resource "github_team_membership" "example" {
  for_each = {
    for pu in local.project_user : "${pu.username}:${pu.project_name}" => pu
  }

  team_id  = github_team.example[each.value.project_name].id
  username = each.value.username
}

许多提供程序都拥有表示两个对象之间关系的资源,因此拥有一个包含每对对象元素的中间数据结构对于这些情况来说是一个有用的构建块,然后您可以从中派生就像我在原始代码段中的 output "example" 中所做的那样向任一方向映射。