How to Assign an availability set to a VM in Azure
Assigning Availability Set At The Time Of Creation Of VM
At the point of creation of a VM in a cloud, you’ll get an option to put that VM in a specific Availability Set. After the creation of VM you can’t change its availability set or to put it in a new one through GUI..
Change The Availability Set Using PowerShell
A VM can only be added to an availability set when it is created. In order to change the availability set, you need to delete and recreate the virtual machine.
- Capture the following key details from the VM to be modified.
Name of the VM
$vm = Get-AzureRmVM -ResourceGroupName <Name-of-resource-group> -Name <name-of-VM> $vm.Name
VM Size
$vm.HardwareProfile.VmSize
Network primary network interface and optional network interfaces if they exist on the VM
$vm.NetworkProfile.NetworkInterfaces[0].Id
OS Disk Profile
$vm.StorageProfile.OsDisk.OsType $vm.StorageProfile.OsDisk.Name $vm.StorageProfile.OsDisk.Vhd.Uri
Disk profiles for each data disk
$vm.StorageProfile.DataDisks[<index>].Lun $vm.StorageProfile.DataDisks[<index>].Vhd.Uri
VM extensions installed
$vm.Extensions
- Delete the VM without deleting any of the disks or the network interfaces.
Remove-AzureRmVM -ResourceGroupName <resourceGroupName> -Name <vmName>
- Create the availability set if it does not already exist
New-AzureRmAvailabilitySet -ResourceGroupName <resourceGroupName> -Name <availabilitySetName> -Location "<location>"
- Recreate the VM using the new availability set
$vm2 = New-AzureRmVMConfig -VMName <VM-name> -VMSize <vm-size> -AvailabilitySetId <availability-set-id> Set-AzureRmVMOSDisk -CreateOption "Attach" -VM <vmConfig> -VhdUri <osDiskURI> -Name <osDiskName> [-Windows | -Linux] Add-AzureRmVMNetworkInterface -VM <vmConfig> -Id <nicId> New-AzureRmVM -ResourceGroupName <resourceGroupName> -Location <location> -VM <vmConfig>