首页 > 解决方案 > 从 terraform 资源创建记录列表

问题描述

在 Terraform 中,我试图从创建的 DNS A 记录中创建 DNS SRV 记录。我想用 aws_route53_record.etcd 名称中的名称填充记录,但在引用资源名称时遇到错误。

有没有简单的方法来实现这一目标?

# This resource works without errors
resource "aws_route53_record" "etcd" {

  count = length(var.control_plane_private_ips)
  
  zone_id = data.aws_route53_zone.test.zone_id
  name    = "etcd-${count.index}.${data.aws_route53_zone.test.name}"
  type    = "A"
  ttl     = 60
  records = var.control_plane_private_ips

}

resource "aws_route53_record" "etcd_ssl_tcp" {

  zone_id = data.aws_route53_zone.test.zone_id
  name    = "_etcd-server-ssl._tcp.${data.aws_route53_zone.test.name}"
  type    = "SRV"
  ttl     = 60

  # code is producing an error here. Would like to add the names to the records
  for_each = [for n in aws_route53_record.etcd : { name = n.name }]
  records = [
    "0 10 2380 ${each.value.name}.${data.aws_route53_zone.test.name}"
  ]
 
}

运行 a 时terraform plan,出现以下错误。

Error: Invalid for_each argument

  on main.tf line 55, in resource "aws_route53_record" "etcd_ssl_tcp":
  55:   for_each = [for n in aws_route53_record.etcd : { name = n.name }]

The given "for_each" argument value is unsuitable: the "for_each" argument
must be a map, or set of strings, and you have provided a value of type tuple.

标签: terraform

解决方案


根据反馈想出来的。谢谢您的帮助!

resource "aws_route53_record" "etcd_ssl_tcp" {

  zone_id = data.aws_route53_zone.kubic.zone_id
  name    = "_etcd-server-ssl._tcp.${data.aws_route53_zone.test.name}"
  type    = "SRV"
  ttl     = 60

  records = [
      for n in aws_route53_record.etcd :
      "0 10 2380 ${n.name}"
  ]

}

推荐阅读