簡體   English   中英

通過 Terraform 獲取 AWS ELB 的所有托管區域 ID 的問題

[英]Issue to get all hosted zone id of AWS ELB through Terraform

我通過 terraform 在同一區域創建了兩個 AWS NLB。現在我必須在 Route53 中使用類型別名創建 DNS 條記錄。 但是有錯誤。

Error: [ERR]: Error building changeset: InvalidChangeBatch: [Tried to create an alias that targets 11111111111111111111111-xxxxxxxxxxxx.elb.eu-west-2.amazonaws.com., type A in zone ZHURV0000000, but the alias target name does not lie within the target zone] status code: 400, request id: 2xxxxxxxxxxxxxxxxx

當我只有一個 NLB 時,它工作正常。 因為,我們需要 ELB 區域 ID 來創建別名類型為 DNS 的條目。 並且兩個 NLB 都有不同的區域 ID。 但 terraform 通過以下代碼僅提供單個區域 ID。

data "aws_elb_hosted_zone_id" "main" {}

我從以下鏈接參考: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_hosted_zone_id

最終的問題是:如何獲取同一區域ELB的2nd,3rd.. zone id?

彈性負載均衡沒有第二個和第三個zone id之類的東西。 每個地區都有一個 ID,實際上您可以從此處獲取 ID: https://docs.aws.amazon.com/general/latest/gr/elb.html

您可以為多個記錄重復使用相同的data塊。 將改變的是每個負載均衡器唯一的域名:

resource "aws_route53_record" "www" {
  ...
  type    = "A"

  alias {
    name    = aws_lb.my_network_load_balancer.dns_name # This changes based on load balancer
    zone_id = data.aws_elb_hosted_zone_id.main # The remains the same for each record 
  }
}

更新:

data "aws_elb_hosted_zone_id" "main" {}不適用於網絡負載均衡器。 我們可以通過引用aws_elb資源的屬性來獲取 canoical 托管區域 ID: aws_lb.my.network_load_balancer.zone_id

最后我得到了以下 NLB DNS 條目的解決方案:在這里,我從 NLB 名稱中獲取區域 ID。

注意: “aws_elb_hosted_zone_id”將為您提供 ALB 的區域 ID,而不是 NLB

  resource "aws_route53_zone" "this" {
  name = lower("${var.base_domain_name}")
}

#get DNS zone
data "aws_route53_zone" "this" {
  name = lower("${var.base_domain_name}")
  depends_on = [
  aws_route53_zone.this
]
}

data "aws_lb" "my_nlb" {
  name = "my-nlb"
}

resource "aws_route53_record" "nlb_dns" {
  zone_id = data.aws_route53_zone.this.zone_id
  name    = "my-nlb-dns"
  type    = "A"
  alias {
    name                   = "my_nlb-0000000.us-east-1.elb.amazonaws.com"
    zone_id                = data.aws_lb.my_nlb.zone_id  # this is the fix
    evaluate_target_health = true
  }
}

這是我如何執行此操作的代碼段:

data "aws_lb_hosted_zone_id" "route53_zone_id_nlb" {
  region = var.region
  load_balancer_type = "network"
}
resource "aws_route53_record" "route53_wildcard" {
  depends_on = [helm_release.nginx_ingress]
  zone_id = data.terraform_remote_state.aws_remote.outputs.domain_zone_id # Replace with your zone ID
  name    = "*.${var.domain}" # Replace with your subdomain, Note: not valid with "apex" domains, e.g. example.com
  type    = "A"
  alias {
    name                   = data.kubernetes_service.nginx_ingress.status.0.load_balancer.0.ingress.0.hostname
    zone_id                = data.aws_lb_hosted_zone_id.route53_zone_id_nlb.id
    evaluate_target_health = false
  }
}

注意力!

不要混合LB 的 zone_id (它是 static 並且在此處AWS 文檔的區域之間不同)和Route 53 區域本身的 zone_id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM