1

我正在通过 azurerm_virtual_machine 中的计数创建 4 个 vm,但我只想创建一个公共 IP 并将其与第一个 VM 关联?如果可以的话,这可能吗?

下面是我的模板文件

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 并将它们添加到所有虚拟机。

多个 vms-with-public-ip

设置动态 IP

附加公共 IP

4

1 回答 1

3

公共 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可以使用条件表达式执行以下操作:

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 : null
  }
}
于 2021-01-28T11:41:29.443 回答