Integrating Azure Functions With Azure Logic Apps

Overview

In a previous article, we have seen the details of Azure functions and saw how they expose a gateway to achieve almost everything in Azure whenever it comes to event driven programing.

In this article, we will be going through one such scenario which integrates two great offerings of Azure which are Azure functions and logic Apps.

This article assumes that you know basics of Azure functions and logic apps. If not, it’s highly advisable that you should learn details of both offerings by visiting links below,

Scenario

We will create a simple Azure function using our favorite language, C#, which will be responsible for writing entries in Azure storage.

As we have seen, Azure functions can be executed whenever any event occurs, meaning that we will need some kind of trigger to execute our function which we are about to create. Now either I create that trigger manually or if I can automatically generate it based on any particular event, t is where Logic app comes to the rescue.

We will be leveraging Azure logic app to generate that trigger for us. Azure logic app offers lots of inbuilt triggers e.g. Manual trigger, recurring, external event based trigger e.g. Item is added in SharePoint list etc.

I always enjoy working with Azure and have implemented a good amount of solutions using Azure for multiple customers.

Though Azure offers lots of health check offerings for solutions hosted on Azure which can effectively cater to customer’s service HA requirement, however, for developers or IT admins it becomes tricky to know if any particular Azure offering is facing some outage in  a particular region unless we visit Azure status website i.e. https://status.azure.com

No doubt that, it is a wonderful website providing all required information about current status of all Azure services and in all regions but one has to monitor it.

In this example, what we want to do is that we want to automate this process of monitoring Azure status so that we are immediately notified whenever any event occurs in Azure status web site and just for sake of integrating azure storage in this example, we will maintain a record of all the services which faced downtime inside an Azure table storage.

We will be using Logic app’s RSS Feed trigger event and point it to scan the RSS feed of status.azure.com website and generate an event based on feed changes.

Implementation

So let’s start by creating Azure Logic app – It is a quite straightforward process.

Since we will be working with a new Azure portal and all our resources will be created in ARM mode let’s create a Resource Group in Southeast Asia region before creating anything else. We will name our resource group as ‘IntegrationRG’ and logic app as ‘IntegrationApp’



Once the logic app is created, let’s navigate to it and click on Edit from top.

We will be presentedwith a Logic App designer with a number of templates to get started but we will choose blank and start customizing it from there.

Designer shows the list of Microsoft managed APIs, essentially different available triggers to choose from. We will choose the RSS trigger from the list.

As mentioned in the description of trigger API, it will trigger an event whenever a new item is published in any RSS feed.



Once the trigger action is added, let’s configure it and point it to periodically scan the status.azure.com RSS feed.



Click on RSS feed icon to get RSS feed and note it.



Enter the RSS feed in the Feed URL input of trigger and keep poll frequency as 10 minutes.

Once this is done, we want to invoke our Azure function and add it as next action here in this editor so that response from the feed can be sent to our Azure function which will store it in Azure table storage.

Save your changes in logic app editor by clicking save icon and exiting from it.

Now we need two things. First, an Azure storage and second the Azure function.

Let’s create both. To keep this article short, let’s not dive deep into the process of creating storage and azure function using Azure portal.

We will name storage account as ‘statusstorage’ and azure function app as ‘integrationapps’.

Once the function app is provisioned, we will create a new function and will name it as ‘StatusWriter’.

Note that, in order to make sure that this function would appear in Logic apps editor we will need to create it using Generic Webhook C# Template.



Once the function is created, let’s navigate to it and browse to integrate part where we will be able to set the input and output of this function.

As we want this function to write entries in the Azure table storage, let’s select the output as table storage and select correct storage account i.e. ‘statusstorage’



In the next step, let’s do the binding of parameters and select correct storage account in which storage table will be created if it does not exist.



If you do not see your storage account in the dropdown, then simply create a new connection to your storage account by clicking new button. All it does is, adds connection string entry in your function app’s application settings file.

Click on save and browse to the develop tab.

As this azure function will be interacting with table storage using Azure storage APIs so we would need to add the reference of Microsoft.WindowsAzure.Storage and also mention using statement as shown below



Next, let’s decide the schema of AzureStatus storage table. Process remains the same i.e. typically we define a class inheriting from TableEntity class so let’s add our AzureStatus class at bottom of editor



Once we have schema class ready, let’s modify bindings of the main ‘Run’ function.

  1. public static async Task<object> Run(HttpRequestMessage req, ICollector<AzureStatus> outputTable, TraceWriter log)  
  2. {  
  3.    return req.CreateResponse(HttpStatusCode.OK, "");  
  4. }  
Note that we have added outputTable binding pointing to AzureStatus storage table and cleaned up the Run method.

Now the next part is simple, it’s all about extracting the inputs sent in the Http request message object and adding them to the AzureStatus storage table in Azure.



On first line of Run function, we are simply writing log information.

On the next line, Http request message object content are read as string and this JSON string has been de-serialized and retrieved in a dynamic object ‘data’.

Note that, properties of the data object mentioned above e.g. Title, Link and Description will be sent from Logic App. We will see how to initialize those in next section.

These properties of data object holding values sent from Logic apps will be added to the Azure table storage as shown above.

After adding new entry in the table storage, we can send response back to the caller. For now, we will be simply sending Http response with status OK and response string as content shown above.

Save the source code and make sure that there are no compilation errors.

Let’s go back to the logic app editor and see if we can add this function as an action after RSS feed trigger.

Click ok Add next step, it will show all the available Microsoft managed actions by default. Click on that small dropdown arrow right beside managed APIs box and select Show Azure functions in same region option.



It will show all the functions available in the same region of Logic App.



Select ‘integrationapps’ function.



Select the status writer function.



Note that it asks for input payload object which is nothing but the JSON string and you will be able to see all the available values which we can pass as input to our function. These are nothing but the outputs from previous action i.e. RSS Feed trigger.

Let’s initialize inputs for our function app and form the JSON string



Note that Tile, Description and Link are the properties of the dynamic data object which we retrieved in the function app source code. Here are we are simply initializing those properties with available output of Feed trigger.

Save changes in logic app editor and we are done.

Now this logic app will be scanning the Azure status feed every 10 minutes and if any new item is published in the feed, it will be passing it to the Azure function for further processing i.e. recording to Azure table storage.

We could have even customized this flow to send an email alert using SMTP or outlook using logic app connectors or we could have also sent the SMS message using Twilio service as a next step after adding entries in the table storage, but just to keep example simple we have limited the scope.

But wait, how do we validate it?

I would have to wait for an outage in any azure service to show you the resulting entries in storage table.

So I will be updating this post with screenshot of the entries once the RSS feed of azure status is updated.
 
[Updated]
 
Here is the AzureStatus storage table which got updated based on Azure Status RSS Feed
 
 
 
 
On a concluding note, would like to add that Azure functions have enabled enormous opportunities and its integration with logic app makes it extremely powerful to drive event driven enterprise grade workflow applications.

 

X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now