azureazure-virtual-machineazure-vm-templates

Azure VM resize in same physical cluster


I am trying to resize an Azure VM using command line. The VM size is listed in the list of sizes that this VM can be resized to. But when I try to resize this VM I am unable to do because of this error

Unable to resize the VM 'XYZ' because the requested size Standard_F4s is not available in the current hardware cluster.

Is there a way we can get the information beforehand to which all sizes this VM can be resized to successfully?


Solution

  • You can use Get-AzureRmVmSize to check VM size. The sample below does not only check hardware cluster but also disk, region availability to make sure you can resize your VM.

    $rg = "azuredev-rg"
    $vmName = "A0VM"
    Get-AzureRmVMSize -ResourceGroupName $rg -VMName $vmName
    

    Note that the output is dependent on the VM status running. You can change your existing VM size when it is running. However, not all available sizes are listed if that VM is running. It means some designated sizes require stopping VM to be deallocated first (due to different hardware cluster)

    For defensive programming, below sample would be a consideration to check VM status before performing resizing

    $vm = (Get-AzureRmVM -ResourceGroupName $rg -Name $vmName -Status).Statuses
    $vm.DisplayStatus
    

    The output shows you

    Provisioning succeeded
    VM running
    

    Below is the sample PowerShell script to check VM status then perform resizing

    $rg = "azuredev-rg"
    $vmName = "A0VM"
    $newSize = "Standard_B1s"
    
    $vm = Get-AzureRmVM -ResourceGroupName $rg -Name $vmName
    $vmS = Get-AzureRmVMSize -ResourceGroupName $rg -VMName $vmName
    
    if ($vmS.Name -contains $newSize) 
    {
        Write-Output "This size is supported"
        $vm.HardwareProfile.VmSize = $newSize
        Update-AzureRmVM -VM $vm -ResourceGroupName $rg
        Write-Output "The VM size is being updated"
    }
    
    else
    {
        while($vmStatus.DisplayStatus -contains "VM running")
        {
            $vmStatus = (Get-AzureRmVM -ResourceGroupName $rg -Name $vmName -Status).Statuses
            Write-Output $vmStatus
            Write-Output "VM is being stopped"
            Start-Sleep -Seconds 3
        }
        Stop-AzureRmVM -Name $vmName -ResourceGroupName $rg -Force
        $vm.HardwareProfile.VmSize = $newSize
        Update-AzureRmVM -VM $vm -ResourceGroupName $rg
    }