Azure · Custos · Máquinas Virtuais

Azure Savings Scheduled Shutdown and Startup of Virtual Machines

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

I once heard, and have used ever since, the analogy that using cloud resources is like using electricity: the more you use, the more expensive it will be. However, 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 all, Virtual Machines are compute resources, and these are billed per hour; however, this does not mean that if you run a VM for 59 minutes you won’t be charged because you didn’t reach a full hour.

In fact, Azure charges you by the number of full minutes, so to estimate prices for VMs, they’re presented per hour, or as 60 minutes in total if you prefer.

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 is 90 minutes, so Azure will calculate the charge accordingly.

Some math for better understanding

Let’s take the following VM size: D2 v3 (2 vCPUs, 8GB RAM) which is advertised as US$0.19/hour

Dividing 0.19 by 60 gives us the cost per minute, which is 0.00316. Since we ran for 90 minutes, 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, which 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, or a physical room to maintain the servers, but that doesn’t mean you aren’t paying for them—these costs are embedded into the VM’s price, along with Disks, Licensing, and Storage Transactions.

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

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

Each option can affect the final price, whether by changing the region or by adding an additional disk. Also, this is an estimate 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 that is used during business hours by a specific person—why keep the resource up and running 24×7 if it isn’t required?

But before showing how to schedule a stop / start, I’d like to highlight a couple of things:

Stopped vs Deallocated

A VM can either be stopped or stopped (deallocated), and the difference is whether you are charged for the compute resource or not.

To avoid billing for your compute resources, the VM must be in a Deallocated state.

Avoiding surprises

Even though you are not using compute resources by deallocating the VM, there are related items that you continue to be billed for, such as the Operating System disk.

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, stopped, or deallocated.

Also, a Static Public IP incurs 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 option to send a notification before auto-shutdown by selecting Yes and filling in one of the choices 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

To schedule the start action, you need to have an Automation Account and work with Runbooks to make it happen.

In the Azure Portal, use the Search field (1) at the top of the portal and type automation, then click 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 in 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 according 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

In the Runbook, under Resources find Schedules and click on it; you will see a schedule and notice when the Next Run will be

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 in my next post.

Photo by Sharon McCutcheon on Unsplash

AzureCostCustosMáquinas VirtuaisVirtual Machines
Azure Savings Scheduled Shutdown and Startup of Virtual Machines — Vinicius Deschamps