One of the characteristics of cloud computing is agility. Agility means how rapidly you can provision cloud resources and how quickly you can change to meet a scale need. In the context of infrastructure provisioning, you sometimes choose a wrong VM size for your application. Another case when the changing need exists is to scale down the infrastructure when you want to release resources. Saying your e-commerce only needs to be boosted during a specific marketing campaign. After the campaign ends, you need to change the size back to origin to save cost.

Fortunately in Azure, you can change your VM size when you realize the existing one does not meet your need. There are some ways to do so. You can go to Azure portal to change a VM size or use PowerShell to do so.

With Azure Portal, just go to the VM you need to change its size. Under the blade, click Size.

From the list of available sizes, pick your choice. You can change VM size when your VM is running. After you save the change, your VM is automatically rebooted.  Once the VM is running, you can only pick a size which is supported in the same hardware cluster. It basically means not all VM sizes are listed when it is running. Because moving VM to another hardware cluster would be complicated. The rule of stopping VM is also applicable to your VM in availability set.

The image below shows you the PowerShell to change VM size from Standard_A0 to Standard_B1s (which provides burstability). However, as the target size is not supported, you receive the error message.

Cannot change if VM is running

To choose a different cluster, saying from A0 to F series, you first must stop your VM. When it is fully stopped and deallocated, the VM is not actually hosted on the cluster it was. It is much easier for you to ‘allocate” it to a different group of hardware cluster.

Some notes for size changing:

  1. You can change your VM size even when it is running.
  2. Not all sizes are listed if your VM is running, stop it before changing.
  3. Number of disks supported would prevent you from resizing.
  4. Regional availability is also a key.

VM in production needs a careful plan before stopping. Stopping VM could erase some configurations (e.g. dynamic IP address).

PowerShell Command

For those who are lazy at using Azure Portal to configure, PowerShell is a cool friend. The following command line can be used to check all available virtual machines that are supported for resizing. This command line does not only check hardware cluster but also disk, region availability to make sure you can resize your VM.

Note that the output is dependent on the VM status running. You can use the following one to check whether your VM is supported or not (if you are lazy of checking a long list)

If you want to retrieve running status, you need to get the VM status first by using the code below

Then you can retrieve DisplayStatus property with below

The output shows you two things:

Based on this output, you can write a PowerShell to fully check whether the size is supported and perform automated resizing.

To update the size, you need to read to VmSize property in HardwareProfile class

Below is the full code snippet to stop Azure VM, including checking if it can be stopped in Running mode. Otherwise it is stopped before resizing.

You can learn more about Azure VM resizing here.