, ,

Azure Savings Scheduled Shutdown and Startup of Virtual Machines

Avatar de Vinicius

Today’s post will cover Scheduled Shutdown and Startup of Virtual Machines and is part of the Azure Savings series of that cover ways to save money on Azure.

Once I heard, and used ever since this analogy, that using cloud resources is similar to use electricity: the more you use, the more expensive it will be. Although, you can also save money if you apply to the cloud what you currently do for electricity

Turn off things that are not in use!

How Virtual Machines are billed?

First of, Virtual Machines are compute resources, and these are billed per hour, however, it does not mean that if you run a VM for 59 minutes you won’t be charged because you didn’t reach one hour.

In fact, Azure charges you by the number of full minutes, so to have an estimative the prices for VMs are presented per hour, or entire 60 minutes if you will.

But what does it mean by full minutes? Well, if you run your VM for 1 hour, 30 minutes and 45 seconds, the total of full minutes are 90 minutes, so Azure will do the maths and charge you accordingly.

Some math for better understanding

Let’s get the following VM size: D2 v3 (2 vCPUs, 8GB RAM) that is being presented as US$0.19/hour

By dividing 0.19 / 60 it give us the cost per minute which is 0.00316, since we have ran for 90 minutes then let’s calculate 0.19 + (0.00316 * 30 minutes)

US$0.28 for 90 minutes

If you access the Azure Calculator, you can double check the price match when you change the field in front of Hours to 1.5 that is the same as 90 minutes

azure pricing calculator d2 v3 virtual machine 90 minutes price blog vinicius deschamps

How a VM price is composed?

In a cloud environment, you don’t need to think about electricity, air conditioning, a physical room to maintain the servers but that does not mean you don’t have to pay for them, because these things are embedded into VM’s price, as well as Disks, Licensing and Storage Transactions.

By going to the Azure Calculator, you can have a basic idea of how much would cost a VM

azure pricing calculator d2 v3 virtual machine 1 month basic detailed price blog vinicius deschamps

Each option could affect the final price, either by simply changing the region or by adding an additional disk. Also, this is an estimative of the price for 1 month of usage of a D2 v3 VM in Azure, US$137.44

Why should I schedule a stop / start of a VM?

To save money! Imagine a scenario where you have a VM which is used during business hours by a specific person, why would you leave the resource up & running 24×7 if isn’t required?

But prior to show how to schedule a stop / start I’d like to highlight a couple things

Stopped vs Deallocated

A VM can either be stopped or stopped (deallocated), and the difference is being charged by the compute resource or not.

In order to avoid billing for your compute resources the VM must be in a Deallocated state

Avoiding surprises

Despite the fact you are not using compute resources by deallocating the VM, there are things associated to it that you continue to billed, like the Operational System disk, for example.

Here is the cost from my Deallocated VM in a single day, about US$0.88/day (~US$26.4/month)

azure cost management cost analysis storage price 1 day blog vinicius deschamps

Dynamic Public IP vs Static Public IP

By default an Azure Virtual Machine is configured with a Dynamic Public IP, and it remains tied to the VM until it is restarted, stoppped or stopped (deallocate).

Also, a Static Public IP incurr costs when a VM is deallocated.

How to schedule shutdown / startup of a VM?

Schedule Shutdown

Go to your Virtual Machine, and under Operations look for Auto-shutdown, and click on it

azure virtual machine operations auto shutdown blog vinicius deschamps

Change the Enabled from Off to On, to be able to modify the options: scheduled shutdown, time zone and send notifications before auto-shutdown?

azure virtual machine operations auto shutdown enabled on blog vinicius deschamps

You have the ability to send a notification before auto-shutdown, by clicking on Yes and fill with one of the possibilities Webhook URL or Email address

azure virtual machine operations auto shutdown send notifications before auto shutdown yes blog vinicius deschamps

Once you choose how the Auto-shutdown fits better for your activity, hit Save

azure virtual machine operations auto shutdown configured save blog vinicius deschamps

Schedule Startup

In order to schedule the start action, you need to have an Automation Account and play with Runbooks to make it happen

At the Azure Portal, use the Search field (1) at the top of the portal and type automation, then click in Automation Accounts

azure portal search automation automation accounts blog vinicius deschamps

In Automation Accounts, press Add

azure automation accounts add blog vinicius deschamps

Fill the form at Add Automation Account providing Name (1), Subscription (2), Resource Group (3) and Location (4), then press Ok (5)

azure add automation account blog vinicius deschamps

Once we have the Automation Account created, let’s click on it to continue the configuration

azure automation accounts click on existent one blog vinicius deschamps

Then under Process Automation, look for Runbooks (1) and Browse gallery (2)

azure automation accounts runbooks browse gallery blog vinicius deschamps

You can search for runbooks that would address your needs, but at this time we want to Start a VM, and it is the first option, let’s click on it Start Azure V2 VMs

azure automation accounts runbooks browse gallery start azure v2 vms blog vinicius deschamps

Then click Import

azure automation accounts runbooks browse gallery start azure v2 vms import blog vinicius deschamps

Fill the Name (1) and press OK (2)

azure automation accounts runbooks browse gallery start azure v2 vms import choose name click ok blog vinicius deschamps

Back to the Runbooks, you should now see a new Runbook named StartAzureV2VM, click on it

azure automation accounts runbooks startazurev2vm new runbook blog vinicius deschamps

Click Edit

azure automation accounts runbooks startazurev2vm new runbook edit blog vinicius deschamps

And then Publish (1), and click Yes (2)

azure automation accounts runbooks startazurev2vm new runbook edit graphical runbook publish blog vinicius deschamps

Now, let’s create a schedule by clicking Link to schedule

azure automation accounts runbooks link to schedule blog vinicius deschamps

Click Link a schedule to your runbook

azure automation accounts runbooks link to schedule schedule runbook blog vinicius deschamps

And Create a new schedule

azure automation accounts runbooks link to schedule schedule runbook create a new schedule blog vinicius deschamps

Fill the information Name (1), Starts (2), Time zone (3), Recurrence (4), Recur every (5) and press Create (6)

azure automation accounts runbooks link to schedule schedule runbook new schedule configured blog vinicius deschamps

Now we have the schedule set, let’s Configure parameters and run settings

azure automation accounts runbooks link to schedule schedule runbook parameters and run settings blog vinicius deschamps

Provide the following information accordingly to the Virtual Machine you want to start, and provide ResourceGroupName (1) and VMNAME (2), then press OK (3)

azure automation accounts runbooks link to schedule schedule runbook parameters and run settings parameters blog vinicius deschamps

All set, click OK to create the schedule

azure automation accounts runbooks link to schedule schedule runbook configured blog vinicius deschamps

At the Runbook, under Resources find Schedules and click on it, you will see there’s a schedule and showing when will be the Next Run

azure automation accounts runbooks schedules next run time blog vinicius deschamps

And that’s it!

I hope you liked it, and I’ll see you on my next post.

Photo by Sharon McCutcheon on Unsplash

Avatar de Vinicius

4 respostas para “Azure Savings Scheduled Shutdown and Startup of Virtual Machines”

Deixe um comentário

O seu endereço de e-mail não será publicado. Campos obrigatórios são marcados com *