如何在 terraform 中正确使用 each.value in for_each?
How to correctly use each.value in for_each in terraform?
我正在尝试为每个用户创建一个文件,并将分配给他们的项目作为文件的内容。我很容易得到“${each.value}”,因为它是一个字符串列表。请问有什么解决办法吗?
locals {
data = {
"project1" = {
user_assigned = ["user1", "user2", "user3"]
}
"project2" = {
user_assigned = ["user2", "user3", "user4"]
}
}
`
resource "local_file" "foo" {
for_each = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
content = "${each.value}"
filename = "${each.key}"
}
错误:
on test.tf line 85, in resource "local_file" "foo":
85: content = "${each.value}"
|----------------
| each.value is list of string with 2 elements
content
必须是字符串,但在您的情况下它是一个列表,例如["project1", "project2"]
。将其转换为字符串的一种方法是通过 jsonencode:
resource "local_file" "foo" {
for_each = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
content = jsonencode(each.value)
filename = each.key
}
我正在尝试为每个用户创建一个文件,并将分配给他们的项目作为文件的内容。我很容易得到“${each.value}”,因为它是一个字符串列表。请问有什么解决办法吗?
locals {
data = {
"project1" = {
user_assigned = ["user1", "user2", "user3"]
}
"project2" = {
user_assigned = ["user2", "user3", "user4"]
}
}
`
resource "local_file" "foo" {
for_each = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
content = "${each.value}"
filename = "${each.key}"
}
错误:
on test.tf line 85, in resource "local_file" "foo":
85: content = "${each.value}"
|----------------
| each.value is list of string with 2 elements
content
必须是字符串,但在您的情况下它是一个列表,例如["project1", "project2"]
。将其转换为字符串的一种方法是通过 jsonencode:
resource "local_file" "foo" {
for_each = transpose(zipmap(keys(local.data), values(local.data)[*].user_assigned))
content = jsonencode(each.value)
filename = each.key
}