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

如何通过Terraform获取Azure PostgreSQL灵活服务器的自动分配IP?

获取Azure PostgreSQL灵活服务器自动分配IP的Terraform方案

方法1:直接从PostgreSQL资源提取IP属性

azurerm_postgresql_flexible_server资源本身暴露了private_ip_address属性,可直接引用获取主节点IP。

示例代码

在现有配置中添加输出块即可获取:

output "postgres_private_ip" {
  description = "PostgreSQL灵活服务器的私有IP地址"
  value       = azurerm_postgresql_flexible_server.postgres.private_ip_address
}

执行terraform apply后,控制台会输出该IP。若启用高可用模式,还可通过private_ip_addresses列表获取主备节点全部IP:

output "postgres_all_private_ips" {
  description = "PostgreSQL服务器所有私有IP(含高可用备节点)"
  value       = azurerm_postgresql_flexible_server.postgres.private_ip_addresses
}

方法2:从专用DNS区域的A记录查询IP

如果需要从已创建的DNS A记录中提取IP,可使用azurerm_private_dns_a_record数据源查询:

示例代码

data "azurerm_private_dns_a_record" "postgres_a_record" {
  name                = var.name
  zone_name           = azurerm_private_dns_zone.private_dns_zone_postgres.name
  resource_group_name = var.resource_group.name

  depends_on = [azurerm_postgresql_flexible_server.postgres]
}

output "postgres_ip_from_dns" {
  description = "从专用DNS A记录获取的IP地址"
  value       = data.azurerm_private_dns_a_record.postgres_a_record.records[0]
}

注意事项

  • 需确保使用的azurerm provider版本≥3.0,private_ip_address等属性在该版本后才正式支持。
  • 高可用模式下,private_ip_address返回主节点IP,private_ip_addresses返回包含备节点的IP列表。

内容的提问来源于stack exchange,提问作者Eddy

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 09:51:13