Home > Software engineering >  Issue to get all hosted zone id of AWS ELB through Terraform
Issue to get all hosted zone id of AWS ELB through Terraform

Time:03-29

I have created two AWS NLBs in the same region through terraform. Now I have to make DNS records in Route53 with type alias. 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. because, we need ELB zone id to make DNS entry with alias type. and both NLB has different zone ID. but terraform is providing only single zone id through below code.

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

The ultimate problem is: How to get the 2nd, 3rd .. zone id of ELB in the same region ??

CodePudding user response:

There is no such thing as second and third zone id for elastic load balancing. 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.

You can reuse the same data block for multiple records. What will change is the domain name which is unique for each load balancer:

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

  alias {
    name    = aws_elb.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 
  }
}

CodePudding user response:

You could add a second recource

data "aws_elb_hosted_zone_id" "secondary" { region = "target_region" }

region: (Optional) Name of the region whose AWS ELB HostedZoneId is desired. Defaults to the region from the AWS provider configuration.

  • Related