Azure Compute

Azure VM Shutdown Tips to Save Money

There are 2 options to “shutdown” a Virtual Machine (VM) running in Microsoft Azure. Each of the two methods have different affects on the cost of your Azure subscription. One way you will still get charged for the compute resources, and the other will free you from paying for the compute resources and help you reduce your overall cost. This article takes a look at these, and explains the difference between them.

[toc]

How Azure VM Billing Works

When Microsoft Azure Virtual Machines (VMs) are running they are billed on a “per hour” basis. You will only pay for the number of hours your VM is running after you create it. If you leave the VM running all month long, this means you pay for the entire month. But, if you stop and deallocate the VM, then you will only be billed for the time the VM is actually running.

The reason you are only billed for the time when the VM is running, and not when it’s deallocated, is because when it’s running is the only time actual compute hardware in the datacenter is dedicated to your VM. When the VM is deallocated, then Microsoft Azure releases the hardware reservation for your VM so it can be used by some other Azure resource that needs it.

Beware that when you “Stop” the VM by shutting down the Operating System, then the VM is still running in Microsoft Azure. In order to stop billing for the VM, you need to stop the VM to put it into a “deallocated” state. When the VM is in the “Stopped (deallocated)” state, that’s when billing will be paused. The “Stopped (deallocated)” state is discussed further down below.

Keep in mind that when the VM is deallocated you are still paying for the storage used to store the VMs operating system disk and any other data disks attached to the VM. Storage still takes up resources in the Azure datacenter so you will still be billed for those. Although storage is relatively inexpensive, so leaving a VM in your Azure Subscription that is not running and deallocated wont really cost very much.

Here’s a video overview of the topics discussed within this article on how to properly shutdown an Azure VM to save money.

Stopped vs Stopped (Deallocated)

The first method to shutdown an Azure VM, that sounds logical in the context of connecting with Remote Desktop, is to Shutdown the Operating System. In this scenario you would be connected with Remote Desktop, and when done with your work you go to the Power options within the Windows operating system and then select the Shutdown option. This will essentially “turn off” or power down the Virtual Machine (VM), and stop it from running. However, with this method, even though the VM is not running you will still be paying for the Azure VM compute resources (aka hardware allocation) in the datacenter. This will cause the Azure Portal to report the status of the VM as “Stopped”.

The second method, and the important one to remember, is to go into the Azure Portal (or use the Azure CLI or Azure PowerShell) and use Azure to Stop the VM. Instead of just shutting down the Operating System, Azure will also deallocate the compute resources allocated for the VM. This releases the compute resources to be used for another customer within Microsoft Azure. This will cause Azure to no longer charge you for the compute resources, and Azure will report the status of the VM as being in a “Stopped (Deallocated)” state.

Screenshot: VM blade in Azure Portal showing VM is Stopped (deallocated)

While an Azure VM is in the “Stopped (Deallocated)” state, you will not be charged for the VM compute resources. However, you will still need to pay for any OS and data storage disks attached to the VM.

It’s a good idea that when ever you don’t actually need the VM to be running that you Stop it using Azure so that the resources are released. While in the “Stopped (Deallocated” status, you will not be paying for the VM resources. This will really help you save money!

Use Azure CLI to Stop (Deallocate) Azure VM

You can also use the Azure CLI, or even PowerShell cmdlets to stop and deallocate Azure Virtual machines (VMs). Using the Azure Portal is convenient, but there are times when automation is a better solution.

Stopping and Deallocating an Azure VM using the Azure CLI can be done with the following commands:

# Stop Azure VM
az vm stop --name {vm name} --g {resource group name}
# Deallocate Azure VM
az vm deallocate --name {vm name} -g {resource group name}

I encourage you to check out the Quickly Start / Stop ALL VMs article I’ve previously written, as it includes more description and additional script samples for stopping Azure VMs.

Manually Shutdown VM within Azure Portal

To “properly” Stop a VM in the Azure Portal to release the resources and save money, you can follow these steps:

  1. Within the Azure Portal, navigate to the Virtual Machine blade for the desired VM.
  2. On the Overview pane, click the Stop button.
Screenshot: “Stop” button highlighted for VM blade in Azure Portal

There is one caveat to be aware of when shutting down an Azure VM so it gets placed into the Stopped (Deallocated) status. Since this causes Azure to release the server resources associated with the Virtual Machine, it not only releases the CPU and Memory resources but also the Dynamic IP Address allocation. Due to this, when you Start the VM back up again, the IP Address will likely change. If you require the IP Address to never change for your VM, then you’ll need to configure a Static IP Address for the VM.

To start up a Stopped VM, you can follow these steps:

  1. Within the Azure Portal, navigate to the Virtual Machine blade for the desired VM.
  2. On the Overview pane, click the Start button.
Screenshot: “Start” button highlighted for VM blade in Azure Portal

Another point that’s important to remember when stopping Azure VM’s and placing them into the “Stopped (Deallocated)” state is that you do still pay for the Azure Storage account usage. Remember, the Storage account is where the VM’s .vhd disk image file is stored. Stopping the VM retains all the VM’s settings / configurations, as well as the .vhd image stored in Azure Storage. As a result, you will still incur some cost for the storage, but at least you will save on the VM resources. After all, the Storage will only cost a small amount of money compared to the much higher cost of the Virtual Machine resource allocation if it were left running constantly.

Schedule VM Auto Shutdown

Manually shutting down a VM to put it in the Stopped (Deallocated) status is a great way to save cost on Azure VM’s. Although, you do need to remember to Stop the VM. This introduces a certain level of human error in the process of saving you hosting costs on your Azure VMs. As a result, Microsoft has added a scheduled auto-shutdown feature into the platform to assist you in this effort.

With the Auto-shutdown feature, you are able to configure a specific Time (with Time Zone) when Azure is to automatically shutdown the VM. When configured, the VM will automatically be stopped if it is still running at that time of day.

To configure Auto-shutdown of an Azure VM, you can follow these steps:

  1. Within the Azure Portal, navigate to the Virtual Machine blade for the desired Virtual Machine.
  2. In the list of links on the Virtual Machine blade, click on Auto-shutdown.
  3. On the Auto-shutdown pane, configure the specific TimeTime Zone, and desired notification Webhook URL settings, then click Save.
Screenshot: “Auto-shutdown” pane of VM blade with options highlighted

If you forget to Stop your VM at the end of the day, or whenever the Auto-shutdown time is configured it will get Shutdown automatically. When using a Visual Studio development VM, this can become a good thing on Friday afternoons (or any other day when you might be in a hurry) when you’re most likely to forget to shutdown the VM.

Related Articles

44 Comments

  1. Sunil June 16, 2017

    is this option available for azure gov? we are using the arm portal and don’t see the “scheduling” menu or the “auto-shutdown” option.

    1. Chris Pietschmann June 18, 2017

      If it’s not there then you probably can’t do it. Azure Gov Cloud is always a bit behind the Public Cloud on features, unfortunately.

  2. tony June 27, 2017

    Can’t find it in the Azure MSDN Subscription Holders as well. I wonder if there’s a powershell command for this?

  3. Brian July 6, 2017

    Hi Chris, is there a powershell command to create the schedule? I’d like to include it on my powershell script that provisions my test servers.

    Thanks, Brian

  4. gjohnson71 July 13, 2017

    Does the autoshutdown act like someone pulling the plug on a physical server? I have an Azure VM with SQL Server installed and I don’t want any data to get corrupted.

    1. Chris Pietschmann July 15, 2017

      It will properly shutdown the OS first. The Azure VM Agent is used for Azure to be able to “reach into” the VM and tell the OS to shutdown before it stops the Virtual Machine.

  5. Chestere baring August 7, 2017

    Hello chris, thank you for sharing this. I would also like to ask, if vm is stopped(deallocated), what we are paying will be the storage, like SSD? Or the whole related storage? Thank you

    1. Chris Pietschmann September 20, 2017

      When a VM is Stopped and Deallocated then all you are paying for is the Storage. If a VM is just Stopped, then you are still paying for the VM allocation and Storage.

  6. Abhishek Kulkarni August 8, 2017

    Hi,

    I am facing the error, “Couldn’t configure the Auto-shutdown feature. Close the virtual machine blade and try again. Error: [object Object]” while I’m using the Auto-Shutdown. How do I solve it? Do I need to have some specific role for my subscription?

    Thank you,
    Abhishek

    1. Chris Pietschmann September 20, 2017

      Here’s some help on Azure Portal errors: https://buildazure.com/2016/09/01/fixing-azure-portal-errors/

  7. Stefan August 8, 2017

    Until MS might find it necessary to allow Auto-Dealloc of a stopped VM, it is possible to use Azure Automation for this. To deallocate a stopped VM, create a powershell runbook that issues a Stop-AzureRmVM when a VM in stopped-state is detected.

    The script looks essentially like this (add login and subscription selection, then run it via an hourly schedule):

    $powerstateMask = ‘PowerState/*’
    $powerstateStopped = ‘PowerState/stopped’
    $powerstateDealloc = ‘PowerState/deallocated’

    $VM = (Get-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName -Status)

    if(!$VM) { Write-Error “VM ‘$resourceGroupName’ does not exist.” }
    else
    {
    $vmState = ($VM.Statuses | Where Code -Like $powerstateMask)[0]
    Write-Output (“Current PowerState of VM is ‘{0}'” -f $vmState.DisplayStatus)
    if($vmState.Code -eq $powerstateStopped)
    {
    # Note: Deallocation via Stop-AzureRmVM is blocking (might take 1-2 minutes)
    Write-Verbose “Going to deallocate the VM (this might take a minute)…”
    Stop-AzureRmVM -Name $vmName -ResourceGroupName $resourceGroupName -Force
    Write-Verbose “Done!”
    }
    else { Write-Verbose “All is fine!” }
    }

  8. Simon Weaver March 19, 2018

    Is there such a thing as ‘hibernate’ or ‘sleep’? So applications will still be open but the hardware will be deallocated? I’m setting up an ’emergency’ dev machine and I’d like to leave things open.

    1. Chris Pietschmann March 19, 2018

      Nothing supported in Azure did directly. You may be able to do it; I have not tried to hibernate an Azure VM before reallocating…

  9. Phanindra Pokala March 23, 2018

    Hi Chris,

    Will there be any change in IP address if i STOP and START the VM from the portal?

    Also will there be any impact on the applications installed on that server?

    Thanks in advance.

    1. Chris Pietschmann March 23, 2018

      By default, Azure VMs will have a Dynamic IP Address assigned to them so when they are Deallocated then the IP Address can/will change. However, you could configure your VMs to have Static IP Addresses, then they would not change when deallocated.

      As far as applications installed on the server, these would have no effect from deallocation itself. When stopping and deallocating a VM you are essentially doing the same as turning off the VM or machine, so you should expect normal behavior as you would expect here.

  10. rohit March 30, 2018

    Auto-Shutdown doesn’t have a time of auto-boot
    Is there a way I can set the start time as well?

    1. Chris Pietschmann April 3, 2018

      No, Auto-Shutdown does not enable you to set a startup time. You could use scripts to automate this yourself. The main purpose is for Dev/Test environments where you manually start up the VM when necessary then you don’t have to worry about shutting it down later.

    2. Carlos June 1, 2018

      There’s an app to start your Azure VM on demand, so you or your users don’t need to go to the portal. You can sing up for a free trial here:
      https://www.mycloudtoolbox.com/avmsstarter?c=buildazure.com/2017/03/16/properly-shutdown-azure-vm-to-save-money/

  11. Peter Felber June 7, 2018

    Is it possible to somehow execute a Stop(deallocate) from within the vm without extra permissions?
    eg: shutdown –deallocate

    1. Chris Pietschmann June 7, 2018

      You could use the Azure CLI or PowerShell but not without permissions to Azure.

    2. Carlos June 7, 2018

      You can delegate the permission to deallocate the VM to this web app and set up any user to deallocate the VM within the app. Check it out here: https://www.mycloudtoolbox.com/avmsanywhere?c=buildazure.com/2017/03/16/properly-shutdown-azure-vm-to-save-money/

  12. Rahul Sarkar January 28, 2019

    Hi Chris
    Thanks for this life saver article. I am getting upto speed on Azure using my company provided MSDN VS professional subscription.
    This is a tip which every beginner Azure Developer should familiarize themselves with! Thanks a bunch mate.

    1. Chris Pietschmann March 25, 2019

      Thanks Rahul! I’m really glad my article has helped you. Please subscribe to the Build Azure Weekly newsletter if you haven’t already to get weekly status updates on Microsoft Azure in your email every week. 🙂

  13. Sunil June 21, 2019

    Hi Chris,

    I have allocated Static IP for my VM.
    Now my question is if I write a script to Start and Stop the VM on a schedule, will I lose the Static IP ?

    Thanks for you article, I thought if we stop the VM, we are not paying anything, but came to know we do pay for Storage 🙂

    Sunil Nokku

    1. Chris Pietschmann July 15, 2019

      If you have a static IP configured, then deallocating your VM will not affect the IP address. With the IP set as Static, it’s reserved for your use regardless of the state of the VM.

      Always glad to help. Thanks!

  14. Mark Bell April 1, 2020

    Hi. You mentioned that deallocated resources will allow other azure users to utilise the resources while you are not. Does this mean that there could be a risk of not been able to restart the VM due to limited resource availability for some VM Sizes, for example, the N-Series GPU based VMs. Thanks

    1. Chris Pietschmann April 3, 2020

      The only way you wouldn’t be able to restart the VM would be if the Azure Region ran out of available resources for your chosen instance size / pricing tier. While possibly, not very likely, but it could happen.

      1. Mark April 3, 2020

        Chris, Thanks for your response. Given the current global pandemic, I would imagine that many companies are moving some services/workloads to the cloud which I think will increase the risk of my previous comment. I’m personnel costing VMs in European data centres and have a fear of not been able to restart some critical devices at present which are a necessity to some of our users working from home. Do you thing there is is a way to find out what percentage Microsoft oversell their available resources or a particular region?

      2. Chris Pietschmann April 3, 2020

        When you say you have VMs that are critical to your users, then you wont be shutting those down. Production resources are generally left running so you can maintain availability. If you are looking to use VM Scale Sets to have flexible scalability, then you will still set a minimum number of instances to have running. Once a VM is running, you wont lose the ability to keep that VM running. When starting up VMs, it’s very rare for there to be issues provisioning the resources in Azure, and even at that it’s only for a very short period of time. Also, “Overselling” of resources in the cloud is not something that I’ve ever even heard about, from any cloud provider. This really isn’t something that you need to worry about.

  15. thomas wrobel May 29, 2020

    Chris: after stopping a VM server will the servers public IP change upon restart? Thank You

    1. Chris Pietschmann May 29, 2020

      By default the VM will have a dynamic IP, so yes it will change. If you configure a static IP then your IP will remain the same after deallocation and reallocation of the VM.

  16. VIVEK JAIN June 21, 2020

    Hi Chris: Does Auto Shut down also deallocated the VM?

    1. Chris Pietschmann June 22, 2020

      Yes it deallocates the VM.

  17. Kapil Naker July 22, 2020

    Hello Chris,
    As you mentioned that “Stopping the VM retains all the VM’s settings / configurations, as well as the .vhd image stored in Azure Storage”. I could not find my de-allocated vm’s disk file in the Azure storage. However, I can still see the Disk export option for the VM disk and can download the VHD file . So my question is, when the VM is deallocated, does Azure internally manage the storage part of disk and other configuration or is it stored in Azure storage from where we can check using storage explorer.

    1. Chris Pietschmann July 22, 2020

      The old method of storing a .VHD file for VMs in Azure was to use an Azure Storage Account (which still can be done today), but the new way is to use Managed Disks. As a result, most Virtual Machines created today in Azure will have a “Disk” resource where the .VHD file is stored. This is what you are referring to. You will still pay for the storage of that .VHD file when the VM is “Stopped (deallocated)” just the same whether your VM is using Azure Disks or Storage Account for it.

  18. Aleksandr July 27, 2020

    Hi Chris,

    I will be creating VMs in Azure that will not be frequently used, these will have Managed SSDs for about 512Gb. As these VMs will spend significant time in the de-allocated state, I would like to estimate my costs that will be incurred for the storage of the .VHD files. Please suggest any resource that I can use to estimate these costs or provide a realistic case so I can have ballpark figure for such. Thank you.

    1. Chris Pietschmann July 28, 2020

      You can use the Azure Pricing Calculator at https://azure.com/pricing to estimate cost. For the VM… The Disk storage will be billed all month, and the compute for the VM Size will be only billed for the time you have the VM running by the hour. Any time spent in a “Stopped (deallocated)” state wont accrue towards the VM Size billing, but will still count towards the Disk storage.

  19. learners September 25, 2020

    Is VM backup process will run even if VM in shutdown state?

    1. Chris Pietschmann September 27, 2020

      Yes, as long as your back up process isn’t something running directly on the VM.

  20. allan santiago oshima October 22, 2020

    Hi Chris, how could I auto-start the VM again?

    1. Chris Pietschmann October 23, 2020

      You would have to instrument some other kind of automation using Azure Automation, Azure Function, or something else that could use the Azure CLI or other method to automatically start the VM again at a desired time.

  21. Andrea December 9, 2020

    If I auto shut down my vm, my data in sql server won’t get updated?

    1. Chris Pietschmann December 9, 2020

      Correct, if you shutdown the VM then anything running on it will be stopped.

  22. Yuga Prasad December 20, 2020

    Hi Chris,

    May i know the exact cost difference when the VM is in the Deallocated and stop state