Invoke Azure Runbooks Using Webhook From Client Applications

It has been a long journey and my article view count has reached the milestone of one million. Thanks to the entire C# Corner Team and the readers for the continued support.

In this article, we will see how to invoke Azure runbook using Webhook from Client Applications.

Azure Automation

Azure Automation enables users to automate tasks that are manual and repetitive in nature by using Runbooks. Runbooks are nothing but a set of tasks that perform some automated implementation in Azure Automation.

Runbooks in Azure Automation are based on Windows PowerShell or Windows PowerShell Workflow. We can code and implement the logic that we want to automate using PowerShell. You can see how to create runbooks and the practical scenarios from here

Webhook

A webhook allows us to start a runbook that we have created in Azure Automation using a single HTTP request. We can use the webhook from a variety of external services like GitHub, Visual Studio Team Services, and custom applications from which we can start Azure runbooks that helps us run some predefined logic. An overall webhook flow is shown in the below image. In this article we will see how to invoke the azure runbook using webhooks from various client applications.

Azure
Image Source : docs.Microsoft.com

Create a Webhook

We have already covered how to create the runbook in Azure and saw how to retrieve JSON Data from REST Endpoint and mail it to business users. Now we need a method to invoke the runbook remotely from our client application. We will be using Webhook for this purpose.

We can create a webhook from the Runbook page of the Azure Automation Account. Thus we can say that the webhook is hooked to a runbook upon its creation. Select the ‘Webhook’ option as shown below,

Azure

This will open up the page where we have to specify the below parameters that will be used to create the webhook:

  • Name: Specify a name for the webhook
  • Enabled: By default, it is enabled. If we disable it, we cannot access it from client applications. We can enable/disable it even after webhook creation.
  • Expires: Every webhook comes with an expiration date beyond which it cannot be used. We can change once the webhook is created.
  • URL : This is an important property of the webhoook as we will be using this URL to issue the POST HTTP request. The URL cannot be changed and contains a security token that enables the HTTP request to issue the call without any further authentication.

Note

We should note down the URL securely as it serves the purpose of a password. Any user having this URL can issue an HTTP POST request and run the runbook without any further authentication due to the presence of the security token in the URL. Moreover, we can see this URL only during the webhook creation time.

Azure

Click on OK to save the webhook parameters. The Webhook URL.

Click on Create to provision the runbook webhook.

Azure

Going to the runbook page, we can see that the webhook has been created.

Azure

Invoke the Runbook using Webhook from Client Applications

We can invoke a runbook using the webbook by issuing an HTTP Post request of the Webhook URL that we received while doing webhook creation. We can invoke the URL from a variety of client applications. We will see few examples in this section.

Invoke Webhook from PowerShell

We can invoke the Azure Runbook Webhook from the local machine using PowerShell. Ensure that we are using PowerShell 3.0 or above as ‘Invoke-RestMethod’ command is available only in the higher versions.

Azure

  1. $request = 'https://s3events.azure-automation.net/webhooks?token=GpU3Gd4E86eBzboUC5ptXl%2f9rXVP%2fuTdC16wJ1M4KsU%3d'  
  2. $result = Invoke-RestMethod -Method Post -Uri $request  

 

On running the above script from PowerShell we have invoked the webhook which triggered the runbook that get JSON Data from a REST endpoint and have mailed it to business user. The email screenshot is shown below,

Azure

Invoke runbook from C#

Similarly we can invoke the runbook from a Windows Application using C# Code. We will be making use of the HttpWebRequest object to issue the POST request. The url used will be the webhook URL we received when we created it. The code block to issue an http request to invoke the web request is shown below.

  1. try  
  2.                 {  
  3.                     string uri = "https://s3events.azure-automation.net/webhooks?token=GpU3Gd4E86eBzboUC5ptXl%2f9rXVP%2fuTdC16wJ1M4KsU%3d";  
  4.                       
  5.                     HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(uri);  
  6.                     string data = string.Empty;  
  7.                     request.Method = "POST";  
  8.                     request.ContentType = "text/plain;charset=utf-8";  
  9.       
  10.                     System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();  
  11.                     byte[] bytes = encoding.GetBytes(data);  
  12.       
  13.                     request.ContentLength = bytes.Length;  
  14.       
  15.                     using (Stream requestStream = request.GetRequestStream())  
  16.                     {  
  17.                         requestStream.Write(bytes, 0, bytes.Length);  
  18.                     }  
  19.                      
  20.                     request.BeginGetResponse((x) =>  
  21.                     {  
  22.                         using (HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(x))  
  23.                         {  
  24.                             using (Stream stream = response.GetResponseStream())  
  25.                             {  
  26.                                 StreamReader reader = new StreamReader(stream, Encoding.UTF8);  
  27.                                 String responseString = reader.ReadToEnd();  
  28.                                 MessageBox.Show("Webhook Triggered at "+System.DateTime.Now+" \n Job Details : "+ responseString);  
  29.                             }  
  30.                         }  
  31.                     }, null);  
  32.                 }  
  33.                 catch (Exception ex)  
  34.                 {  
  35.                     throw ex;  
  36.                 }  

 

Upon running the code we have received the data returned from the POST Success method which shows the Job ID as well as the run time of the application.

Azure

Heading over to the Azure portal, we can see that the runbook has been triggered at the same time, as shown in the application message box.

Azure

Now let's go ahead and see the Job that is responsible for running the runbook. We can see the Job section by selecting the runbook as shown below,

Azure

Click on Jobs to see the list of completed and running jobs. As we can see the first job that is shown in the list has the same ID as shown in the Client Application message box. Its status has gone to ‘Completed’ indicating a successful run of the runbook.

Azure

The runbook has retrieved the results from the REST endpoint and has sent a mail to the specified user along with the data as shown below.

Azure

Invoke runbook from Jquery

We can also invoke the runbook by issuing a POST http request from the client side code using Jquery. This is fairly simple as we just have to use the $.post shorthand to issue the Ajax post requests using the Webhook url. We have used JSFiddle to test it out.

Azure

  1. $.post( "https://s3events.azure-automation.net/webhooks?token=GpU3Gd4E86eBzboUC5ptXl%2f9rXVP%2fuTdC16wJ1M4KsU%3d"function( data ) {  
  2.    alert('Webhook Trigerred !')  
  3. });  

 

Upon running the above JSFiddle code, we can see that the job status has become ‘Running’ in the Azure Portal .

Azure

It will soon go to ‘Completed’ Status and we will receive the email which contains the data retrieved from the REST endpoint . We can modify the code in the runbook to implement any other requirement logic. Updated code will be reflected when the runbook is ‘Published’

Azure

Summary

Thus, we saw how to automate business processes by creating an Azure Runbook and invoking the runbook using Webhooks from client applications.


Similar Articles