Azure VM - Add/Attach The Availability Set To The Existing VM

Once we've created an Azure VM without an availability set, later if we want to add it to an availability set there is no obvious option on the Azure portal. We can add an availability set only while creating the VM.
 
The problem comes when we try to add always-on on IaaS SQL instance. One of the pre-requisites is IaaS VMs should be on an availability set. So to enable always-on on the existing IaaS VM either we have to re-create the VM or need to create a new one & install the required s/w from scratch.
 
To resolve this restriction and add an availability group to the existing Azure VM, we can follow the below steps sequentially,
  1. Get the list of attached OS + Data disk of existing VM
  2. Get the Network interface details.
  3. Remove the Azure Disk Encryption (ADE) if it's enabled.
  4. Remove the Azure VM (delete it)
  5. Now create the new VM with the same name & with Availability set.
  6. After re-creating the VM change the OS disks & Data disk [as noted in step-1].
  7. Start the VM and try to access it. [this VM start will take some time]
  8. Once you're able to access it & verify it's working, you can start enabling ADE (if required)
  9. The above-mentioned process is all manual and it will take some time. There is a well known PowerShell script to automate this. [except disabling & removing ADE]
Here is the PowerShell script to add/attach availability set to existing VM. 
  1. # Set variables    
  2.     $resourceGroup = "<<your-resource-group>>"    
  3.     $vmName = "<<your-vm-name>>"    
  4.     $newAvailSetName = "<<your-availability-set-name>>"    
  5.   
  6. # Get the details of the VM to be moved to the Availability Set    
  7.     $originalVM = Get-AzVM `    
  8.        -ResourceGroupName $resourceGroup `    
  9.        -Name $vmName    
  10.   
  11. # Create new availability set if it does not exist    
  12.     $availSet = Get-AzAvailabilitySet `    
  13.        -ResourceGroupName $resourceGroup `    
  14.        -Name $newAvailSetName `    
  15.        -ErrorAction Ignore    
  16.     if (-Not $availSet) {    
  17.     $availSet = New-AzAvailabilitySet `    
  18.        -Location $originalVM.Location `    
  19.        -Name $newAvailSetName `    
  20.        -ResourceGroupName $resourceGroup `    
  21.        -PlatformFaultDomainCount 2 `    
  22.        -PlatformUpdateDomainCount 2 `    
  23.        -Sku Aligned    
  24.     }    
  25.       
  26. # Remove the original VM    
  27.     Remove-AzVM -ResourceGroupName $resourceGroup -Name $vmName        
  28.   
  29. # Create the basic configuration for the replacement VM.     
  30.     $newVM = New-AzVMConfig `    
  31.        -VMName $originalVM.Name `    
  32.        -VMSize $originalVM.HardwareProfile.VmSize `    
  33.        -AvailabilitySetId $availSet.Id    
  34.    
  35. # For a Linux VM, change the last parameter from -Windows to -Linux     
  36.     Set-AzVMOSDisk `    
  37.        -VM $newVM -CreateOption Attach `    
  38.        -ManagedDiskId $originalVM.StorageProfile.OsDisk.ManagedDisk.Id `    
  39.        -Name $originalVM.StorageProfile.OsDisk.Name `    
  40.        -Windows    
  41.   
  42. # Add Data Disks    
  43.     foreach ($disk in $originalVM.StorageProfile.DataDisks) {     
  44.     Add-AzVMDataDisk -VM $newVM `    
  45.        -Name $disk.Name `    
  46.        -ManagedDiskId $disk.ManagedDisk.Id `    
  47.        -Caching $disk.Caching `    
  48.        -Lun $disk.Lun `    
  49.        -DiskSizeInGB $disk.DiskSizeGB `    
  50.        -CreateOption Attach    
  51.     }    
  52.       
  53. # Add NIC(s) and keep the same NIC as primary    
  54.     foreach ($nic in $originalVM.NetworkProfile.NetworkInterfaces) {        
  55.     if ($nic.Primary -eq "True")    
  56.         {    
  57.             Add-AzVMNetworkInterface `    
  58.             -VM $newVM `    
  59.             -Id $nic.Id -Primary    
  60.             }    
  61.         else    
  62.             {    
  63.               Add-AzVMNetworkInterface `    
  64.               -VM $newVM `    
  65.               -Id $nic.Id     
  66.                 }    
  67.     }    
  68.   
  69. # Recreate the VM    
  70.     New-AzVM `    
  71.        -ResourceGroupName $resourceGroup `    
  72.        -Location $originalVM.Location `    
  73.        -VM $newVM `    
  74.        -DisableBginfoExtension    
With this, we're good to add the availability set to the existing Azure VM.
 
The above steps & PowerShell script was tested on Jun/July 2020. 
 
Reference
  • https://docs.microsoft.com/en-us/azure/virtual-machines/windows/change-availability-set