Use Azure Function to Start Azure Automation Runbook

Of late, I was working on a customer requirement, which is to start a runbook. Yes, many would have done it through logic apps, function app, or webhooks. This blog post is to walk you through the steps to create an Azure Function to start an Azure Automation runbook .

Our goal is to wait until the job completes and fetch the output.  If you are familiar with C#, the ASYNC programming pattern helps you to achieve it quickly. In my case, it’s an IT Pro way. Yes, I am not good with ASYNC programming. So, I managed to convert my PowerShell script to C#, which solved the issue.

Why not an HTTP Trigger?

If the Azure automation account runbook job takes more than 230 seconds, a timeout occurs. The 230 seconds of timeout settings can’t be tuned, and it’s a hard set value. Azure Functions Start Azure Automation Runbook

Azure Functions Start Azure Automation Runbook

Overview

What do we need?

  1. Azure Account.
  2. Automation Account.
  3. Runbook.
  4. SPN with Certificate.
  5. Azure Function App.
  6. VS code / Visual Studio (IDE is your choice)

Runbook

The runbook we need to start has three parameters as
illustrated below

  1. FirstName (String)
  2. SurName (String)
  3. Skills (Array of String)

For our demo purpose, the runbook should collect parameters and prints the data as JSON objects. Below, code does it.

Get Started

If you are familiar with PowerShell, the below snippet is
easy to understand.

Azure Functions Start Azure Automation Runbook

The code above is to start an Azure automation account
runbook and retrieves the output.

Recode in native PowerShell

Do you think it’s reinventing the wheel? No, it is not. It’s
an alternative approach to avoid Az modules.

Using Azure .NET SDK in Azure Functions (Durable)

A simple “DO WHILE” loop is here to wait until the job gets
completed and retrieves it.

HTTP Start Trigger (Starting Point)

Azure Function Start Azure Automation Runbook

Orchestrator Function (A  Gist)

Azure Functions Start Azure Automation Runbook

Get Job Output

Azure Functions Start Azure Automation Runbook

Client Application

The below snippet is to invoke the Azure function app.

Azure Functions Start Azure Automation Runbook

Summary

We have covered the steps to use an Azure Function to start an Azure automation runbook, wait till the job completes and fetch the output. Indeed, there are multiple ways and approaches to do the same. If you have found one, please feel free to share your feedback. In my next blog post, we show the use case of a timer trigger with a real-world example.

Download Azure Migration Advantages guide

About the Author Chendra Venkatesan