Azure Serverless Computing Cookbook
上QQ阅读APP看书,第一时间看更新

Integrating Logic Apps with serverless functions

In the previous recipe, you learned how to integrate different connectors using Logic Apps and developed a simple logic of checking whether the followers count is greater than 200. As it was a simple logic, you were able to implement that in Logic Apps itself. If you need to implement a complex logic, then it wouldn't be possible. In that case, you can implement the complex logic in Azure Functions and invoke Azure Functions from Logic Apps.

In this recipe, you will see how to integrate Azure Functions with Logic Apps. For the sake of simplicity, we will not develop a complex logic. However, we will use the same logic (followersCount > 200) in Azure Functions and invoke it from Logic Apps.

How to do it...

In this section, we'll integrate Azure Functions with Logic Apps by performing the following steps:

  1. Create a new function by choosing the HTTP trigger with Authorization Level as Anonymous, and name it ValidateTwitterFollowerCount.
  2. Replace the default code with the following, as shown in Figure 3.22. You can implement some complex logic here. The following Azure HTTP trigger accepts two parameters:

    The name of the owner of the tweet

    The follower count of the owner

    Just for the sake of simplicity, if the name of the user starts with the letter p, add 100 to the followers count. Otherwise, return the followers count. In your projects, you can implement complex business logic in the HTTP trigger:

    Figure 3.22: Azure Function HTTP trigger
  3. Navigate to the NotifyWhenTweetedByPopularUser logic app and click on Logic App Designer to start editing the logic app.
  4. Now, hover the mouse on the arrow mark to reveal a + icon, as shown in Figure 3.23. Click on it and then click on Add an action button:
    Figure 3.23: Logic app—Add an action
  5. In the Choose an action section, search for Functions, click on Azure Functions, and then click on Choose an Azure Function, as shown in Figure 3.24:
    Figure 3.24: Logic app—searching for the Azure Functions connector
  6. Clicking on the Choose an Azure function button in Figure 3.24 will reveal a list of all the available Azure function apps. You can search for the function app where you have developed the ValidateTwitterFollowerCount function, as shown in Figure 3.25:
    Figure 3.25: Logic app—choosing an Azure function app
  7. When you choose the function app, it will show all the functions inside it. Click on the ValidateTwitterFollowerCount function, as shown in Figure 3.26:
    Figure 3.26: Logic app—choosing Azure Functions
  8. Now, provide the input to the ValidateTwitterFollowerCount function, as shown in Figure 3.27:
    Figure 3.27: Logic app—passing inputs to Azure Functions
  9. In order to receive the response, open the Condition step and add the Body variable, as shown in Figure 3.28. The Body variable contains the response body of the Azure function named ValidateTwitterFollowerCount. This means that, instead of directly comparing the Twitter follower count, we are comparing the response returned by the Azure Function HTTP trigger with the value 200:
    Figure 3.28: Logic app—receiving inputs from Azure Functions
  10. That's it. Save the changes and go to Twitter and create a tweet with #AzureFunctions. The following is an example where the followersCount returned by Twitter for my tweet is 310. However, the Azure function added 100 to the followers count and returned 410 in the response body:
Figure 3.29: Logic app—execution history

In this section, you have learned how to integrate Logic Apps with Azure Functions.

There's more...

If you don't see the intended dynamic parameter, click on the See more button, as shown in Figure 3.30:

Figure 3.30: Logic app—dynamic content—see more

In this recipe, you have learned how to integrate Azure Functions with Logic Apps. In the next recipe, let's explore how to integrate Azure Functions with Cosmos DB.