<p>I've managed  to automate kubernetes cluster deployment with terraform. After bringing up cluster terraform also deploys my apps to cluster using provisioning (running .sh script with local-exec). I am also adding ingress to cluster and I need to get the ingress load balancer IP once it created. preferable option is terraform output.
The way I am getting it now is running this part of code at the end of my script</p>

<pre><code>IP="$(kubectl get ingress appname --no-headers | awk '{print $3}')"
echo "Load Balancer IP $IP"
</code></pre>

<p>However this one has its issues, I need to add sleep before running this command to be sure that the IP is already assigned. and I can't be sure the added sleep time is enough.
Actually need smth like these but for my ingress loadbalancer IP</p>

<pre><code>output "google_container_cluster_endpoint" {
  value = "${google_container_cluster.k8s.endpoint}"
}

output "google_container_cluster_master_version" {
  value = "${google_container_cluster.k8s.master_version}"
}
</code></pre>

<p>I have managed to get external ingress ip in fully declarative approach. It is based on different providers including azurerm, kubernetes, helm. I am targeting Azure Kubernetes Service, but the solution is cloud agnostic.</p>
<p>Solution explanation:</p>
<blockquote>
<p>Use kubernetes provider to connect cluster after ingress creation. Kubernetes provides allows reading service data like external ip.</p>
</blockquote>
<p>Providers overview:</p>
<ul>
<li>azurerm provider is used for Azure communication
<ul>
<li>It is possible to create Kubernetes (K8s) via different provider,</li>
</ul>
</li>
<li>helm provider is used for ingress installation,
<ul>
<li>It is possible to create ingress using different approach,</li>
</ul>
</li>
<li>kubernetes provider allows me to query Load balancer service</li>
</ul>
<p><strong>Short snippet</strong></p>
<pre><code>provider &quot;kubernetes&quot; { }

provider &quot;helm&quot; { }

resource &quot;helm_release&quot; &quot;nginx-ingress&quot; {
  name             = &quot;nginx-ingress&quot;
  namespace        = &quot;nginx-ingress&quot;
  create_namespace = true
  repository       = &quot;https://kubernetes-charts.storage.googleapis.com&quot;
  chart            = &quot;nginx-ingress&quot;

  set {
    name  = &quot;controller.replicaCount&quot;
    value = &quot;2&quot;
  }
}

data &quot;kubernetes_service&quot; &quot;service_ingress&quot; {
  metadata {
    name      = &quot;nginx-ingress-controller&quot;
    namespace = &quot;nginx-ingress&quot;
  }

  depends_on = [ helm_release.nginx-ingress ] 
}

output &quot;ip&quot; {
  value = data.kubernetes_service.service_ingress.load_balancer_ingress.0.ip
}

</code></pre>
<p><strong>Complete snippet</strong></p>
<pre><code>variable &quot;subscription_id&quot; {
  type = string
}

variable &quot;client_id&quot; {
  type = string
}

variable &quot;client_secret&quot; {
  type = string
}

variable &quot;tenant_id&quot; {
  type = string
}

variable &quot;resource_location&quot;{
  type = string
}

terraform {
  required_providers {
    azurerm = {
      source = &quot;hashicorp/azurerm&quot;
      version = &quot;2.29.0&quot;
    }

    kubernetes = {
      source = &quot;hashicorp/kubernetes&quot;
      version = &quot;1.13.2&quot;
    }

    helm = {
      source = &quot;hashicorp/helm&quot;
      version = &quot;1.3.1&quot;
    }
  }
}

provider &quot;azurerm&quot; {
  subscription_id = var.subscription_id
  client_id       = var.client_id
  client_secret   = var.client_secret
  tenant_id       = var.tenant_id
  features {}
}

provider &quot;kubernetes&quot; {
  load_config_file       = &quot;false&quot;
  host                   = azurerm_kubernetes_cluster.aks.kube_config.0.host
  client_certificate     = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
  client_key             = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
  cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
}

provider &quot;helm&quot; {
  kubernetes {
    load_config_file       = &quot;false&quot;
    host                   = azurerm_kubernetes_cluster.aks.kube_config.0.host
    client_certificate     = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_certificate)
    client_key             = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.client_key)
    cluster_ca_certificate = base64decode(azurerm_kubernetes_cluster.aks.kube_config.0.cluster_ca_certificate)
  }
}

data &quot;kubernetes_service&quot; &quot;service_ingress&quot; {
  metadata {
    name      = &quot;nginx-ingress-controller&quot;
    namespace = &quot;nginx-ingress&quot;
  }

  depends_on = [ helm_release.nginx-ingress ] 
}

resource &quot;azurerm_resource_group&quot; &quot;rg&quot; {
  name     = &quot;myapp&quot;
  location = var.resource_location
}

resource &quot;azurerm_kubernetes_cluster&quot; &quot;aks&quot; {
  name                = &quot;myapp&quot;
  location            = var.resource_location
  resource_group_name = azurerm_resource_group.rg.name
  dns_prefix          = &quot;myapp&quot;
  kubernetes_version  = &quot;1.17.11&quot;

  default_node_pool {
    name            = &quot;default&quot;
    node_count      = 2
    vm_size         = &quot;Standard_B2s&quot;
    os_disk_size_gb = 30
    type = &quot;VirtualMachineScaleSets&quot;
    enable_auto_scaling = false
  }

  service_principal {
    client_id     = var.client_id
    client_secret = var.client_secret
  }

  role_based_access_control {
    enabled = true
  }
}

resource &quot;helm_release&quot; &quot;nginx-ingress&quot; {
  name             = &quot;nginx-ingress&quot;
  namespace        = &quot;nginx-ingress&quot;
  create_namespace = true
  repository       = &quot;https://kubernetes-charts.storage.googleapis.com&quot;
  chart            = &quot;nginx-ingress&quot;

  set {
    name  = &quot;controller.replicaCount&quot;
    value = &quot;2&quot;
  }

  set {
    name  = &quot;controller.nodeSelector.kubernetes\\.io/os&quot;
    value = &quot;linux&quot;
  }

  set {
    name  = &quot;defaultBackend.nodeSelector.kubernetes\\.io/os&quot;
    value = &quot;linux&quot;
  }
}

output &quot;ip&quot; {
  value = data.kubernetes_service.service_ingress.load_balancer_ingress.0.ip
}

</code></pre>
