Every business has processes designed to carry out a certain task. Ever since Henry Gantt (remember the Gantt chart?) introduced workflows to business environments business processes have become more efficient. A workflow is a sequence of activities required to complete a task. Businesses usually have a lot of workflows because they carry out several business processes.
Some of these processes are manual because of the activities involved and hence, can’t be automated. But a good number of these processes like onboarding a new employee or customer are digital, and hence can be automated. This is where Azure Logic Apps come in.
What’s Azure Logic Apps?
According to the Azure Logic Apps documentation, “Azure Logic Apps is a cloud service that helps you schedule, automate, and orchestrate tasks, business processes, and workflows when you need to integrate apps, data, systems, and services across enterprises or organizations.”
Azure Logic Apps helps with integrating existing third party services, APIs, and apps to your business processes, workflows, and systems through its numerous pre-built connectors. This leaves you to focus on apps’ business logic and functionality.
In this article, we will take you through some basics of creating a logic app, and we would do this by creating a simple logic app. While doing this, we would cover the following concepts; variable declaration, making HTTP requests to external APIs, parsing the returned JSON data, composing a workflow, in-built functions, and loops.
Before we proceed here are some terminologies you should know.
- Connectors: Connectors are wrappers around APIs or third-party apps and services that expose the underlying services of these products to Azure Logic Apps.
- Triggers: Some connectors come with something called a trigger. Triggers in connectors get fired when a certain event or new data received meets a predefined condition. A good example of an event is when you receive an email, or when a certain time approaches. Some of the triggers in Azure Logic Apps are HTTP triggers, recurrence triggers, request triggers and so on.
- Actions: Actions are the tasks you want to be executed when a trigger fires. They usually come after a trigger in the logic app and execute only when the trigger you attach to it fires.
Now that that is taken care of, let’s move on to creating our logic app.
Building A Simple Logic App
To build an Azure Logic App, you can use the Azure Portal, Visual Studio(IDE), or Visual Studio Code(text editor). In this article, we would make use of the Azure Portal. If you don’t have an Azure account, sign up for a free account here. Once you are done with that, sign in to the Azure Portal to get started. The homepage of the portal should look like this.
The logic app we will create will get data from an API with dummy to-do list data. The JSON data contains a userId, an id, a title, and a completed property. What we want our logic app to do is to go through each entry in the JSON data and check if the completed property is set to “true”.
After that, we would create a file for each to-do title whose completed property is set to “true” using a blob storage.
Create A Logic App Project
1. To begin, search for Logic Apps in the search bar above and select it.
2. When the Logic Apps page comes into view, select Add.
3. Fill the form in the Logic App pane by the left, just like you see above. If your subscription isn’t “Free Trial”, select what’s applicable to you. The Resource Group is like a folder used to organize your logic apps and other Azure services you use. Also, for location, you can select any of the locations listed there. Leave every other thing as it is and click on create.
4. Wait for a while for the deployment process to be done. Once it is, click on the notifications icon, and click on Go to resource.
5. Click on Blank Logic App to create your app.
Add An HTTP Trigger
Now, let’s add an HTTP trigger. All Azure Logic Apps have to begin with a trigger.
1. Search for HTTP and select it.
2. Next, select the HTTP method as GET and enter this(https://jsonplaceholder.typicode.com/todos) as the API URL. Then click on the click on the title bar of the trigger to collapse the pane.
Parsing The JSON
Now let’s parse the JSON data received from the HTTP trigger
1. Click on New Step.
2. Search for Parse JSON and select it.
3. Click on the Content input field. A Dynamic content pane should pop up. You should see a list of outputs from the HTTP trigger. Click on Body to access the JSON data.
4. Next is the Schema input field. To get the schema, we need to copy a sample of the data from the API. To do that, visit the API link above and copy just two entries just as it’s done in the above image.
5. At the bottom of the Parse JSON action window, you should see Use sample payload to generate schema. Click on it and paste the sample you copied from the above step. Delete the comma at the last entry and add the closing “]” bracket after the last “}” bracket. Click Done and collapse the Parse JSON window.
Add A Variable
Now we will initialize a variable. The variable will serve as an identifier for each to-do item when we create a file to save them.
1. Click on New Step. in the search bar, search for “initialize variable” and select it.
2. Enter a name for the variable, choose Integer as the type and set the initial value to zero. We would be increment this variable every time a to-do file is created. Collapse the pane when you’re done.
Add A Loop
We will make use of a For each loop to go through the collection of to-do items. We would then use a conditional block to check each item’s completed value is true, and if the id is equal to 1. This way only a few entries are returned. There are 200 entries in the to-do list data.
Normally, to add a For each loop, you click on New step and search for it. But whenever you make use of certain outputs from previous triggers, Azure Logic Apps automatically creates a For each loop for you. So let’s take advantage of that.
1. Click on New step, search for “condition” and select it.
2. Click on Choose a value. A Dynamic content pane is displayed to you. Select completed. The moment you do this, a For each loop action is created, and the condition you are creating is placed inside, collapsed.
3. Click on the Condition action to expand it. Complete it as you see in the above picture. Type “true” in Choose a value for the first condition, and “1” for the second. To add a new condition, Click on Add > Add Row. Make sure to check the checkbox beside both conditions.
4. Now that we have added our condition, let’s make it do something when true. We won’t be doing anything for when it is false. In the If true block, click on Add an action, search for “create blob” and click on it.
NB: You would need to create a blob storage before you can do this. So make sure to create one and add a container to it before doing this step. You can learn how to do that here.
5. In the Create blob action pane, set the folder path to the container you created in the above step. For Blob name and Blob content, we would add that dynamically.
6. Click on the Blob name field. A Dynamic content pane should open. Type in ”todo_”. Then from the Dynamic content pane, select “counter.” If you don’t see it immediately, click on see more under Variables. Now add “.txt” after “counter” in the Blob name field.
7. Click on the Blob content field. A Dynamic content pane should open up here as well. Select “title” from the pane, and type “is completed.” after it in the Blob content field. If you don’t see “title” immediately on the Dynamic content pane, click on see more under Parse JSON.
8. The final step is to increment or variable. Click on Add an action in the For each action pane. Search and select “increment variable”. Select counter from the drop-down list under Name. It should be the only variable there. Then enter “1” has your value.
That’s it for the For each action, and the logic app itself. Before we run the app, we need to change a setting on the For each action. By default, Logic Apps runs each iteration of a loop in parallel. We don’t want that. So let’s change that.
Go to the title bar of the For each action. At the extreme right is an icon with three dots, click on it and select settings. Enable the switch button beside Concurrency control, and bring the slide below it to 1. Click Done when you’re through. You can now collapse the For each action.
Click on Run on the menu bar and wait for the notification that will show that your app ran successfully. You should see something similar to the above image if it runs successfully. You can check the files created in the container you created in your blob storage.
One concept we didn’t cover in our logic app above is the use of in-built functions. In-built functions are expressions in Azure Logic Apps that allow you to perform certain actions on the data in your workflow. You can do things such as manipulate a string, convert values, or perform math operations.
There are several categories of in-built functions you can use. There are string functions, collection functions, logical comparison functions, conversion functions, math functions, date and time functions and so on. These functions are referred to as expressions in the logic app designer.
It’s easy to use an in-built function in your app. Let’s say you want to generate a username for a user whose first name and last name is given, we can make use of some of the expressions in Logic Apps to achieve this.
For this task, we would need two of expressions, the subString() function, and the concat() function. The subString() function returns a specified portion of any string passed into it, while the concat() function takes in two strings and joins them together. To generate the username, first you use the subString() function to get the first three(or any preferred number) letters of the first and last name, then you use the concat() function to join them together.
You can create a variable to store the result for future reference. That way, you don’t have to keep entering the expression to get the username.
Advantages Of Azure Logic Apps
As you would have probably noticed, there are several advantages to using logic apps in your business. Let’s examine them below.
- No-code development: Unlike some other platform that requires you to use a programming language for automation, azure logic apps are designed by click actions and do not need one.
- Serverless: Azure Logic Apps is a serverless app service and hence doesn’t require you to have a server to use it. Logic app instances run on Azure servers instead.
- Cost-effective: Azure Logic Apps runs a Pay-As-You-Go( PAYG) payment model (So you’re charged only when a logic app instance is running. This is much cheaper compared to running the logic app on an on-site server.
- Automatic scaling: This is one of the features of serverless apps. Azure Logic App can execute as many logic app instances has the situation requires, without the need to pay for a fixed number of executions since it’s a PAYG service.
- Templates: Azure Logic Apps comes with templates for commonly used workflows and integrations. This saves you time when you want to develop a new workflow.
This article is just to give you a peek into what Azure Logic Apps is and how it works. The Logic Apps documentation contains a full explanation of all its parts. You can also find tutorials and logic app samples on the website.