[英]Issue to get all hosted zone id of AWS ELB through Terraform
I have created two AWS NLBs in the same region through terraform. Now I have to make DNS records in Route53 with type alias.我通过 terraform 在同一区域创建了两个 AWS NLB。现在我必须在 Route53 中使用类型别名创建 DNS 条记录。 But there are an error.但是有错误。
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
It was working fine, when I had only single NLB.当我只有一个 NLB 时,它工作正常。 because, we need ELB zone id to make DNS entry with alias type.因为,我们需要 ELB 区域 ID 来创建别名类型为 DNS 的条目。 and both NLB has different zone ID.并且两个 NLB 都有不同的区域 ID。 but terraform is providing only single zone id through below code.但 terraform 通过以下代码仅提供单个区域 ID。
data "aws_elb_hosted_zone_id" "main" {}
Im taking reference from below link: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_hosted_zone_id我从以下链接参考: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_hosted_zone_id
The ultimate problem is: How to get the 2nd, 3rd.. zone id of ELB in the same region??最终的问题是:如何获取同一区域ELB的2nd,3rd.. zone id?
There is no such thing as second and third zone id for elastic load balancing.弹性负载均衡没有第二个和第三个zone id之类的东西。 There is one per region for everyone, in fact you can get the IDs from here: https://docs.aws.amazon.com/general/latest/gr/elb.html .每个地区都有一个 ID,实际上您可以从此处获取 ID: https://docs.aws.amazon.com/general/latest/gr/elb.html 。
You can reuse the same data
block for multiple records.您可以为多个记录重复使用相同的data
块。 What will change is the domain name which is unique for each load balancer:将改变的是每个负载均衡器唯一的域名:
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
}
}
Update:更新:
data "aws_elb_hosted_zone_id" "main" {}
does not work with.network load balancers. data "aws_elb_hosted_zone_id" "main" {}
不适用于网络负载均衡器。 We can get the canoical hosted zone id by referencing an attribute of aws_elb
resource: aws_lb.my.network_load_balancer.zone_id
.我们可以通过引用aws_elb
资源的属性来获取 canoical 托管区域 ID: aws_lb.my.network_load_balancer.zone_id
。
Finally I got below solution for NLB DNS entry: Here, I'm fetching zone id from the NLB name.最后我得到了以下 NLB DNS 条目的解决方案:在这里,我从 NLB 名称中获取区域 ID。
Note: "aws_elb_hosted_zone_id" will provide you zone id of the ALB, not NLB注意: “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
}
}
Here is the snippet how I do this:这是我如何执行此操作的代码段:
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
}
}
Attention!注意力!
Don't mix zone_id of LB (it is static and differs between regions here AWS document) and zone_id of Route 53 zone itself.不要混合LB 的 zone_id (它是 static 并且在此处AWS 文档的区域之间不同)和Route 53 区域本身的 zone_id 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.