serverless integration

Serverless Integration with Logic Apps, Functions and Cognitive Service API

Published on : Feb 9, 2017

Category : Microsoft Azure



Integration today is different than it was before with Logic Apps, Functions, API Management and the Service Bus. Integration service Logic App can be used to build a serverless integration solution. No more do you need to set up a server or procure one. An Azure Subscription and presto, you have access to every service Microsoft Azure bring. Kent Weare got Steef-Jan Wiggers to talk about this on Middleware Friday. You can watch the live demo version of this example here.
You can download the entire article as a PDF document. Serverless Integration with Logic Apps, Functions and Cognitive Service API.
A Logic App can be seen as a container you run a workflow in i.e. you can define a trigger followed by one of more actions in a visual designer. The trigger can be a Microsoft Managed API, a custom API App hosted in a Web App in the same region you want to call, or an API in API Management in your subscription. An action or actions that follow up on a trigger can be:
  • Microsoft Managed API
  • API App hosted in a Web App
  • API in API Management in your subscription
  • Logic Apps in the same region
  • Azure Functions in the same region
Azure Functions are another service in Azure that is serverless i.e. you can setup a Function App that is a container for your functions. The functions can be built into a browser using either C#, JavaScript, or some of the other languages. The code you create can be run and tested in the function app environment.

Our scenario for Serverless Integration

In this blog, we will showcase a Logic App in combination with Functions and another Azure Service, the recently in preview available Cognitive Services. And we will build the solution completely in a browser and run it with Azure without using Visual Studio. Both Logic App Definition and Functions can be built using Visual Studio templates. Our scenario is as follows — we have a Logic App with the following definition:
  • Twitter trigger that polls every three minutes and filters tweet on the hashtag #trump
  • The tweet text be sent to the Cognitive Service Sentiment API
  • The result of the API will be evaluated by a Function
The outcome of the evaluation will determine if the tweet will be sent to slack channel good, moderate or bad. Scenario Image - serverless integration The other component in this scenario is Slack, which is a SaaS solution that enables people to collaborate better or in this case can be used as an event channel viewer to observe the sentiment on twitter about the upcoming new US president. Kent Weare got Steef-Jan Wiggers as a guest speaker to talk about this on Middleware Friday. You can watch the live demo version of this example here.

Cognitive Service APIs

Cognitive Service API in Azure is one of the Platform services. It is a collection of API’s for Vision, Speech, Language, Knowledge and Search. You can discover each collection through the landing page. These API’s are in preview and can be found in the Azure Market Place. Cognitive Services Preview - serverless integration You can have access to the services by creating them i.e. creating a Cognitive Services account that can be tied to one of the API’s. By clicking Create a pane will appear, where you can name your Cognitive Service Account, specify your subscription and select which API you like to use. To support our scenario, we will select the Text Analytics API. Create Cognitive Services Account - serverless integration The final step before we create the account is to select the pricing tier. And to select the pricing tier that aligns with your requirements you can click view full pricing details. Cognitive Services Pricing Tier - serverless integration Note: As you can see, you can select free tier to experiment with the API. The only location that can be selected is West US!

The Evaluation Function

The Function in our scenario will be provided with the input from the Cognitive Service API response. Azure Functions provide a serverless coding capability using a Browser and the piece’s code you write can run in Azure i.e. within a Function App. Hence you are getting an on-demand experience that is based on trigger by an event. An event can be a new message in a Service Bus queue, which can be a trigger for the function and the input is the message. You can connect to various data sources or messaging solutions thus making it easy to process and react to events. Azure Functions provide bindings to enable this so as a developer; you do not have worry too much about how the data flows to and from the Function. So, in a nutshell, Functions are based on triggers and bindings and code created in various languages (C#, F#, Node.js, Python, PHP, batch, bash, or any executable), that can simplify integration. As developers, you can create HTTP-based API endpoints accessible by a wide range of applications, mobile and IoT devices. And it is scale-based and on-demand, so you pay only for the resources you consume through either a consumption plan or app price plan. The difference is that consumption is based on processing instance i.e. Function. And this plan is suitable if you have short running functions in a job or run intermitted. With an App price plan, your functions run in a dedicated VM and are always available. These price plans can be basic, standard or premium depending on your needs. And it is suitable if you have existing, under-utilized VMs that are already running other code or if you expect to run functions continuously or almost continuously. Once a Function App is provisioned, you can add functions to it. For our scenario, we add a GenericWebHook-CSharp. We will rename the function to ProcessTextAnalysisScore and in the develop tab we see some generic default code, which we will change to the code below. Generic WebHook-CSharp - serverless integration As you can see in the screen above, we can test our clicking test and provide a body, hit run and see the result in the output. The code can be developed and tested in a browser, hence we remain serverless and building this building block for our scenario.
You can download the entire article as a PDF document. Serverless Integration with Logic Apps, Functions and Cognitive Service API.

The Logic App Definition

In a Logic App, we can define a workflow based on a trigger followed by one or more actions. And we can do this in a visual designer in a browser. Once we click on our Logic App, we can select a predefined template or an empty one. We choose the latter to create a flow based upon our scenario. Our trigger will be a Twitter tweet that filters on tweets with hashtag #trump. We follow up on the trigger by calling a Cognitive Service API. This will be our first action. To connect to our Cognitive Service API, we need to know the account name and key. Logic App Trump - serverless integration We can subsequently specify the text from the tweet we like to analyze and the language in our action. Logic App Configure - serverless integration The next step is using the score from the Cognitive Service API (detect sentiment) to be sent to the function that will evaluate the score and provide us with text containing Good, Moderate or Bad. Logic App Configure 2 - serverless integration The outcome of the Function will be used for a type decision tree i.e. conditions in our Logic App definition. If the outcome (Body) our function is Good, then it will go to the IF YES branch where an action is defined to send the Tweet text and Tweeted by to Slack channel good. If that’s not the case, then the second condition will evaluate if the outcome equals Moderate and if that is true, then the IF YES branch will execute where an action is defined to send the Tweet text and Tweeted by to Slack channel moderate. If not, then the IF NO branch will execute where an action is defined to send the Tweet text and Tweeted by to Slack channel bad. Logic App Complete - serverless integration In Slack, I created a new team named sentimenttrump and within this team, three channels will be created. The tweet text and tweet by will become visible in the Slack channels of the sentimenttrump team.

Run the Logic App

Donald Trump is the new US president and there is a lot of activity around him. For instance, a lot of about him have the hashtag #trump. Therefore, it will not be a surprise that as soon as I enable the Logic App, there will be lot of tweets that will be processed. The screenshot below will tell you that. Logic App Run - Sentiment analysis - serverless integration Since there are massive number of tweets having #trump, the Logic App will be called a lot of times and some as you can see fail. The reason some fail is not that the Logic App can handle the workload, it is the Cognitive Service API that is not processing every call. Logic App Run - View details - serverless integration We have chosen a certain price plan, which only allows a specific amount of calls (see paragraph of Cognitive Services API). To be able to process the load, we will need a different pricing tier. The graphs of Cognitive Service API show that text analysis got a pretty big hit. Logic App Run - View details graph - serverless integration If we look at our Slack channel, we see that the channels good, moderate and bad are filled by tweets. Slack Notifications - serverless integration All tweets are scored and routed as such to the appropriate channel. A Slack member of SentimentTrump can observe the tweets in the channels. You can watch the live demonstration of this Logic App by Steef-Jan Wiggers on Middleware Friday.

Key considerations

The pricing aspect is one of the key drivers of Azure. In our scenario, we have shown that we can build a solution fast, with little costs using consumption price plans and lower tier Cognitive Services API. With Azure, you can scale easily with Functions and Cognitive Services API if necessary. By placing every Azure artifact in one resource group, you can easily monitor the cost of the solution. Therefore, managing cost is easy in Azure. Scale is an aspect that became apparent when running our solution. The Logic App could easily handle the load, but the Cognitive Service API could not since it was limited by pricing tier. Hence, this service could not handle the load. Thus, you have to be aware of the load you are putting on your Logic App and know beforehand if the other artifacts involved in your solution are able to handle the load. In our scenario, the Azure Function and Cognitive Service API — if one of the services needs to scale out, the cost will increase. The Cognitive Service API will require a higher pricing tier to handle the load generated by the Logic App anticipating on tweets with #trump.

Call to Action

In this blog, you have seen the power and ease of building a solution using a few building blocks in Azure:
  • Azure Functions
  • Logic Apps
  • Cognitive Services API
And I encourage you to explore them and invest time to digest the technology, experiment yourself building your own type of solution. The demand in the future for the Azure integration type of solutions will only increase. Enterprises are currently embracing the cloud on a large scale. Hence, more hybrid and cloud integration solutions will be necessary. Moreover, the skills to build such solutions will be mandatory.
You can download the entire article as a PDF document. Serverless Integration with Logic Apps, Functions and Cognitive Service API.