Get Started with Azure Automation

Microsoft Azure has a neat way to store and run code right from within Microsoft Azure called “Azure Automation”. If you’re familiar with Amazon’s Lambda service, Azure Automation is similar in many ways. The main difference is that in Azure, we’re working with PowerShell code instead of Python or Node.js.

Create An Azure Automation Account

To get started, the first thing that we need to do is to setup an Azure Automation Account. In your Azure instance, browse for “Automation Accounts” and then click Add. Give the account a name and a subscription that the PowerShell commands should run under. As with any Azure objects, select a resource group or create your own and then select a location. The last setting is to decide whether or not the account with be an “Azure Run As” account. If you select “Yes” then the account will have access to other Azure Resources within your instance. For our examples, this account should be a “run as” account.

AzureAutomationAccount

Building a Runbook

Now that the account has been created, we can open the account properties and go runbooks. A runbook is the script that will be executed through the Azure portal. Click Create a new runbook (you can import one if you already have existing runbooks as well) and give the runbook a name. Then under the “Runbook type” field, you’ll need to select the type of runbook. There are two main types of runbooks, PowerShell or Graphical. Powershell workflows allow you to type PowerShell code directly into the Azure window, while the Graphical type will show you cmdlets and they can be added by clicking on them. For our purposes we’ll be typing the PowerShell code in directly, so we won’t use the Graphical type. Add a description and click the “Create” button.

AzureAutoRunbook2

Now we can edit our runbook and put our PowerShell code into the work pane. I’ve added some code here to authenticate with Microsoft Azure and get a list of all virtual machines with “test” somewhere in their name. Then stop the virtual machine. Maybe you even have the code delete virtual machines with “test” in their name to cleanup your Azure subscriptions and stop paying for those resources. You can make the code look how you want it.

AzureAutoRunbook3

Once you’ve written your PowerShell code, you can click the “Test pane” link to take you to the test window. Here you can enter any parameters that might be involved in the script and execute it. The status will show up as queued in the test pane and then eventually will run. This usually isn’t immediately executed, but should execute fairly quickly after being submitted. The code will execute and show you any results in the pane.

AzureAutoRunbookTest

Once you’ve tested your code and are happy with it, you can “Publish” the code to make it available to your users. The Runbooks screen will show you the current status.

AzureAutoRunbook1

Create an Entry Point

Now that the code is published you can run it from the Azure portal, but you can also set it on a schedule, or create a webhook to call through a REST call. In our example a schedule might be a good way to execute the code and shutdown your test VMs on a specific routine to cut costs, but here I’m actually going to add a Webhook so that I can execute it through a REST call.

AzureAutoWebhook0

I click Add Webhook and create a new one. The webhook also gets a name and expiration date as well as being able to enable or disable it from the Internet. The last box will show the URL used to make a POST request. Be sure to copy that URL to use it later on. You can also set some of the configuration parameters ahead of time so that you don’t pass them through the REST call, its up to you.

AzureAutoRunbookWebhook1

Execute the Code

I used an online REST call maker and pasted in my Request URL. This triggers the PowerShell code we just created through the webhook.

AzureAutoRunbookWebhook2

Once I run it, a job will be returned, and we can view the job status on the runbook screen.

AzureAutojobs1

 

When I look in my virtual machines screen, I see that my virtual machine named “testvm” is now stopped thanks to our script running.

AzureTestVMrunbookexample1

Additional Options

Credentials

Under your Automation Account, in Assets you can also add credentials so that they can be used in future scripts. Perhaps you need credentials to login to some of your virtual machines to execute PowerShell scripts on them. The credentials screen lets you store these in the portal for reuse.

AzureAsset

Install PowerShell Modules

In some cases you may want to add additional PowerShell functionality to your Azure instance. For instance, you might want to install the Posh-SSH module so that you can execute ssh commands from PowerShell. You can do this in a couple of ways, the first is to go to your Azure Automation account and go to the assets option. Select Modules and then you can browse the gallery for the module you want to install in your Azure instance.

PoSHGallery2

 

The second way is to go right to the PowerShell Gallery and find the module. Microsof thas added a very nice “Deploy to Azure Automation” button to add the module to your Azure instance.PoSHGallery1zure

Get Started with Azure Automation

Leave a reply