首页 > 解决方案 > 如何在外部数据查询中使用模块输出变量

问题描述

我正在使用 terraform 模块在 Azure 中创建专用终结点。创建私有端点后,我需要通过调用 API 创建 DNS 条目。所以我正在使用 ```data external`` 资源执行一个 python 脚本。python 脚本的输入之一是私有端点 IP。我试图通过查询使用来自私有端点模块的输出变量(private_endpoint_ip)作为python脚本的输入。这是我的代码

module "pe" {
  source = "../../
  private_endpoint_network = {resource_group_name = "test", vnet_name = "test", subnet_name = "test"}
  private_endpoint_name = "test"
  private_endpoint_resource_group_name = "test"
  location = "eastus"
  private_endpoint_resource_id  = var.storage_id
  subresource_names     = ["blob"]
}
data "external" "record" {
   depends_on = [module.pe]
   program = ["python", "${path.root}/dns.py"]
   query = {
     name = "abctest"
     ipv4 =  module.pe.private_endpoint_ip         #This is one of the output variables of pe
   }
}

如果我在代码上方执行 terraform plan,则会收到以下错误

Error: Incorrect attribute value type
│
│   on main.tf line 40, in data "external" "record":
│   40:    query = {
│   41:      name = "abctest"
│   42:      ipv4 = module.pe.private_endpoint_ip  
│   43:      type =  "dns"
│   44:    }
│     ├────────────────
│     │ module.pe.private_endpoint_ip is a list of string, known only after apply
│
│ Inappropriate value for attribute "query": element "ipv4": string required.

谁能帮我弄清楚,如何通过查询将私有端点模块的输出作为输入传递给 python 脚本?

标签: terraformterraform0.12+

解决方案


推荐阅读