尝试创建aws_route53_record指向负载均衡器,但使用terraform不断出错

时间:2020-10-15 12:42:10

标签: amazon-web-services terraform amazon-route53 terraform-provider-aws

因为该域已经存在,所以将区域导入到我的配置中:

resource "aws_route53_zone" "example_hosted_zone" {
  name = "example.club"
}

53号公路记录:

resource "aws_route53_record" "us-battasks" {
  zone_id = aws_route53_zone.example_hosted_zone.zone_id
  name    = "us-battasks"
  type    = "CNAME"
  ttl     = "60"
  records = [aws_lb.restricted_access_lb.id]
}

resource "aws_route53_record" "us-battasksapi" {
  zone_id = aws_route53_zone.example_hosted_zone.zone_id
  name    = "us-battasksapi"
  type    = "CNAME"
  ttl     = "60"
  records = [aws_lb.restricted_access_lb.id]
}

Terraform计划显示它将创建资源,但是当我应用时,出现以下错误:

Error: [ERR]: Error building changeset: InvalidChangeBatch: [Invalid Resource Record: FATAL problem: DomainLabelTooLong (Domain label is too long) encountered with 'arn:aws:elasticloadbalancing:us-east-1:221124075124:loadbalancer', Unparseable CNAME encountered]
    status code: 400, request id: e43e5ced-957f-4bcd-83d2-1e7eaea7665b



Error: [ERR]: Error building changeset: InvalidChangeBatch: [Invalid Resource Record: FATAL problem: DomainLabelTooLong (Domain label is too long) encountered with 'arn:aws:elasticloadbalancing:us-east-1:221124075124:loadbalancer', Unparseable CNAME encountered]
    status code: 400, request id: 33d3340e-f2f2-4c95-bc96-a9de1349afc4

如果有帮助,这里是负载均衡器的Terraform代码:

resource "aws_lb" "restricted_access_lb" {
  name               = "restricted-access-lb"
  internal           = false
  load_balancer_type = "application"
  security_groups    = [aws_security_group.swarm_node_sg.id, aws_security_group.monolaunch_instance_sg.id, aws_security_group.restricted_access_sg.id]
  subnets            = [aws_subnet.public_subnet_b.id, aws_subnet.public_subnet_a.id]

  enable_deletion_protection = true   
}

2 个答案:

答案 0 :(得分:1)

aws_lb resourceid是ARN,这就是为什么您在尝试创建Route53记录时看到错误中显示的负载均衡器的ARN的原因。

相反,您应该使用dns_name attribute来代替,它会映射到负载均衡器的地址。

resource "aws_route53_record" "us-battasksapi" {
  zone_id = aws_route53_zone.example_hosted_zone.zone_id
  name    = "us-battasksapi"
  type    = "CNAME"
  ttl     = "60"
  records = [aws_lb.restricted_access_lb.dns_name]
}

相反,如果您想使用alias A record来避免进行第二次DNS查找(以及区域中顶点记录周围的问题),请改用以下命令:

resource "aws_route53_record" "us-battasksapi" {
  zone_id = aws_route53_zone.example_hosted_zone.zone_id
  name    = "us-battasksapi"
  type    = "A"
  ttl     = "60"

  alias {
    name                   = aws_lb.restricted_access_lb.dns_name
    zone_id                = aws_lb.restricted_access_lb.zone_id
    evaluate_target_health = true
  }
}

答案 1 :(得分:0)

在您的aws_route53_record中,您正在使用:

records = [aws_lb.restricted_access_lb.id]

这将尝试使CNAME成为负载均衡器的 ARN 。相反,您应该使用

records = [aws_lb.restricted_access_lb.dns_name]

理想情况下,它也应该是别名记录,而不是docs中所示的CNAME。