Cost optimization in Azure
Tags: azure
Published: 2019-09-11 19:00:57

---
title: Cost optimization in Azure
published: true
tags: Azure
canonical_url: https://peterrombouts.nl/2019/02/15/cost-optimization-in-azure/
---

In my [previous post](https://dev.to/prombouts/managing-virtualmachines-in-azure-with-c-17pd-temp-slug-4334241) I showed how easy it is to start or deallocate machines using C# and Azure Functions.

Azure VirtualMachines can be handy but very costly if you forget to turn them off∗ and leaving them running for 24/7.

But, what if you have dozens of existing VMs and are looking for a quick way to schedule the on/off times for those machines to save money?

In this post I will show a very simple script to use with [Azure Automation](https://azure.microsoft.com/nl-nl/services/automation/) that enables you to get this up and running, literally within the hour.

> Note: I would advise to use [Azure DevTest labs](https://azure.microsoft.com/en-us/services/devtest-lab/) if you have development virtual machines. That will give you the opportunity to reuse, claim or share VMs within a group of users.
> 
> \* In this blogpost I will use the word **Stop** in code and comments, but in fact that will **Deallocate** the machines, and will stop incurring costs.

 

## Calculating costs

First of all, lets take a quick look at how much you can actually save with the instructions in this blogpost. If we take a D4s v3 machine and let it run for 24/7, it will cost approximately **260 euros a month (in West Europe)**. If you have a team of 8 developers, that adds up to more than **2000 euros a month**.

For sake of this blogpost, let us state the actual working hours are (monday to friday) from 7 AM until 7 PM. This brings the hours down to 12/5. The running costs of one machine will be lowered to approximately **92 euros a month**. For a team of **8** developers, this will save over **1300 euros a month** , each month, with the use of a simple script.

## Azure Automation

I will not explain how to setup Azure Automation, as that is pretty simple to setup using the Azure Portal. I’ll jump right into importing and creating runbooks. We start in the Azure Automation blade in the portal:

![1AzureAutomation](https://peterrombouts.files.wordpress.com/2019/02/1azureautomation-1.png?w=840)

Click on ‘Runbooks’ in the left pane, and you’ll see some predefined runbooks added by the system.

> Please [download my PowerShell script for importing here](https://gist.github.com/prombouts/43f14670739bab51834bd3753c8c89f7)

After downloading my script, you can click on ‘Import runbook’ in the menu bar of the blade.

After importing, the runbook will have the status ‘New’:

![2OverviewRunbooks](https://peterrombouts.files.wordpress.com/2019/02/2overviewrunbooks.png?w=840)

To start using the runbook, click on the ‘Stop-Start-AzureVM-By-Tag’ runbook. In the next screen, click on ‘Edit’:

![3Runbook](https://peterrombouts.files.wordpress.com/2019/02/3runbook.png?w=840)

This will open up the actual runbook script, and show all the PowerShell code. In here you can adjust the code to your liking if you want to add logic or change parameters:

![4EditPublish](https://peterrombouts.files.wordpress.com/2019/02/4editpublish.png?w=840)

Now, click on ‘Publish’ and you can run your first runbook!

## Tagging your VMs

Obviously you do not want to manually click to start or stop the machines. In my simple code example, I’ve filtered on a specific tag name and tag value. Tags can be set programmatically or manually. For playing around with this script, simply go to a VM, and make sure the tags are set up like this:

![5TagExample](https://peterrombouts.files.wordpress.com/2019/02/5tagexample.png?w=840)

The tagname is ‘ **ScheduledOnOff** ‘ and the value is ‘ **Yes** ‘.

## Scheduling your runbook

At this moment the runbook is not automated. There is no schedule attached, but you could manually trigger the runbook to start or stop your machines.

To create a schedule, go back to the runbook, and select ‘Schedules’:

![6Scheduling](https://peterrombouts.files.wordpress.com/2019/02/6scheduling.png?w=840)

Creating a new schedule is easy; I’ve created one schedule to start at 07.00 at weekdays:

![9StartWeekdays](https://peterrombouts.files.wordpress.com/2019/02/9startweekdays.png?w=840)

The final step is to setup the parameters. In my predefined script, using the default parameters, we only have to set ‘Start’ in the action field:

![8Parameterize](https://peterrombouts.files.wordpress.com/2019/02/8parameterize.png?w=840)

> The same setup should be followed to deallocate the VMs, creating a schedule with the ‘Stop’ action at the preferred time of day.

## Conclusion

This blog explains how to save resources and reduce costs in a very quick and effective way. This can be seen as a patch or temporary solution, because if some user deletes or changes the tags, the script will not work for that specific VM anymore.

Nonetheless it is a very handy, quickly implemented solution to fix your long-running-always-on dev or test machines!