How I saved thousands of dollars on my Azure environments!

I just love such headlines, because it instantly attracts attention.

But in this case it is actually true. And Microsoft is even wants us to do this. I want to write how to automatically shut down and start up environments in Azure, so that you are not spending more than needed. This post is for newbies, and experts surely will bombard the comment section with improved suggestions on how to make it even better.

In this example I have 4 environment running in Azure and the machine type I prefer is the D13_v2. This will cost me 3696 USD per month if I just let them stay on for 744 hours per month.

But I only plan to use them 07:00 à 17:00 Monday to Friday. This is 200 hours per month, and then it will just cost 993 USD J A lot of fun can be done with these extra credits.

So what is the next step? The trick is to use the Azure Powershell Runbook. Here is the step-by-step instruction on how to set it up:

1. Log into Azure, and open the Azure automation

2. Add an Automation Account.
    Create a name, like “TurnOffOnVM”.
    Select the subscription, and if a resource group should be created. Also if you want an Azure Run As Account. (I didn’t bother to have that, since I have no important stuff on these environments)

3. Then create an Asset named “automation” for holding credentials, that will run the shutdown/start up scripts. The credentials you are using must have the rights to run scripts and to start/stop VM’s.

4. Let’s create 2 Runbooks, that holds the scripts and schedules for the start and stop scripts.

5. Use the “Powershell Workflow” type

 

6. Let’s put in the “Start script”. It’s done here

 

I have removed my VM-names in this example.

If you wonder what your VM name is, it is the computer name, that can be seen here:

Here is a copy-paste version of the Start-up script:

workflow StartVM

{

    $cred = get-automationpscredential -name “automation”

    add-azureaccount -credential $cred

    select-azuresubscription -subscriptionname “Microsoft Azure Enterprise”

 

    $VMs = Get-AzureVM

 

foreach($VM in $VMs)

{

if ($VM.Name -In “VMName1”, “VMName2”, “VMName3”, “VMName4” )

{

if ($VM.PowerState -ne “Started”)

        {

     Start-AzureVM -Name $VM.Name -ServiceName $VM.ServiceName -ErrorAction Continue

        }

}

}

}

7. Let’s put in the “Stop script”. It is basically the same procedure as creating the “start script”, so I just add the copy-past version of the script.

workflow StopVM

{

    $cred = get-automationpscredential -name “automation”

    add-azureaccount -credential $cred

    select-azuresubscription -subscriptionname “Microsoft Azure Enterprise”

 

    $VMs = Get-AzureVM

 

    foreach($VM in $VMs)

    {

    if ($VM.Name -In “VMName1”, “VMName2”, “VMName3”, “VMName4” )

    {

        if($vm.Status -eq ‘ReadyRole’)

        {

        Stop-AzureVm -Name $vm.Name -ServiceName $vm.ServiceName -Force

        }

 

    }

    }

}

 

Remember to press the “publish” button the scripts J

8. Let’s create a schedule (one for the Start runbook, and one for the stop runbook)


9. You can now monitor the start/stop scripts:

 

10. Go party with all the credits you have saved! And if you see me, and use this script, buy me a beer J

 

Happy DAX’ing J

4 thoughts on “How I saved thousands of dollars on my Azure environments!

  1. Hi Kurt, Thank you so much. Do you find an issue with the LCS disconnect between shutting down the environment via Azure and stopping it from LCS? I have found that when I stop the environment directly from Azure LCS does not recognize that it is stopped nor visa/versa. Unless this has recently been resolved by Microsoft. Interested to hear your comments. Thanks… Diane

    Like

    • Hi Diane. We just use the Azure portal to see if if the environments are live or not. But the scripts presented here do not work for ARM deployments, and I have created new scripts that work for that. …. Maybe I should create an update of the blog ??

      Take care, and happy DAX’ing

      Like

  2. Hi Kurt,

    Great post and thank you.. We are thinking about doing that for our department, but have experienced a drop in performance, probably due to the cache being wiped out. Do you have any feedback/ideas/comments on that?

    Best Regards,

    Jens Kristiansen

    Like

    • Thanks Jens. I have the same experience, and if some knows how to quickly warm up the VM, that would be great. One thing I do to improve performance(Right before a presales demo) is just to do a complete compile and sync the VM 🙂

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.