使用for_each在地图中出现列表的地形问题



我试图从地图中的列表中获取每个值,并在地图中的描述中迭代它的前缀列表,但无法计算出如何。

变量:

users = {
"user1" = {
description = ""
secret_key_value = {
username       = "user1"
home_directory = "/user1/"
}
allowlist = ["200.0.0.1/32"]
},
"user2" = {
description = ""
secret_key_value = {
username       = "user2"
home_directory = "/user2/"
}
allowlist = ["200.0.0.5/32", "200.0.0.10/32"]
}
资源:

resource "aws_ec2_managed_prefix_list" "sftp" {
count = local.prefix_list_enabled ? 1 : 0
name           = "User Whitelist"
address_family = "IPv4"
max_entries    = 10
dynamic "entry" {
for_each = { 
for k, v in var.users : k => v 
if v.allowlist != "" || v.description != ""
}
content {
cidr        = entry.value.allowlist
description = entry.value.description
}
}
}

有了上面的内容,我得到了"不合适的属性值";cidr"; string required."。我需要分解allowlist变量键中的列表值,并使用描述遍历它们。有人知道我该怎么做吗?

你必须把压平你的users:

locals {
users_flat = merge([
for k,v in var.users: {
for cidr in v.allowlist:
"${k}-${cidr}" => {
description = v.description
secret_key_value = v.secret_key_value
"cidr"  = cidr
}
}
]...)   
}

然后

dynamic "entry" {
for_each = local.users_flat
content {
cidr        = entry.value.cidr
description = entry.value.description
}
}

相关内容

  • 没有找到相关文章

最新更新