每个“for”资源具有多个属性的 Terraform 输出对象?

Terraform output object with multiple attributes for each of `for` resources?

我有 Terraform,其中的资源是用 for 创建的。通常,此资源的每个实例都有几个属性。目前,我有该资源组的一系列地图输出,但每个地图输出仅包含一个键值对。我希望我的 terraform 输出包含地图或对象的列表或地图,其中包含按资源实例分组的所有属性。我该怎么做而不使用 flatten; zipmap 等从我当前的输出构建它们?此示例使用 aws_route53_record,但这是一个通用查询:

当前代码

output "r53record_zonal_fqdn" {
  value = {
    for entry in aws_route53_record.zonal :
    entry.name => entry.fqdn
  }
}
output "r53record_zonal_records" {
  value = {
    for entry in aws_route53_record.zonal :
    entry.name => entry.records
  }
}
output "r53record_zonal_zone_id" {
  value = {
    for entry in aws_route53_record.zonal :
    entry.name => entry.zone_id
  }
}

如您所料,这会呈现三个地图,其中 aws_route53_record.zonal.name 作为键,其他属性作为值。

我想要的是将这些输出按资源分组,每个值都有一个预定义的键,例如(伪代码):

output "r53record_zonal_zone_id" {
    value = {
        for entry in aws_route53_record.zonal : {
            instance[count.index] {
                "name"    = entry.name 
                "fqdn"    = entry.fqdn
                "records" = entry.records
                "zone_id" = entry.zone_id
            }
        }
    }
}

为每个实例生成地图或地图列表。

如何才能做到这一点或类似的事情?

我在 for_each 循环中创建了一个带有两个“名称”参数的随机 route53_record 资源块,并尝试输出接近您要查找的内容。

假设“mydomain.com”是 Route53 中的域,例如....

resource "aws_route53_record" "zonal" {
for_each=toset(["site1","site2"])
  name    = each.key
  zone_id = "ABCDZONEIDSTRING"
  type    = "A"
  ttl     = "300"
  records = ["192.168.1.10"]
}



output "test" {
  value = {
    for dns, details in aws_route53_record.zonal:
    dns => ({"fqdn" = details.fqdn , "zone_id" = details.zone_id , "records" = details.records})
  }
}

这将以这种方式创建输出..

    test = {
      "site1" = {
         "fqdn" = "site1.mydomain.com"
         "records" = [
           "192.168.1.10",
         ]
         "zone_id" = "Z0630117NTQNSYTXYQ4Z"
      }

      "site2" = {
         "fqdn" = "site2.mydomain.com"
         "records" = [
          "192.168.1.10",
         ]
         "zone_id" = "Z0630117NTQNSYTXYQ4Z"
       }
    }

假设您传递了带有域名的名称值,如下所示...

for_each=toset(["site1.mydomain.com","site2.mydomain.com"])

输出看起来像

    test = {
       "site1.mydomain.com" = {
       "fqdn" = "site1.mydomain.com"
       "records" = [
        "192.168.1.10",
       ]
       "zone_id" = "ABCDMYZONEIDSTRING"
       }

       "site2.mydomain.com" = {
       "fqdn" = "site2.mydomain.com"
       "records" = [
        "192.168.1.10",
       ]
       "zone_id" = "ABCDMYZONEIDSTRING"
       }
    }