Thursday, March 16, 2017

How to Automate Start/Run and Stop/Deallocate of Azure VM or other resources

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. One of most useful of this automation process is deallocated and bring them running state repeatedly as per the schedule. This brings down the cost of resources.
One of the latest features in Azure Management Portal, you can now automate stop/deallocate as shown below, however you can’t start/run.


This post covers an example how to automate Start/Run and Stop/Deallocate of Azure VM.
Step1: Create an Automation account
An Automation account is an Azure resource that you create. You can manage all your Azure, cloud, and on-premises resources with a single Automation account. An Automation account is a container for the items you need to make your automation work: these are Runbooks, modules, assets like credentials and schedules, and configurations.
Along with automation account, some sample Runbooks will be created.
Click on ASSETS and make sure there are 2 connections were created.
Step2:  Create or Import Runbook
Runbooks in Azure Automation are based on Windows PowerShell or Windows PowerShell Workflow, so they do anything that PowerShell can do. In this example, I am using the existing runbook which I got from this link.
Once you extract the source, there will be 4 Runbooks.
You need to import all 4 Runbooks as shown below
Setp3: Configure the Runbook script
Change the below 2 parameters for Start and Stop script and then publish.
$AutomationRG (Automation Resource Group)
$AutomationAcct (Automation Account)
Without do any changes publish the StartVM and StopVM Runbooks, because of Start and Stop Runbook will call these 2 and pass the parameters.
Step4: Create a Tag for Azure VM that you need to automate.
his runbook is written to identify the resources suing on Tags. You apply tags to your Azure resources to logically organize them by categories. Each tag consists of a key and a value. For example, you can apply the key "Environment" and the value "Production" to all the resources in production. Without this tag, you may have difficulty identifying whether a resource is intended for development, test, or production. However, "Environment" and "Production" are just examples. You define the keys and values that make the most sense for organizing your subscription.

Step4: Start the Start Runbook

This will show the parameters request with default values. As this runbook was already configured, you just let it run.


You can monitor the progress via Output. Based on this script Start runbook will trigger to run the StartVM.
You can notice that the Azure VM will start and running.
Step6: Start Stop Runbook
You will notice that Azure VM will stop and deallocated.


Step 5: Schedule the Runbooks

Cheers!
Uma

1 comment: