使用 Terraform 将 NIC 附加到 Azure 中的 运行 VM

Attach NIC to a running VM in Azure using Terraform

我编写了一个 terraform 脚本来在 Azure 中部署 VM。我想 Attach/detach 在不同时间将多个 NIC 连接到该 VM。 由于 VM 在部署后启动 运行,因此当我尝试将另一个 NIC Id 添加到 azurerm_virtual_machine 资源块内的 network_interface_ids 时出现错误。 我认为 Terraform 还不能改变 Azure 中 VM 的状态。 我在 terraform 中看不到任何 VM-NIC 关联资源,我该如何实现?

我是 cloud 和 terraform 的初学者,所以这可能是一个基本问题,但我无法在任何地方找到解决方案。感谢任何帮助。

我当前的代码如下所示:

resource "azurerm_network_interface" "nic1" {
  name = "nic1"
  resource_group_name = data.azurerm_resource_group.rg.name
  location = data.azurerm_resource_group.rg.location
  enable_accelerated_networking = true
  ip_configuration {
    name = "nic1-config1"
    # public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
    primary = true

  }
  ip_configuration {
    name = "nic1-config2"
    public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
  }
  tags =  var.TAGS
}



 resource "azurerm_network_interface" "nic2" {
  name = "nic2"
  resource_group_name = data.azurerm_resource_group.rg.name
  location = data.azurerm_resource_group.rg.location
  enable_accelerated_networking = true
  ip_configuration {
    name = "nic2-config1"
    # public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
    primary = true

  }
  ip_configuration {
    name = "nic2-config2"
    # public_ip_address_id = azurerm_public_ip.public_ip.id
    private_ip_address_allocation = "dynamic"
    subnet_id = data.azurerm_subnet.subnet.id
  }
  tags =  var.TAGS
}


resource "azurerm_virtual_machine" "vm" {
  name = "vm"
  resource_group_name = data.azurerm_resource_group.rg.name
  location = data.azurerm_resource_group.rg.location
  network_interface_ids = [azurerm_network_interface.nic1.id]
  vm_size = "Standard_D4S_v3"
  delete_os_disk_on_termination = true
  delete_data_disks_on_termination = true
  storage_os_disk {
    name = "os-disk"
    create_option = "FromImage"
    caching = "ReadWrite"
    managed_disk_type = "Premium_LRS"
    disk_size_gb = 32
  }

  primary_network_interface_id = azurerm_network_interface.nic.id
  
  storage_image_reference {
    id = lookup(var.VMI,data.azurerm_resource_group.rg.location)
  }


  os_profile {
    admin_username = "test"
    computer_name = "test"
    admin_password = "test"
  }
  os_profile_linux_config {
    disable_password_authentication = true


    ssh_keys {
      path = "/home/user/.ssh/authorized_keys"
      key_data = "..."
    }
  }
  tags =  var.TAGS
  
}

我成功部署了上述基础设施。

现在我想将 nic2 附加到此 VM,因此我进行了以下更改

resource "azurerm_virtual_machine" "vm" {
....
  network_interface_ids = [azurerm_network_interface.nic1.id, azurerm_network_interface.nic2.id]
...
}

https://azure.microsoft.com/en-us/blog/introducing-the-new-dv3-and-ev3-vm-sizes 说我最多可以将两个 NIC 连接到 Standard_D4s_v3。

我收到这个错误,它清楚地表明关闭 VM 然后尝试连接。

错误:compute.VirtualMachinesClient#CreateOrUpdate Code="AddingOrDeletingNetworkInterfacesOnARunningVirtualMachineNotSupported" Message="具有单个网络接口的虚拟机 vm 必须停止释放才能更新为具有多个网络接口,反之亦然。"

我想知道是否有一种方法可以在 azure 中将 NIC 热连接到 VM?

我在看这个https://github.com/Azure/azure-powershell/issues/4565

当您将另一个 NIC 附加到现有 VM 时,VM 必须处于停止(解除分配)状态。它的设计。 Terraform 没有单独的资源用于 VM 和 NIC 之间的关联。

所以据我所知,无论如何您都需要先停止 VM,然后将第二个 nic id 添加到 VM 作为您所做的更改:

resource "azurerm_virtual_machine" "vm" {
....
  network_interface_ids = [azurerm_network_interface.nic1.id, azurerm_network_interface.nic2.id]
...
}

当虚拟机处于停止状态时,再应用Terraform代码。要通过 Terraform 停止虚拟机,您可以使用 null_resource with local-exec provisioner:

执行 CLI 命令
resource "null_resource" "example2" {
  provisioner "local-exec" {
    command = "az vm stop --resource-group groupName --name vmName"
    interpreter = ["/bin/bash", "-c"]
  }
}