Aug 18 / Barry Luijbregts

How to reduce your Azure costs (shutdown Azure VM properly)

In Microsoft Azure, it is sometimes difficult to see when services are costing you money and when they don’t.

For instance: when you shut down an Azure VM, from the machine itself, it is still costing you money. The Azure Virtual Machine pricing page tells us this:
To shut down an Azure VM completely and have it not costing you any money, we need to bring it to the state Stopped (Deallocated).

This doesn’t happen when you shut down the VM from within the VM. That will only bring the VM to the Stopped state.

There are two ways to get the VM in this state:

1. In the (old) Azure Management portal

Go to the old Azure Management Portal: http://portal.azure.com.

Navigate to the VM you want to stop, and click SHUT DOWN:
You can do the same thing from the current Azure Portal. 

The downside to actually shutting down is that all the resources allocated with your VM, will be released. This includes The virtual IP address assigned to it.

To prevent this from happening, make sure that the Cloud service which supports the VM contains at least one VM that is not in the state Stopped (Deallocated).

2. In Azure PowerShell

If you haven’t done this already; install and configure Azure PowerShell:

1. Download and install Azure PowerShell though the Web Platform Installer
2. Open the PowerShell command prompt (as an Administrator)
3. Run Add-AzureAccount. This will pop-up a logon screen, in which you should enter the credentials of your Azure account. After logging on, your subscription details have been added to PowerShell.
4. If you have more than one subscription, you should select the one you want to use by running Select-AzureSubscription “Subscription name”

You can find additional information on how to install and configure Azure PowerShell here

Next, we can query the Azure VM’s we have to see which state they are in by running Get-AzureVM. 
This will fetch the data of all the VM’s you have in the currently selected subscription:

1 PS C:\> Get-AzureVM
3 ServiceName Name Status
4 ----------- ---- ------
5 vs2015-sbjna2i5 vs2015 ReadyRole

Now, you can shut down the VM to deallocated state by running the command Stop-AzureVM:

1 PS C:\> Stop-AzureVM -ServiceName "vs2015-sbjna2i5" -Name "vs2015"
 2 VERBOSE: 8:42:20 AM - Completed Operation: Get Deployment
 3 
 4 Confirm
 5 The specified virtual machine is the last virtual machine in this deployment. Continuing will result in a new IP
 6 address for your deployment. To shut down without losing the deployment IP use -StayProvisioned.
 7 [Y] Yes [N] No [S] Suspend [?] Help (default is "Y"): Y
 8 VERBOSE: 8:42:34 AM - Begin Operation: Stop-AzureVM
 9 VERBOSE: 8:43:05 AM - Completed Operation: Stop-AzureVM
10 
11 OperationDescription OperationId OperationStatus
12 -------------------- ----------- ---------------
13 Stop-AzureVM c308967e-fe9b-ba9f-b60d-63036ab3b0f0 Succeeded
14 
15 
16 PS C:\> Get-AzureVM
17 
18 ServiceName Name Status
19 ----------- ---- ------
20 vs2015-sbjna2i5 vs2015 StoppedDeallocated

You can see in the PowerShell above, that you can also shut down without getting to the deallocated state by using the flag –StayProvisioned.

The command used to work the other way around, being that you would have to use the flag –Force to go to deallocated state.

Now, if we visit the Azure portal again, we can see that the VM is in Stopped (Deallocated) state and is therefor no longer costing you any money on compute and license costs.

Be aware that you are still paying for the storage on which the VM’s HDD is stored and for the cloud service that you might still have running.
Created with