<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>

Tal Rofe
<p>You can get the ELB-hosted Zone Id using the data source <a href="https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/elb_hosted_zone_id" rel="nofollow noreferrer"><code>aws_elb_hosted_zone_id</code></a>, as it only depends on the region where you created this ELB. Technically you can hardcode this value also because these are static values on a regional basis.</p>
<p>Official AWS Documentation on <a href="https://docs.aws.amazon.com/general/latest/gr/elb.html" rel="nofollow noreferrer">Elastic Load Balancing endpoints</a></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;
  }
}

## Add data source ## 
data &quot;aws_elb_hosted_zone_id&quot; &quot;this&quot; {}
### This will use your aws provider-level region config otherwise mention explicitly.

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.this.id ## Updated ##
    evaluate_target_health = true
  }
}
</code></pre>
<p>Out of your question scope, even though hopefully this may work but I would also suggest you look into <a href="https://kubernetes-sigs.github.io/aws-load-balancer-controller/v2.4/guide/integrations/external_dns/" rel="nofollow noreferrer">external-dns</a> for managing DNS with EKS.</p>
