1

I am trying to retrieve the hostname in my Application Load Balancer that I configured as ingress.

The scenario currently is: I am deploying a helm chart using terraform, and have configured an ALB as ingress. The ALB and the Helm chart was deployed normally and is working, however, I need to retrieve the hostname of this ALB to create a Route53 record pointing to this ALB. When I try to retrieve this information, it returns null values.

According to terraform's own documentation, the correct way is as follows:

data "kubernetes_ingress" "example" {
  metadata {
    name = "terraform-example"
  }
}

resource "aws_route53_record" "example" {
  zone_id = data.aws_route53_zone.k8.zone_id
  name    = "example"
  type    = "CNAME"
  ttl     = "300"
  records = [data.kubernetes_ingress.example.status.0.load_balancer.0.ingress.0.hostname]
} 

I did exactly as in the documentation (even the provider version is the latest), here is an excerpt of my code:

# Helm release resource
resource "helm_release" "argocd" {
  name = "argocd"

  repository       = "https://argoproj.github.io/argo-helm"
  chart            = "argo-cd"
  namespace        = "argocd"
  version          = "4.9.7"
  create_namespace = true

  values = [
    templatefile("${path.module}/settings/helm/argocd/values.yaml", {
      certificate_arn = module.acm_certificate.arn
    })
  ]
}

# Kubernetes Ingress data to retrieve de ingress hostname from helm deployment (ALB Hostname)
data "kubernetes_ingress" "argocd" {
  metadata {
    name      = "argocd-server"
    namespace = helm_release.argocd.namespace
  }

  depends_on = [
    helm_release.argocd
  ]
}

# Route53 record creation
resource "aws_route53_record" "argocd" {
  name    = "argocd"
  type    = "CNAME"
  ttl     = 600
  zone_id = aws_route53_zone.r53_zone.id

  records = [data.kubernetes_ingress.argocd.status.0.load_balancer.0.ingress.0.hostname]
}

When I run the terraform apply I've get the following error:

╷
│ Error: Attempt to index null value
│ 
│   on route53.tf line 67, in resource "aws_route53_record" "argocd":
│   67:   records = [data.kubernetes_ingress.argocd.status.0.load_balancer.0.ingress.0.hostname]
│     ├────────────────
│     │ data.kubernetes_ingress.argocd.status is null
│ 
│ This value is null, so it does not have any indices.

My ingress configuration (deployed by Helm Release):

apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: argocd-server
  namespace: argocd
  uid: 646f6ea0-7991-4a13-91d0-da236164ac3e
  resourceVersion: '4491'
  generation: 1
  creationTimestamp: '2022-08-08T13:29:16Z'
  labels:
    app.kubernetes.io/component: server
    app.kubernetes.io/instance: argocd
    app.kubernetes.io/managed-by: Helm
    app.kubernetes.io/name: argocd-server
    app.kubernetes.io/part-of: argocd
    helm.sh/chart: argo-cd-4.9.7
  annotations:
    alb.ingress.kubernetes.io/backend-protocol: HTTPS
    alb.ingress.kubernetes.io/certificate-arn: >-
      arn:aws:acm:us-east-1:124416843011:certificate/7b79fa2c-d446-423d-b893-c8ff3d92a5e1
    alb.ingress.kubernetes.io/group.name: altb-devops-eks-support-alb
    alb.ingress.kubernetes.io/listen-ports: '[{"HTTPS":443}]'
    alb.ingress.kubernetes.io/load-balancer-name: altb-devops-eks-support-alb
    alb.ingress.kubernetes.io/scheme: internal
    alb.ingress.kubernetes.io/tags: >-
      Name=altb-devops-eks-support-alb,Stage=Support,CostCenter=Infrastructure,Project=Shared
      Infrastructure,Team=DevOps
    alb.ingress.kubernetes.io/target-type: ip
    kubernetes.io/ingress.class: alb
    meta.helm.sh/release-name: argocd
    meta.helm.sh/release-namespace: argocd
  finalizers:
    - group.ingress.k8s.aws/altb-devops-eks-support-alb
  managedFields:
    - manager: controller
      operation: Update
      apiVersion: networking.k8s.io/v1
      time: '2022-08-08T13:29:16Z'
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:finalizers:
            .: {}
            v:"group.ingress.k8s.aws/altb-devops-eks-support-alb": {}
    - manager: terraform-provider-helm_v2.6.0_x5
      operation: Update
      apiVersion: networking.k8s.io/v1
      time: '2022-08-08T13:29:16Z'
      fieldsType: FieldsV1
      fieldsV1:
        f:metadata:
          f:annotations:
            .: {}
            f:alb.ingress.kubernetes.io/backend-protocol: {}
            f:alb.ingress.kubernetes.io/certificate-arn: {}
            f:alb.ingress.kubernetes.io/group.name: {}
            f:alb.ingress.kubernetes.io/listen-ports: {}
            f:alb.ingress.kubernetes.io/load-balancer-name: {}
            f:alb.ingress.kubernetes.io/scheme: {}
            f:alb.ingress.kubernetes.io/tags: {}
            f:alb.ingress.kubernetes.io/target-type: {}
            f:kubernetes.io/ingress.class: {}
            f:meta.helm.sh/release-name: {}
            f:meta.helm.sh/release-namespace: {}
          f:labels:
            .: {}
            f:app.kubernetes.io/component: {}
            f:app.kubernetes.io/instance: {}
            f:app.kubernetes.io/managed-by: {}
            f:app.kubernetes.io/name: {}
            f:app.kubernetes.io/part-of: {}
            f:helm.sh/chart: {}
        f:spec:
          f:rules: {}
    - manager: controller
      operation: Update
      apiVersion: networking.k8s.io/v1
      time: '2022-08-08T13:29:20Z'
      fieldsType: FieldsV1
      fieldsV1:
        f:status:
          f:loadBalancer:
            f:ingress: {}
      subresource: status
  selfLink: /apis/networking.k8s.io/v1/namespaces/argocd/ingresses/argocd-server
status:
  loadBalancer:
    ingress:
      - hostname: >-
          internal-altb-devops-eks122-support-alb-1845221539.us-east-1.elb.amazonaws.com
spec:
  rules:
    - host: argocd.altb.co
      http:
        paths:
          - path: /
            pathType: Prefix
            backend:
              service:
                name: argocd-server
                port:
                  number: 80
1
  • I recommend using external-dns instead of managing this through TF. Commented Aug 9, 2022 at 15:23

1 Answer 1

4

The terraform datasource for Ingress is : kubernetes_ingress_v1. https://registry.terraform.io/providers/hashicorp/kubernetes/latest/docs/data-sources/ingress_v1

data "kubernetes_ingress_v1" "argocd" {
  metadata {
    name      = "argocd-server"
    namespace = helm_release.argocd.namespace
  }

  depends_on = [
    helm_release.argocd
  ]
}

This should work.

Sign up to request clarification or add additional context in comments.

3 Comments

I made the change to kubernetes_ingress_v1, and testing from the terraform console, it returns the value correctly. However, when it comes time to terraform apply, it still says that the status field is null. data.kubernetes_ingress_v1.argocd.status.0.load_balancer.0.ingress.0.hostname
Have you tried adding a depends_on for resource "aws_route53_record" ?
kubernetes_ingress_v1 rather than kubernetes_ingress helped me. I was then able to get the ingress hostname with: data.kubernetes_ingress_v1.this.status.0.load_balancer.0.ingress.0.hostname

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.