【发布时间】:2023-03-30 15:58:01
【问题描述】:
我编写了一个 terraform 脚本来在 Azure 中部署 VM。我想在不同时间将多个 NIC 附加/分离到该 VM。
由于 VM 在部署后开始运行,当我尝试在 azurerm_virtual_machine 资源块内向 network_interface_ids 添加另一个 NIC Id 时出现错误。
我认为 Terraform 还不能改变 Azure 中 VM 的状态。
由于在 terraform 中看不到任何 VM-NIC 关联资源,我该如何实现?
我是云和 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 附加到这个虚拟机,所以我做了以下更改
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。
我收到此错误,它清楚地表明关闭虚拟机然后尝试附加。
错误:compute.VirtualMachinesClient#CreateOrUpdate Code="AddingOrDeletingNetworkInterfacesOnARunningVirtualMachineNotSupported" Message="具有单个网络接口的虚拟机 vm 必须先停止释放,然后才能更新为具有多个网络接口,反之亦然。"
我想知道是否有办法在 Azure 中将 NIC 热附加到 VM?
【问题讨论】:
-
首先您可以发布代码和错误消息。其次,如果您要删除 nic,则需要将其关闭。最后,根据机器的大小,您可以连接的 NIC 数量受到限制。
标签: terraform terraform-provider-azure