You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform配置Route53指向Aurora只读实例时类型错误,如何修复

问题

尝试通过以下Terraform代码创建AWS Aurora只读实例:

resource "aws_rds_cluster_instance" "wordpress_cluster_instance_readers" {
  count              = var.number_of_instances # 3
  apply_immediately  = true
  cluster_identifier = aws_rds_cluster.wordpress_db_cluster.id
  identifier         = "wordpress-cluster-instance-reader${format(count.index + 1)}"
  instance_class     = "db.t2.small"
  engine             = aws_rds_cluster.wordpress_db_cluster.engine
  engine_version     = aws_rds_cluster.wordpress_db_cluster.engine_version

  depends_on = [aws_rds_cluster.wordpress_db_cluster]
}

随后使用以下代码配置Route53 DNS记录指向这些只读实例:

resource "aws_route53_record" "readers" {
  count = var.number_of_instances

  zone_id = var.zone_id
  name    = "reader${count.index + 1}.${var.domain}"
  type    = "CNAME"
  ttl     = "300"
  records = [element(aws_rds_cluster_instance.wordpress_cluster_instance_readers[*], count.index)]
}

出现如下错误:

│ Error: Incorrect attribute value type
│   on main.tf line 463, in resource "aws_route53_record" "readers":
│  463:   records = [element(aws_rds_cluster_instance.wordpress_cluster_instance_readers[*], count.index)]
│     ├────────────────
│     │ aws_rds_cluster_instance.wordpress_cluster_instance_readers is tuple with 3 elements
│     │ count.index is 1
│ 
│ Inappropriate value for attribute "records": element 0: string required.

请问应如何修正records参数以正确指向只读实例?

解决方案

错误原因是直接引用了RDS集群实例的对象,而Route53的records字段需要字符串类型的实例端点地址,需提取实例的endpoint属性(即RDS实例的域名地址)。

有两种修正方式:

方式一:使用element配合属性访问

resource "aws_route53_record" "readers" {
  count = var.number_of_instances

  zone_id = var.zone_id
  name    = "reader${count.index + 1}.${var.domain}"
  type    = "CNAME"
  ttl     = "300"
  records = [element(aws_rds_cluster_instance.wordpress_cluster_instance_readers[*].endpoint, count.index)]
}

方式二:直接索引实例列表并访问属性(更简洁)

因为count与实例列表长度一致,直接通过索引访问更直观:

resource "aws_route53_record" "readers" {
  count = var.number_of_instances

  zone_id = var.zone_id
  name    = "reader${count.index + 1}.${var.domain}"
  type    = "CNAME"
  ttl     = "300"
  records = [aws_rds_cluster_instance.wordpress_cluster_instance_readers[count.index].endpoint]
}

两种方式都能正确获取RDS只读实例的端点域名,满足Route53 CNAME记录对字符串类型的要求。

内容的提问来源于stack exchange,提问作者gklucard

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 10:45:40