how to find AKS external ip using terraform data block

2/6/2022

I am trying to get ingress EXTERNAL-IP in k8s. Is there any way to get the details from terraform data block. like using data "azurerm_kubernetes_cluster" or something?

-- iluv_dev
azure-aks
kubernetes
terraform

2 Answers

2/6/2022

The solution that pops into my head (which I'm sure is not the ideal one) is to use local-exec, and then use kubectl to interrogate the ingress resource in-cluster to get the IP.

Something like this (caveat: haven't tested it and I don't use AKS, so I don't know for sure if it'll work as expected)

resource "null_resource" "example1" {
  provisioner "local-exec" {
    command = "kubectl get ingress name-of-ingress-controller-lb | jq .status.loadBalancer.ingress[0].ip"
  }
}
-- Blender Fox
Source: StackOverflow

2/7/2022

you can create the Public IP in advance with terraform and assign this IP to your ingress service:

YAML:

apiVersion: v1
kind: Service
metadata:
  annotations:
    service.beta.kubernetes.io/azure-load-balancer-resource-group: myResourceGroup # only needed if the LB is in another RG
  name: ingress-nginx-controller
spec:
  loadBalancerIP: <YOUR_STATIC_IP>
  type: LoadBalancer

Same but Terraform code:

resource "kubernetes_service" "ingress_nginx" {
  metadata {
    name = "ingress-nginx-controller"
    
    annotations {
      "service.beta.kubernetes.io/azure-load-balancer-resource-group" = "${azurerm_resource_group.YOUR_RG.name}"
    }

  spec {
    selector = {
      app = <PLACEHOLDER>
    }
    port {
      port        = <PLACEHOLDER>
      target_port = <PLACEHOLDER>
    }

    type = "LoadBalancer"
    load_balancer_ip = "${azurerm_public_ip.YOUR_IP.ip_address}"
  }
}
-- Philip Welz
Source: StackOverflow