azure - 从 Terraform 中的 map 列表中输出特定值

标签 azure kubernetes syntax azure-devops terraform

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

我想做的是使用数据 block 来获取 Terraform 创建的 IP,并将其传递给 terraform helm 模板以配置 Nginx 入口 Controller

当我使用数据 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
}

我将得到如下输出

 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 的值传递到我的 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
  ]
}

当我尝试使用上述方法时,它抛出一个错误

 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.

最佳答案

你应该这样做:

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文章

关于azure - 从 Terraform 中的 map 列表中输出特定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60403861/

相关文章:

powershell - 如何使用powershell在azure web应用程序中创建文件夹并添加文件

kubernetes - 无法在minikube和haproxy负载中导出IP

do block 中 case 表达式的 Haskell 语法

kubernetes - 一旦最近的作业通过,如何清理失败的 CronJob 生成的作业

javascript 自执行函数 - 不同的语法

c++ - 解构表达式

mongodb - Azure CosmosDB : enable dot notation for MongoDB API

asp.net - 将 ASP.NET 部署到 Windows Azure 云,应用程序在云上运行时出现错误

c# - Azure 队列客户端依赖项注入(inject)

kubernetes - 简单入门 Istio Helm 安装