Automating & Turning Off / Shutting Down Your Azure VMs

sharepointAzureGuide

This blog posts consists of 3 parts:

  1. Automating the shut down process of your Azure VMs (virtual machines) as a backup for when we forget to do it
  2. Setting up reminders for Azure billing thresholds when it goes beyond a certain spend amount or usage
  3. Understanding what “shut down” does and doesn’t mean in Azure

A quick history on Windows Azure pricing. Until a few months ago, Azure billed for any Virtual Machine (VM) that was in your account, even if it was not running. To much ballyhoo, legions of Microsoft techies applauded the change to not charge you for a VM when it is “shut down”. This is a great feature if you do not need to have your machines always up and running (i.e., demos, test, dev), but the reality is we are all human and I have met countless people that at some point have forgotten to turn them off (I am the poster child for this particular issue unfortunately). I hope this article will help you with some proactive steps you can take to avoid any unfortunate billing issues occurring.

 

1. Automating the Shut Down Process / Shutting Down of Your Azure VMs (Virtual Machines) as a Backup for When We Forget to Turn Them Off

 

2. Setting up Email / Text / Phone Reminders for Azure Billing Usage

If you can’t get something to automatically shut down your VMs, you could at least set up an Azure billing alert email (which could also forward to a text or phone call via a 3rd party service if desired). The good thing about this is that it is a reminder for the subscription’s total usage. This means if you have a lot of charges from things other than VMs such as bandwidth, storage or other items, the alert will still trigger. I have written a detailed blog post on how to do that here.

 

3. Understanding What “Shut Down” Does and Doesn’t Mean in Azure

YES IT’S SHUT DOWN

Stop the VM from the management portal:

YesShutDownYourVM


or through Azure PowerShell

ShutDownVMAzurePowerShell

Use the Azure Service Management API (I think this can only be done in C# but not sure as I am a bit of novice on this part), YES this is shut down:

ExecuteClientActionInOCS(null, CommandRuntime.ToString(), s => this.Channel.ShutdownRole(s, this.ServiceName, CurrentDeployment.Name, roleName, PostShutdownAction.StoppedDeallocated));

NO IT’S NOT SHUT DOWN

 

DoesNotCountTurningOffVM

‘PostShutdownAction’ equal to “Stopped”. Running this code (I think in C#) will NOT shut down your VM from billing:

ExecuteClientActionInOCS(null, CommandRuntime.ToString(), s => this.Channel.ShutdownRole(s, this.ServiceName, CurrentDeployment.Name, roleName, PostShutdownAction.Stopped));

 

The Future…

The Azure Portal will soon include monitoring for VMs in their dashboard, however as of April 2014 it is not available yet. I am continuing to investigate new ways such as the preview of Azure Watch Scheduled Jobs to see if they may be other options available. If you figure out any yourself, please let me know and comment below!!!!

Posted in Azure Tagged with:
12 comments on “Automating & Turning Off / Shutting Down Your Azure VMs
  1. Question,

    when I shut down a VM using this script or simply the button in the Azure management portal I get a message stating that I will lose my VIP ip adress. How can I avoid this.
    I need the VIP IP adress for our demo of a archive appliance.

    Thanks in advance,

    Toussaint

  2. Hi Toussaint,

    I had heard of some people setting up a small VM with the cheapest/lowest CPU/memory you can pick to leave on all the time to keep an IP as one trick. But more recently I thought that Azure made a change to allow 5 reserved IPs, however I have not looked into this myself. Check out this post at the 5:24 mark from Andrew Connell & Chris Johnson’s Microsoft Cloud Show podcast and see if it helps you:

    http://www.microsoftcloudshow.com/podcast/Episodes/031-talking-azure-office-365-and-microsoft-with-microsoft-s-kirk-evans

    Cheers,

    Matthew

  3. We just went through this confusion with Azure recently. We managed to claw back about $1,000 in related fees, but only because it so happened that it was not clear that a trial SharePoint VM was not “free” for the VM itself and the pricing docs were not clear. Without that “edge”, we’d have been out the dough. Totally agree that this is not yet well-managed, but happy to see that Azure is allowing for no-cost when it’s not running – that’s what’s bringing us over from other services (we run environments on a bunch of other providers).

  4. I am happy you were able to recover some of the money. I was not able to get a hold of anyone that had any authority to do anything, the only support contact I kept getting was an offshore 3rd party company that was handling the support for Azure at the time and handled it in the stereotypical offshore model.

  5. Eddi says:

    Hi Toussaint,

    I hope you are doing fine ! About the VM and it’s IP address ….. shouldn’t be so hard to only use the DNS name … create a CNAME in your own DNS and use that one ….

  6. Michael Z says:

    Toussaint, you can now basically create a reservation, effectively giving you a static IP, even when the VM is shutdown (deallocated).

    #Checks to see if the IP is available. VM must be stopped (deallocated) for it to become available
    Test-AzureStaticVNetIP -VNetName YourNetworkNameHere -IPAddress 192.168.4.7(IP you want)
    #Prepares the could service and VM named to be assigned an IP
    $staticVM = Get-AzureVM -ServiceName yourVM -Name YourVM
    #Assigns the IP listed to the service/VM named above
    Set-AzureStaticVNetIP -VM $staticVM -IPAddress 192.168.4.7 | Update-AzureVM
    Get-AzureStaticVNetIP -VM $staticVM

  7. Thanks for sharing that with everyone! 🙂

  8. Noah says:

    Nice list! To add another to the group for automating shutdown, I recently finished an Azure Automation runbook that does this for you using tagging of resources. Those interested can find it and the details here:

    https://automys.com/library/asset/scheduled-virtual-machine-shutdown-startup-microsoft-azure

    Noah

  9. This is great Noah, I had not had time to write a Runbook yet myself so I am sure this will help a lot of folks out. Thanks for sharing.

  10. Rowan Cooney says:

    Hmm Well At least Try and Disconnect the VM from the same place where shutdown is close the VM And shut it down from the azure portal

  11. Igor Papirov says:

    If your company is looking to systematize and monitor such cost-saving automation in their Azure environments, I would recommend checking out two blogs that show a more white-glove approach to how Azure VMs can be shutdown or scaled down on a schedule:

    Shut-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-schedule-shutdowns-of-azure-vms/
    Scale-down Azure VMs on a schedule: http://cloudmonix.com/blog/how-to-automate-scaling-of-azure-vms/

  12. Hey Igor, yes I have seen your products and would recommend people check them out if they are looking for other solutions.

Leave a Reply

Your email address will not be published.