Output a specific value from list of maps in Terraform

2/25/2020

I am trying to provision Azure AKS using terraform. When I create AKS with the LoadBalancer profile it will create a load balancer with static public IP. I need these IP to pass onto Nginx controller

What I am trying to do is I am using a data block to get the IPs created by Terraform and pass it to terraform helm templates to provision Nginx ingress controller

When I use data block

data "azurerm_public_ips" "example" {
  resource_group_name = azurerm_kubernetes_cluster.aks.node_resource_group
  attached            = true
  allocation_type = "Static"
  name_prefix = "kubernetes"
  depends_on = [
  azurerm_kubernetes_cluster.aks]
}

output "ip" {
  value = data.azurerm_public_ips.example.public_ips
}

I will be getting the output as follow

 ip = [
  {
     "domain_name_label" = ""
     "fqdn" = ""
     "id" = "/subscriptions/xxxx/resourceGroups/xx/providers/Microsoft.Network/publicIPAddresses/kubernetesxx"
     "ip_address" = "00.00.00.00"
     "name" = "kubernetes-xxxxx"
  },
]

What I want to achieve is to pass the value of ip_address to my helm chart

data "helm_repository" "incubator" {
  name = "stable"
  url  = "https://kubernetes-charts.storage.googleapis.com"
}
resource "helm_release" "ingress" {
  repository = data.helm_repository.incubator.url
  chart = "nginx-ingress"
  name = "test"
  set {
    name  = "controller.service.loadBalancerIP"
  value  =data.azurerm_public_ips.example.public_ips[ip_address]
  }
  depends_on = [
  data.azurerm_public_ips.example
  ]
}

When I tried with above method it throwing an error saying

 Error: Invalid reference
on aks.tf , in output "ip":
   value = data.azurerm_public_ips.example.public_ips[ip_address]
  A reference to a resource type must be followed by at least one attribute access, specifying 
  the resource name.
-- adesh REDDY
azure
azure-devops
kubernetes
syntax
terraform

1 Answer

2/26/2020

you should do this:

element(data.azurerm_public_ips.example.public_ip, 1).ip_address

https://www.terraform.io/docs/configuration/functions/element.html

notice this is for 0.12, for 0.11 check this article

-- 4c74356b41
Source: StackOverflow