我正在通过 azurerm_virtual_machine 中的计数创建 4 个虚拟机,但我只想创建一个公共 IP 并将其与第一个虚拟机关联?如果可以的话,这可能吗?
下面是我的模板文件
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
}
}
resource "azurerm_public_ip" "public_ip" {
name = ...
location = ...
resource_group_name = ...
}
resource "azurerm_virtual_machine" "vms" {
count = 4
network_interface_ids = [element(azurerm_network_interface.nics.*.id, count.index)]
}
我已经完成了以下问题,但它们创建了多个公共 IP 并将它们添加到所有虚拟机中。
答案 0 :(得分:1)
公共 IP 是使用 azurerm_public_ip 创建的:
resource "azurerm_public_ip" "public_ip" {
name = "acceptanceTestPublicIp1"
resource_group_name = azurerm_resource_group.example.name
location = azurerm_resource_group.example.location
allocation_method = "Dynamic"
}
在您的 azurerm_network_interface
中有地址,您可以使用 Conditional Expressions 执行以下操作:
resource "azurerm_network_interface" "nics" {
count = 4
name = ...
location = ...
resource_group_name = ...
ip_configuration {
subnet_id = ...
private_ip_address_allocation = "Static"
private_ip_address = ...
public_ip_address_id = count.index == 1 ? azurerm_public_ip.public_ip.id ? none
}
}