You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Terraform for_each块下获取Azure私有端点私有IP报错求助

解决Terraform中for_each创建的Azure私有端点IP地址获取问题

问题场景

通过for_each批量创建azurerm_private_endpoint资源后,尝试通过以下output配置获取私有IP地址时触发错误:

output "private_endpoint_interface_ip" {
  value = azurerm_private_endpoint.private_endpoint_openAI[*].private_service_connection[0].private_ip_address
}

错误信息:

This object does not have an attribute named "private_service_connection".

错误原因

  1. 资源类型不匹配:使用for_each创建的资源会生成映射(map)或集合(set)类型的资源实例,而非列表,不能用[*]语法展开。[*]仅适用于count参数创建的列表型资源。
  2. 属性名称错误:azurerm_private_endpoint的私有服务连接属性为复数形式的private_service_connections,而非单数的private_service_connection,这是Terraform AzureRM provider的规范(嵌套块在状态中以复数列表形式存在)。

解决方案

根据需求选择以下两种output配置:

方案1:返回所有私有IP的列表

使用values()函数提取所有资源实例,再通过列表推导式获取每个实例的私有IP:

output "private_endpoint_interface_ip" {
  value = [for pe in values(azurerm_private_endpoint.private_endpoint_openAI) : pe.private_service_connections[0].private_ip_address]
}

方案2:返回带实例名称的IP映射

保留for_each的键名(即实例名称),返回键值对形式的结果,方便对应到具体的私有端点:

output "private_endpoint_interface_ip" {
  value = { for key, pe in azurerm_private_endpoint.private_endpoint_openAI : key => pe.private_service_connections[0].private_ip_address }
}

备选属性路径

也可以通过network_interfaces属性获取私有IP,结果与上述一致:

# 列表形式
output "private_endpoint_interface_ip" {
  value = [for pe in values(azurerm_private_endpoint.private_endpoint_openAI) : pe.network_interfaces[0].private_ip_address]
}

# 映射形式
output "private_endpoint_interface_ip" {
  value = { for key, pe in azurerm_private_endpoint.private_endpoint_openAI : key => pe.network_interfaces[0].private_ip_address }
}

内容的提问来源于stack exchange,提问作者Rajnish Kumar Soni

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.29 12:33:14