Terraform 如何将一个资源的每个值用于另一个资源
Terraform How to use for each values from one resource in another resource
我正在尝试基于简单的 csv 在 terraform cloud 中创建用户,结构如下:
email,access_level
test@gmail.com,readonly
test2@gmail.come,owners
我可以使用以下资源块轻松创建用户:
locals {
users_csv = csvdecode(file("${path.module}/users.csv"))
}
resource "tfe_organization_membership" "users_csv" {
for_each = { for r in local.users_csv : r.email => r }
organization = var.ppl_organisation
email = each.value.email
}
但是,如果我想将它们添加到团队中,那么我需要将上述资源的“id”输出到以下资源中:
resource "tfe_team_organization_member" "readonly_member" {
for_each = { for r in local.read_only : r.email => r }
team_id = each.value.access_level == "readonly" ? each.value.access_level : local.readonly_id
organization_membership_id = "${tfe_organization_membership.users_csv.id}"
}
有办法通过吗?
提前致谢。
我认为我在这里要做的第一件事是将 CSV 数据从列表中投影到地图中,如下所示:
locals {
users_csv = csvdecode(file("${path.module}/users.csv"))
users = { for r in local.users_csv : r.email => r }
}
通过像这样定义 local.users
我们可以更简洁地在配置的其他地方多次引用该值:
resource "tfe_organization_membership" "users_csv" {
for_each = local.users
organization = var.ppl_organisation
email = each.value.email
}
resource "tfe_team_organization_member" "readonly_member" {
for_each = local.users
team_id = (
each.value.access_level == "readonly" ?
each.value.access_level :
local.readonly_id
)
organization_membership_id = tfe_organization_membership.users_csv[each.key].id
}
任何设置了 for_each
的资源在表达式中表示为一个对象映射,其键与 for_each
输入映射相同,因此 tfe_organization_membership.users_csv[each.key]
表达式指的是一个其他资源的相应实例的对象表示,由映射键关联。
我正在尝试基于简单的 csv 在 terraform cloud 中创建用户,结构如下:
email,access_level
test@gmail.com,readonly
test2@gmail.come,owners
我可以使用以下资源块轻松创建用户:
locals {
users_csv = csvdecode(file("${path.module}/users.csv"))
}
resource "tfe_organization_membership" "users_csv" {
for_each = { for r in local.users_csv : r.email => r }
organization = var.ppl_organisation
email = each.value.email
}
但是,如果我想将它们添加到团队中,那么我需要将上述资源的“id”输出到以下资源中:
resource "tfe_team_organization_member" "readonly_member" {
for_each = { for r in local.read_only : r.email => r }
team_id = each.value.access_level == "readonly" ? each.value.access_level : local.readonly_id
organization_membership_id = "${tfe_organization_membership.users_csv.id}"
}
有办法通过吗?
提前致谢。
我认为我在这里要做的第一件事是将 CSV 数据从列表中投影到地图中,如下所示:
locals {
users_csv = csvdecode(file("${path.module}/users.csv"))
users = { for r in local.users_csv : r.email => r }
}
通过像这样定义 local.users
我们可以更简洁地在配置的其他地方多次引用该值:
resource "tfe_organization_membership" "users_csv" {
for_each = local.users
organization = var.ppl_organisation
email = each.value.email
}
resource "tfe_team_organization_member" "readonly_member" {
for_each = local.users
team_id = (
each.value.access_level == "readonly" ?
each.value.access_level :
local.readonly_id
)
organization_membership_id = tfe_organization_membership.users_csv[each.key].id
}
任何设置了 for_each
的资源在表达式中表示为一个对象映射,其键与 for_each
输入映射相同,因此 tfe_organization_membership.users_csv[each.key]
表达式指的是一个其他资源的相应实例的对象表示,由映射键关联。