从Terraform中的地图列表中输出特定值

时间:2020-02-25 22:04:38

标签: azure kubernetes syntax azure-devops terraform

我正在尝试使用terraform来配置Azure AKS。当我使用LoadBalancer配置文件创建AKS时,它将创建具有静态公共IP的负载平衡器。我需要这些IP传递到Nginx控制器上

我要做的是我正在使用一个数据块来获取Terraform创建的IP,并将其传递给Terraform头盔模板以提供Nginx入口控制器

当我使用数据块时

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
}

我将得到如下输出

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


我想要实现的是将ip_address的值传递给我的头盔图表

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

当我尝试上述方法时,会抛出错误提示

 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.

1 个答案:

答案 0 :(得分:1)

您应该这样做:

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

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

请注意,这是针对0.12,针对0.11,请查看this文章