向现有托管区域添加别名记录

问题描述 投票:0回答:1

我对 terraform 还很陌生,但我对 AWS 有很好的掌握。我有一个 main.tf,用于在不同可用区中配置 2 个 EC2、附加现有安全组并创建 ALB。当我为路由 53 添加代码而不是添加指向 ALB 的别名记录时,它会创建一个全新的 HZ。有什么办法可以在现有的 HZ 中创建一个别名吗?这是 main.tf 的相关部分:

resource "aws_route53_zone" "primary" {
  name = "<mydomain>"
}

resource "aws_route53_record" "alias_route53_record" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "<mydomain>"
  type    = "A"

  alias {
    name                  = aws_lb.load_balancer.dns_name
    zone_id               = aws_lb.load_balancer.zone_id
    evaluate_target_health = true
  }
}
amazon-web-services terraform terraform-provider-aws route53
1个回答
0
投票

当资源已存在时,有两个选项:

  1. 将其导入 terraform(如果尚未存在)
  2. 如果已经在 terraform 中,请使用数据源获取信息

在这种情况下,我假设您想要使用数据源,因此代码将如下所示:

data "aws_route53_zone" "primary" {
  name = "<mydomain>."
}

resource "aws_route53_record" "alias_route53_record" {
  zone_id = data.aws_route53_zone.primary.zone_id
  name = "<record name>" # you don't need the entire domain here, only the record name
  type = "A"

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

还值得注意的是,如果您希望代码在两个不同的可用区中工作,您可能需要使用提供者别名

© www.soinside.com 2019 - 2024. All rights reserved.