bright ideas

Getting started with Azure Automation

Azure Automation

Azure Automation is one of the powerful features that Microsoft provides, that allows users to either write fresh scripts or leverage existing PowerShell scripts, and execute them through the Azure Portal.

My intention with this blog is to give you an idea of what Azure Automation is capable of, and start thinking how you could leverage this product within your own Azure infrastructure. As I prefer to explain technology via a demonstration, I thought I'd run through some examples with a few screenshots. If you'd like to have a read of the official Microsoft documentation beforehand, read here.

Runbook Demo

In this demo, I'll demonstrate how to create a Runbook and write a simple script that connects to Azure and gets subscriptions and resources.

First of all, navigate to the Azure Automation page and create an account. I called this one "TestAutomationAccount".

Azure Automation 1

Once created, click Runbooks and then Add a runbook.

Azure Automation 2

Fill in the name, select the Runbook type as 'PowerShell Workflow' and click Create. You can add a description, but that's optional.

Azure Automation 3

Once created, you will then be redirected to the Edit page, which looks very similar to typical program editors.

Azure Automation 4

On the left-hand side, there are CMDLETS/RUNBOOKS/ASSETS panels that provide the list of available commandlets that Azure Automation provides. You can expand them to pull out further details.

Azure Automation 5

To connect to Azure with PowerShell, a credential PSObject is required. The object could be created within the script by using a plain text password, but security-wise, it's best to encrypt. Hence, I will be creating a credential asset to store an encrypted password. From the Settings menu, click on Assets.

Azure Automation

Then click Credentials, Add a credential, and fill in the name of the Asset, Username, and Password (make sure this user has admin access to at least one subscription). In this case I called it "AzureAccount".

Azure Automation 7

Once the asset credential is created, go back to the Runbook Edit window. Then, expand Assets, Credentials, and you will see the "AzureAccount" credential created. If you aren't sure how to use the asset, simply right-click and add to canvas. It will automatically populate a line for you.

Azure Automation 8

Using the credential above, finish off the script. An example script is shown below.

Azure Automation 9

Save the Runbook and navigate to the Test Pane. This is where you can test out your script just like local PowerShell. Click Start and you will see that it connects to Azure and prints out available subscriptions and ARM resources.

Azure Automation 10

From here on, you can start building your own Runbook.

Why would you do Azure Automation?

Cost reduction

Using Azure Automation provides a number of ways to reduce cost. First of all, you'll be reducing human cost. For instance, there will be certain tasks that Ops teams need to perform for the Dev team. With Azure Automation, you can simply give access to the Dev team to execute Runbooks themselves; freeing up Ops to concentrate on other tasks.

Another example is that infrastructure costs can be reduced. For example, if you live in New Zealand and workloads are globally dispersed, you will definitely need at least one medium-to-high spec virtual machine in each region in order to manage workloads, such as upgrading a database with DACPAC. Instead of utilising virtual machines, you can convert them into Runbooks. The cost of running a Runbook is $0.003/min in NZD. This is definitely much cheaper than running virtual machines.

Code management

Azure Automation provides Github integration that provides access control and collaboration features. Repositories can be maintained by both the Dev and the ops teams to develop codes efficiently.

Unfortunately, Visual Studio Online (TFS) is not supported at the time of writing this blog.

Azure Automation 11

Webhook

Not only can a Runbook be scheduled, but it can also be triggered by an Azure Alert. One of the best outcomes we're delivering for our customers is to run a set of SQL performance queries when a high DTU utilisation is triggered. The Runbook collects the necessary data and sends the report to the Ops/Dev team for investigation. With this approach, troubleshooting has become much easier as real-time data can be gathered.

More details around Webhook can be found here.

Give it a go

There are a lot of other Azure Automation features available, such as Desired State Configuration (DSC), developing your own Modules, hybrid setup, and so on. Certainly, this is the best way to optimise and automate your current Azure infrastructure.

If you're interested, reach out to us to learn more about other customer use cases.

Steven Kang

Author: Steven Kang

Steven is a DevOps Engineer responsible for managing and designing clients' cloud infrastructure, to drive efficiency and consistency.

28 September 2016 / 0 Comments