One of the great things in Cloud computing is the ability to use resources whenever you want them and pay only only for what you are using. In this post, we are going to explore how you may create a schedule to automatically shut down and start up a virtual machine running on Azure, using Azure Automation, based on a user defined schedule.

First of all, let’s start with some basics.

What is Azure Automation?

Microsoft Azure Automation provides a way for users to automate the manual, long-running, error-prone and frequently repeated tasks that are commonly performed in a cloud and enterprise environment. It saves time and increases the reliability of regular administrative tasks and even schedules them to be automatically performed at regular intervals. The processes you want to automate can be done through runbooks or automating configuration management using Desired State Configuration.

In this post we will use the Runbooks option.

What is a Runbook?

Simply put, a runbook is a set of tasks that perform some automated processes in Azure Automation. It can be a very simple process or a quite complex one by combining other runbooks to perform processes across multiple resources or even multiple clouds and on-premises environments.

For more details you can head to the official Azure Automation documentation.

Let’s start then.

Step 1. Create an Automation Account

Firstly, add an Azure Automation account in your Subscription and to do so:

  • click on the + sign to “Create a new Service” and then from Monitoring + Management group select Automation,
  • fill the details in the Add Automation Account blade (name, subscription, resource group and location), and
  • in Create Azure Run As account option select Yes, in order to create a service principal account for the application in Azure AD, and assign the Contributor role for the account to your current subscription.

After a few minutes, a new Azure Automation account will be created and alongside you will see some sample Runbooks already created to help you get started.

Step 2. Create a new runbook

To create a new runbook, head over to your Automation Account and follow the next steps:

  • click on Runbooks option from the left sidebar,
  • from the list of your available runbooks click on the + sign at the toolbar,
  • select Quick Create and in the new blade, fill in the necessary details (name, runbook type and description). In the Runbook Type option select Powershell which allows you to run scripts based on Windows Powershell, and
  • click on the Create button.

After a few seconds, your new runbook will be created and it will be listed alongside your other runbooks, marked as New. Keep in mind that, in order to use it, you need to publish it first.

Step 3. Add functionality to Startup and Shutdown a Virtual Machine

Select your runbook and from the top bar click on the Edit icon.

A new editor inside the Azure portal appears which allows you to edit and test your Powershell script.

The code for the Powershell script to startup or shutdown a virtual machine is the following:

# Input Parameters for
# - VmName: name of the vm to perform action to
# - ResourceGroupName: resource group where the vm belongs to
# - VmAction:action to perform (startup or shutdown)
Param(
 [string]$VmName,
 [string]$ResourceGroupName,
 [ValidateSet("Startup", "Shutdown")]
 [string]$VmAction
)

# Authenticate with your Automation Account
$Conn = Get-AutomationConnection -Name AzureRunAsConnection
Add-AzureRMAccount -ServicePrincipal -Tenant $Conn.TenantID `
-ApplicationID $Conn.ApplicationID -CertificateThumbprint $Conn.CertificateThumbprint

# Startup VM
IF ($VmAction -eq "Startup") {
    Start-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName
}

# Shutdown VM
IF ($VmAction -eq "Shutdown") {
    Stop-AzureRmVM -Name $VmName -ResourceGroupName $ResourceGroupName -Force
}

Note the parameters at the top of the script that are used to set:

  • the Name of the virtual machine,
  • the Resource Group Name that the VM belongs to, and
  • the action to perform on the VM either to start it up or shut it down.

Step 4. Test your runbook

While you develop your script, you always have the option to test your code and see how it works, by clicking on the Test Pane button found at the top bar.

When you click on it, a new blade appears where:

  • in the left section you can specify the parameters needed in your script,
  • in the center area you can see the status of your runbook (Queued, Running, Completed, etc.).

Depending on the runbook’s result you may see the actual response or an error that has occurred during its execution

In the Test Pane you can run your script with different parameters, press the Start button and watch how your it behaves on different scenarios, all from within the Azure Portal.

Step 5. Publish your runbook

Now that you have written and tested your runbook, it is time to publish it.  To do so you need to click on the globe icon found at the top bar of your runbook:

After publishing your runbook, more options are unlocked, like View, Export, Schedule or add a WebHook to call your runbook using an http call.

Step 6. Create a schedule to automatically startup or shutdown your Virtual Machine

Following the above, you may create a schedule to automatically startup a virtual machine in the morning and shut it down in the evening. To do so, click on the timer icon at the top bar.

Firstly, create a schedule:

and then set the input parameters for this schedule:

After you have finished with the schedules, all of them can be found by clicking on the Schedules option at the left sidebar:

 

Categorized in:

Tagged in:

, ,