<p>There is no direct way to get the zone_id of the load balancer from the <code>kubernetes_service</code> data source. It can only provide you the <code>ip</code> and <code>hostname</code>.
Refer: <a href="https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service#ingress" rel="nofollow noreferrer">https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service#ingress</a></p>
<p>You can get the aws elb hosted zone id using the <a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_hosted_zone_id" rel="nofollow noreferrer">data source</a> <code>aws_elb_hosted_zone_id</code>.</p>
<pre><code>data &quot;aws_elb_hosted_zone_id&quot; &quot;elb_zone_id&quot; {}

resource &quot;aws_route53_record&quot; &quot;backend_record&quot; {
  zone_id = aws_route53_zone.primary.zone_id
  name    = &quot;www.api&quot;
  type    = &quot;A&quot;
  ttl     = &quot;300&quot;

  alias {
    name                   = data.kubernetes_service.backend.status.0.load_balancer.0.ingress.0.hostname
    zone_id                = data.aws_elb_hosted_zone_id.elb_zone_id.id
    evaluate_target_health = true
  }
}

</code></pre>

CC BY-SA 4.0
2023-01-17T13:49:53.953Z
2023-01-17T13:49:53.953Z
Samit
<p>I set an EKS cluster using Terraform. I try to set Route53 record to map my domain name, to the load balancer of my cluster.</p>
<p>I set my EKS cluster:</p>
<pre><code>resource &quot;aws_eks_cluster&quot; &quot;main&quot; {
  name     = &quot;${var.project}-cluster&quot;
  role_arn = aws_iam_role.cluster.arn
  version  = &quot;1.24&quot;

  vpc_config {
    subnet_ids              = flatten([aws_subnet.public[*].id, aws_subnet.private[*].id])
    endpoint_private_access = true
    endpoint_public_access  = true
    public_access_cidrs     = [&quot;0.0.0.0/0&quot;]
  }

  tags = merge(
    var.tags,
    {
      Stack = &quot;backend&quot;
      Name  = &quot;${var.project}-eks-cluster&quot;,
    }
  )

  depends_on = [
    aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy
  ]
}
</code></pre>
<p>And I have created the following k8s service:</p>
<pre class="lang-yaml prettyprint-override"><code>apiVersion: v1
kind: Service
metadata:
    name: backend-service
spec:
    selector:
        app: dashboard-backend
    type: LoadBalancer
    ports:
        - protocol: TCP
          port: '$PORT'
          targetPort: '$PORT'
</code></pre>
<p><strong>As far as I know, once I deploy a k8s service, AWS automatically generates an ALB resource for my service.</strong> So, I set this route53 sources:</p>
<pre><code>resource &quot;aws_route53_zone&quot; &quot;primary&quot; {
  name = var.domain_name

  tags = merge(
    var.tags,
    {
      Name = &quot;${var.project}-Route53-zone&quot;,
    }
  )
}

data &quot;kubernetes_service&quot; &quot;backend&quot; {
  metadata {
    name = &quot;backend-service&quot;
  }
}


resource &quot;aws_route53_record&quot; &quot;backend_record&quot; {
  zone_id = aws_route53_zone.primary.zone_id
  name    = &quot;www.api&quot;
  type    = &quot;A&quot;
  ttl     = &quot;300&quot;

  alias {
    name                   = data.kubernetes_service.backend.status.0.load_balancer.0.ingress.0.hostname
    zone_id                = ??????
    evaluate_target_health = true
  }
}
</code></pre>
<p>I did get the load balancer host name using <code>data.kubernetes_service.backend.status.0.load_balancer.0.ingress.0.hostname</code>, but how can I get its zone ID to use in <code>zone_id</code> key?</p>

<kubernetes><amazon-web-services><terraform>
How can I set a Route53 record as an alias for EKS load balancer?
Tal Rofe
# How can I set a Route53 record as an alias for EKS load balancer?

(Asked by: username_0 on 2023-01-15)

I set an EKS cluster using Terraform. I try to set Route53 record to map my domain name, to the load balancer of my cluster.
I set my EKS cluster:
resource "aws_eks_cluster" "main" {
  name     = "${var.project}-cluster"
  role_arn = aws_iam_role.cluster.arn
  version  = "1.24"

  vpc_config {
    subnet_ids              = flatten([aws_subnet.public[*].id, aws_subnet.private[*].id])
    endpoint_private_access = true
    endpoint_public_access  = true
    public_access_cidrs     = ["0.0.0.0/0"]
  }

  tags = merge(
    var.tags,
    {
      Stack = "backend"
      Name  = "${var.project}-eks-cluster",
    }
  )

  depends_on = [
    aws_iam_role_policy_attachment.cluster_AmazonEKSClusterPolicy
  ]
}

And I have created the following k8s service:
apiVersion: v1
kind: Service
metadata:
    name: backend-service
spec:
    selector:
        app: dashboard-backend
    type: LoadBalancer
    ports:
        - protocol: TCP
          port: '$PORT'
          targetPort: '$PORT'

As far as I know, once I deploy a k8s service, AWS automatically generates an ALB resource for my service. So, I set this route53 sources:
resource "aws_route53_zone" "primary" {
  name = var.domain_name

  tags = merge(
    var.tags,
    {
      Name = "${var.project}-Route53-zone",
    }
  )
}

data "kubernetes_service" "backend" {
  metadata {
    name = "backend-service"
  }
}


resource "aws_route53_record" "backend_record" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www.api"
  type    = "A"
  ttl     = "300"

  alias {
    name                   = data.kubernetes_service.backend.status.0.load_balancer.0.ingress.0.hostname
    zone_id                = ??????
    evaluate_target_health = true
  }
}

I did get the load balancer host name using data.kubernetes_service.backend.status.0.load_balancer.0.ingress.0.hostname, but how can I get its zone ID to use in zone_id key?

**Tags:** kubernetes, amazon-web-services, terraform


---

## 1 answers

### Answer by username_1 on 2023-01-17. Score: 1

There is no direct way to get the zone_id of the load balancer from the kubernetes_service data source. It can only provide you the ip and hostname.
Refer: https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/service#ingress
You can get the aws elb hosted zone id using the data source aws_elb_hosted_zone_id.
data "aws_elb_hosted_zone_id" "elb_zone_id" {}

resource "aws_route53_record" "backend_record" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www.api"
  type    = "A"
  ttl     = "300"

  alias {
    name                   = data.kubernetes_service.backend.status.0.load_balancer.0.ingress.0.hostname
    zone_id                = data.aws_elb_hosted_zone_id.elb_zone_id.id
    evaluate_target_health = true
  }
}


---

