Can we detach VM NIC when there is a zone failure?

Andy Lau Pik Hui 70 Reputation points
2024-10-17T02:11:39.26+00:00

Hello Microsoft Azure Community,

When Zone 1 is having disaster and impacting VM 1, is it still possible to detach the NIC from VM 1 and attach it to VM 2 in Zone 2? Can we also automate this process.

Thanks all in advance!

Azure Virtual Network
Azure Virtual Network
An Azure networking service that is used to provision private networks and optionally to connect to on-premises datacenters.
2,776 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Sai Prasanna Sinde 6,645 Reputation points Microsoft External Staff Moderator
    2024-10-24T06:20:49.7333333+00:00

    Hi @Andy Lau Pik Hui

    Welcome to Microsoft Q&A Platform. Thank you for reaching out & hope you are doing well.

    1. If your VM1 in Zone 1 is having only 1 NIC, so it can't be detached. A VM must have at least one network interface attached to it. For your reference: https://learn.microsoft.com/en-us/azure/virtual-network/virtual-network-network-interface-vm#constraints
    2. The VM2 in Zone 2 needs to support for multiple NICs, if the size of the VM is not supported, we cannot attach the NIC to the VM2. A VM can only have as many network interfaces attached to it as the VM size supports. Please refer: https://learn.microsoft.com/en-us/azure/virtual-machines/sizes/overview?tabs=breakdownseries%2Cgeneralsizelist%2Ccomputesizelist%2Cmemorysizelist%2Cstoragesizelist%2Cgpusizelist%2Cfpgasizelist%2Chpcsizelist
    3. When you delete a VM, the network interfaces are detached from the VM. You can add those network interfaces to different VMs or delete them. For your reference: https://learn.microsoft.com/en-us/azure/virtual-network/virtual-network-network-interface-vm#:~:text=Deleting%20a%20VM%20doesn%27t%20delete%20the%20network%20interfaces%20that%20are%20attached%20to%20it.%20When%20you%20delete%20a%20VM%2C%20the%20network%20interfaces%20are%20detached%20from%20the%20VM.%20You%20can%20add%20those%20network%20interfaces%20to%20different%20VMs%20or%20delete%20them.
    4. If in case you need to avoid data loss and downtime, you can configure the ASR(Azure Site Recovery). In the event of a disaster, you can fail over to the replicated VM. For your reference: https://learn.microsoft.com/en-us/azure/site-recovery/azure-to-azure-how-to-enable-replication
    5. Yes, you can automate the process of detaching and attaching the NIC using Azure Automation, Azure PowerShell, or Azure CLI. You can create a script that performs the necessary steps to detach the NIC from VM 1 and attach it to VM 2.
      Please verify the below example PowerShell script:
         # Detach the NIC from VM 1
         $nic = Get-AzNetworkInterface -Name "VM1-NIC" -ResourceGroupName "RG1"
         $vm = Get-AzVM -Name "VM1" -ResourceGroupName "RG1"
         Remove-AzVMNetworkInterface -VM $vm -NetworkInterface $nic
         # Attach the NIC to VM 2
         $nic = Get-AzNetworkInterface -Name "VM1-NIC" -ResourceGroupName "RG1"
         $vm = Get-AzVM -Name "VM2" -ResourceGroupName "RG2"
         Add-AzVMNetworkInterface -VM $vm -NetworkInterface $nic
      
      For your reference: https://learn.microsoft.com/en-us/azure/virtual-machines/windows/multiple-nics?toc=%2Fazure%2Fvirtual-network%2Ftoc.json#remove-a-nic-from-an-existing-vm

    Kindly let us know if the above helps or you need further assistance on this issue.

    Thanks,

    Sai Prasanna.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.