Terraform - 遍历嵌套地图
Terraform - iterate over nested map
我正在尝试使用资源 - google_bigquery_dataset_iam_binding 为 Bigquery 数据集创建 IAM 绑定。要求是我使用以下结构的变量读取此资源(dataset_id、角色、成员)中的参数 -
bq_iam_role_bindings = {
"member1" = {
"dataset1" : ["role1","role2", "role5"],
"dataset2" : ["role3","role2"],
},
"member2" = {
"dataset3" : ["role1","role4"],
"dataset2" : ["role5"],
}
}
因此,我需要遍历此变量并获取在数据集上为每个成员分配的角色。这里创建的资源总数为八(针对每个成员、每个数据集和每个角色)。
我是 terraform 的新手,只了解如何在地图上应用简单的 for 循环和资源中的 for_each 循环。想了解我正在尝试做的事情怎么可能。
这是我找到的最接近的 - 我可以在其中读取嵌套映射中的值,但在我的情况下我也需要提取键。
有谁能帮忙吗
您可以 re-organize 将其放入更 for_each
友好的对象列表中,并将其存储在本地 helper_list
。
例如:
variable "bq_iam_role_bindings" {
default = {
"member1" = {
"dataset1" : ["role1","role2", "role5"],
"dataset2" : ["role3","role2"],
},
"member2" = {
"dataset3" : ["role1","role4"],
"dataset2" : ["role5"],
}
}
}
locals {
helper_list = flatten([for member, value in var.bq_iam_role_bindings:
flatten([for dataset, roles in value:
[for role in roles:
{"member" = member
"dataset" = dataset
"role" = role}
]])
])
}
这将导致 helper_list
的形式为:
[
{
"dataset" = "dataset1"
"member" = "member1"
"role" = "role1"
},
{
"dataset" = "dataset1"
"member" = "member1"
"role" = "role2"
},
{
"dataset" = "dataset1"
"member" = "member1"
"role" = "role5"
},
{
"dataset" = "dataset2"
"member" = "member1"
"role" = "role3"
},
{
"dataset" = "dataset2"
"member" = "member1"
"role" = "role2"
},
{
"dataset" = "dataset2"
"member" = "member2"
"role" = "role5"
},
{
"dataset" = "dataset3"
"member" = "member2"
"role" = "role1"
},
{
"dataset" = "dataset3"
"member" = "member2"
"role" = "role4"
},
]
上面的表格更容易与 for_each
一起使用,例如:
resource "google_bigquery_dataset_iam_binding" "reader" {
for_each = { for idx, record in local.helper_list : idx => record }
dataset_id = each.value.dataset
role = each.value.role
members = [
each.value.member
]
}
我正在尝试使用资源 - google_bigquery_dataset_iam_binding 为 Bigquery 数据集创建 IAM 绑定。要求是我使用以下结构的变量读取此资源(dataset_id、角色、成员)中的参数 -
bq_iam_role_bindings = {
"member1" = {
"dataset1" : ["role1","role2", "role5"],
"dataset2" : ["role3","role2"],
},
"member2" = {
"dataset3" : ["role1","role4"],
"dataset2" : ["role5"],
}
}
因此,我需要遍历此变量并获取在数据集上为每个成员分配的角色。这里创建的资源总数为八(针对每个成员、每个数据集和每个角色)。 我是 terraform 的新手,只了解如何在地图上应用简单的 for 循环和资源中的 for_each 循环。想了解我正在尝试做的事情怎么可能。
这是我找到的最接近的 -
您可以 re-organize 将其放入更 for_each
友好的对象列表中,并将其存储在本地 helper_list
。
例如:
variable "bq_iam_role_bindings" {
default = {
"member1" = {
"dataset1" : ["role1","role2", "role5"],
"dataset2" : ["role3","role2"],
},
"member2" = {
"dataset3" : ["role1","role4"],
"dataset2" : ["role5"],
}
}
}
locals {
helper_list = flatten([for member, value in var.bq_iam_role_bindings:
flatten([for dataset, roles in value:
[for role in roles:
{"member" = member
"dataset" = dataset
"role" = role}
]])
])
}
这将导致 helper_list
的形式为:
[
{
"dataset" = "dataset1"
"member" = "member1"
"role" = "role1"
},
{
"dataset" = "dataset1"
"member" = "member1"
"role" = "role2"
},
{
"dataset" = "dataset1"
"member" = "member1"
"role" = "role5"
},
{
"dataset" = "dataset2"
"member" = "member1"
"role" = "role3"
},
{
"dataset" = "dataset2"
"member" = "member1"
"role" = "role2"
},
{
"dataset" = "dataset2"
"member" = "member2"
"role" = "role5"
},
{
"dataset" = "dataset3"
"member" = "member2"
"role" = "role1"
},
{
"dataset" = "dataset3"
"member" = "member2"
"role" = "role4"
},
]
上面的表格更容易与 for_each
一起使用,例如:
resource "google_bigquery_dataset_iam_binding" "reader" {
for_each = { for idx, record in local.helper_list : idx => record }
dataset_id = each.value.dataset
role = each.value.role
members = [
each.value.member
]
}