Logic Apps Hello World: Using Azure Functions inside Logic Apps

Posted: April 8, 2016 in Azure App Services, Logic Apps
Tags: , , , , , ,

00-Logic-Apps

Microsoft Integration Product team announced yesterday on their monthly webcast (that you can see it here) some of the new features that are now available in Logic Apps like:

  • “Run” any trigger now
  • HTTP Webhook in Designer
  • SQL Azure Connector now supports Store Procedures
  • Azure Functions Integration and a few other features

In this my first blog post about Logic Apps I will talk about this last feature the new “Azure Functions Integration”.

At Build it was a new feature announced in Azure call “Azure Functions” which basic allows you to run small snippets of code out in the cloud. You can learn more about it here: https://azure.microsoft.com/en-us/services/functions/. So let’s explore this new feature and see how can we use it inside our Logic Apps.

Get started with Azure Functions

To get start using Azure Functions, currently in Preview, you can access to https://functions.azure.com/ and you just need to fill:

  • Your Azure Subscription from the combobox
  • Give a Name to the Function App
  • And select your desired Region

01-Get-Start-with-Azure-Functions

  • And then click “Create + Get started” button.

This will create your Function App and will redirect you to your azure portal subscription.

02-Azure-Portal-Azure-Functions

Let’s leave this windows open for now, because we will create our function through the Logic App design, and get to your Azure Portal subscription.

Create your Hello World Logic App

For this exercise I intend to create a very basic Logic App that will run once a day, be with me this is a simple Hello World, but instead of writing a static message “Hello world” I want to provide a more dynamic behavior and find out the day of the week that this Logic App is being executed and write “Hello World! this is #LogicApps and I hope you have a great Friday” … or Saturday, Sunday, Monday, …

For that I will call an Azure Function to get the day of the week and then post on my twitter this twitter post Smile.

To accomplish that we need:

  • On the Azure Portal click on “Logic Apps” option and click “Add” to create a new Logic App.

03-Azure-Portal-Logic-Apps

    • If is not present you can find it by clicking “+ New” –> “Web + Mobile” –> “Logic App

04-Azure-Portal-Logic-Apps

Note: I will create everything from the scratch, including creating a:

  • Free App Service Plan: represents a set of features and capacity that you can share across multiple apps in Azure App Service, including Web Apps, Mobile Apps, Logic Apps or API Apps. These plans support different pricing tiers (Free, Shared, Basic, Standard and Premium) where each tier has its own capabilities and capacity.
  • and Resource group: are logical containers that allow you to group individual resources such as virtual machines, storage accounts, websites and databases so they can be managed together.
  • In the “Create logic app” panel we need to give it:
    • A “Name”, for example “MyDynamicHelloWorldLApp”
    • And select the desired “Subscription” (if you have more than one)

05-Azure-Portal-Create-Logic-App

    • Set a “Resource Group” or create a new one by selecting “+ New” option and give a resource group name in the “New resource group name” text field

06-Azure-Portal-Create-Logic-App-Add-Resource-Group

    • And finally define the “App Service Plan” by choosing an existing plan or add a new one by
      • clicking “App Service Plan” –> “Create New” and then we need to:

07-Azure-Portal-Create-Logic-App-Create-App-Service-Plan

      • set a “App Service Plan” name, for example “MyFreeLogicAppServicePlan”
      • select your desired Location
      • and then click “Price tier” –> “View All” –> “F1 Free

08-Azure-Portal-Create-Logic-App-Create-App-Service-Plan-free

      • Click “Select” and in then “OK
    • Finally, on the “Create Logic app” panel click “Create” for the deployment process to start.

We have to wait a few seconds for the deployment process to finish, once finished we will be notified in the notification area.

09-Azure-Portal-Logic-App-Deploy-success

Now, if we refresh the Logic Apps page, we will now see and have access to the new Logic App we were building.

10-Azure-Portal-Logic-Apps-list

Note: if we click on “” you can pin this Logic App to your dashboard.

To edit and create the logic flow of you Logic App, you just need to click on the Logic App name to open the Logic Apps Designer.

11-Azure-Portal-Logic-Apps-Designer-Empty

To accomplish the goal that we described in the beginning of this post we need to:

  • When your access to the Logic Apps Designer a search box is presented were you can search for available connectors or triggers (API) management by Microsoft (default option) or inside your subscription. From “Show Microsoft management APIs” option, click on the “Recurrence” trigger

12-Azure-Portal-Logic-Apps-Designer-Add-recurrence-trigger

    • That will enable our Logic App to run periodically based on frequency and interval, in this case:
      • Frequency: “Day”
      • Interval: “1”

13-Azure-Portal-Logic-Apps-Designer-Add-recurrence-trigger-conf

Note: Recurrence trigger do not have anything in the inputs object the common properties for all triggers are sufficient to define a basic recurring trigger.

  • Select the plus sign, and then choose “Add an action

14-Azure-Portal-Logic-Apps-Designer-plus

  • When you select “Add an Action”, again the search box will be presented were all the connectors with their available actions are listed. But now, on the search box you can also select the option: “Show Azure Functions in my subscription

15-Azure-Portal-Logic-Apps-Designer-Show-Azure-Functions

    • This will present all the Azure Functions that you can use inside your Logic App;
    • And also the availability to create new WebHook Node JS Functions
  • From the search box select “Show Azure Functions in my subscription” and then “Create New Function”.

16-Azure-Portal-Logic-Apps-Designer-Create-New-Azure-Functions

    • On the “Configure Function Inputs”, and because with don’t have or need to send any input (in this case), you can type an empty JSON message: “{}” and click “Next

17-Azure-Portal-Logic-Apps-Designer-Configure-Azure-Functions-input

    • On the “Function Authoring
      • Set the “Function name” as “GetHelloWorldTwitterMsg”
      • And past the following code in the “Code” text box
module.exports = function (context, data) {
        
    var d = new Date();
    var weekday = new Array(7);
    weekday[0]=  "Sunday";
    weekday[1] = "Monday";
    weekday[2] = "Tuesday";
    weekday[3] = "Wednesday";
    weekday[4] = "Thursday";
    weekday[5] = "Friday";
    weekday[6] = "Saturday";

    var n = weekday[d.getDay()];
        
  // Response of the function to be used later.
  context.res = {
            
    body: 'Hello World! this is #LogicApps and I hope you have a great ' + n
                
  };
                
  context.done();
                
};
    • Click “Create

18-Azure-Portal-Logic-Apps-Designer-Configure-Azure-Functions-Function-Authoring

  • Once again, select the plus sign, and then choose “Add an action” option
  • From the search box, type “Twitter” and select the “Twitter – Post a new tweet” action

19-Azure-Portal-Logic-Apps-Designer-Add-Twitter-Action

    • The first step is to sign in to twitter and authorize Azure AppService Logic Apps to use your account

20-Azure-Portal-Logic-Apps-Designer-Add-Twitter-sin-in

21-Azure-Portal-Logic-Apps-Designer-Add-Twitter-Autorize

      • After you authorize the Logic App to use your account you can now configure the text to be posted in your twitter account
    • On “Post a new tweet”, click on “TWEET TEXT” text box and the outputs from the “GetHelloWorldTwitterMsg” function will be presented
      • Select “Body” output to be the tweet text to be posted on your twitter

22-Azure-Portal-Logic-Apps-Designer-Twitter-Post-a-new-tweet

  • Finally, you can select “Save”, on the Logic Apps Designer menu, to make your logic app live.

If you now check your twitter account, we will see a new tweet:

24-new-tweet

Manager and Testing your Azure Functions

If you access to your Azure Function app page that we had accessed at the beginning of this post, and refresh it, you will see the new Function that we have created inside our Logic App

25-new-function-inside-Function-app-page

Here we are also able to “debug” and try the function code to check if everything is ok.

26-test-azure-function

A special thanks to my colleague André Silva, who got curious about what I was doing and spend a little of his time with me and helped me with the Javascript code.

Comments
  1. […] or be called from other applications, for example from a logic app, as has been described here by Sandro Pereira. Azure Functions provide out of the box connections for triggers, input and output to a lot of […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s