Search Sreeni's Insights

Filter by content type
cloud November 8, 2016 7 min read

How to integrate systems with Azure Logic Apps?

Azure Logic Apps explained through Service Bus triggers, conditions, HTTP actions, approval emails, ERP integration, blob archiving and source-controlled workflows.

Share:

How to integrate systems with Azure Logic Apps?

No application lives alone these days. An order placed in our web portal has to reach the ERP system, a price file from a partner has to be loaded into our database, a large transaction needs an approval email to a manager and a copy of every document has to be archived. Normally, we write a custom integration service for each of these flows, a Windows Service polling a folder, a console app scheduled at night, some code calling an SMTP server. Every integration is a small application to build, deploy, monitor and maintain. Over the years, we have dozens of them and nobody has the full picture.

Azure Logic Apps became generally available in July 2016. In the previous article, we discussed how to use Azure DocumentDB from .NET. In this article, we are going to see what is Azure Logic Apps, the key concepts and the step by step instructions to build an integration workflow which takes an order from a Service Bus queue, routes it based on the amount and calls an ERP API or sends an approval email.

TL;DR Summary

  • Logic Apps is a cloud service to automate workflows and integrate apps, data and systems without writing much code.
  • A workflow starts with a trigger and continues with actions, conditions and loops.
  • Connectors (Service Bus, Office 365, SQL, FTP, Salesforce, HTTP and many more) handle the authentication and the protocol.
  • The workflow is designed visually and saved as JSON (Workflow Definition Language), so it can be in source control and deployed with ARM templates.
  • Every run is recorded with the inputs and outputs of each step, which makes troubleshooting easier.
  • It is billed per action execution.
  • Use Logic Apps for orchestration and integration, use Azure Functions or Web APIs for complex business logic.

Challenges

Custom code for every integration

Each integration needs code for connecting, authenticating, retrying, logging and error handling. Most of this code is the same across integrations, but it is written again and again.

Hidden integrations

When integrations are scattered across Windows Services, scheduled tasks and scripts on different servers, nobody knows which systems are connected and what happens when one of them is down. It is a big blind spot in our operations.

Troubleshooting failed flows

When an order didn’t reach the ERP system, finding out at which step it failed means searching through logs on multiple servers, if the logs exist at all.

What is Azure Logic Apps?

Azure Logic Apps is a cloud service that helps us to automate and orchestrate tasks, business processes and workflows when we need to integrate apps, data, systems and services across enterprises or organizations, using a visual designer and a large set of managed connectors.

In simple terms, Logic Apps is a “when this happens, do these steps” engine in the cloud. We draw the flow, choose the connectors, and Azure runs it, retries it and records every run.

The key concepts are,

  • Trigger - the event which starts a workflow run, ex: a new message in a queue, a new file in FTP, an HTTP request, a schedule.
  • Action - a step in the workflow, ex: call an API, send an email, insert a row.
  • Connector - a ready made integration with a service, handles authentication and the API calls.
  • Condition and loops - control flow based on the data, ex: if amount is greater than 10,000.
  • Run history - every execution with the status, inputs and outputs of each step.

The following diagram depicts the workflow we are going to build.

Order integration workflow in Logic Apps
Order integration workflow in Logic Apps

The path highlighted in purple is the normal path for most of the orders. The orders with a high amount take the approval path.

Why Logic Apps is important?

In order to reduce the custom integration code, to have all integrations visible in one place with the run history, and to deliver new integrations in days instead of weeks, Logic Apps is a good fit for many of our integration needs. It also improves the operations, the built-in retry policies and the run history reduce the time we spend in investigating failed flows.

Steps to Implement:

Follow the following instructions to create the order integration workflow.

Prerequisites

  • A Service Bus namespace with a queue named orders. We discussed Service Bus queues last year, refer the “How to send and receive messages with Azure Service Bus queues?” article.
  • An ERP API endpoint which accepts an order as JSON over HTTPS.
  • An Office 365 account for sending the approval email.
  • A storage account with a blob container processed-orders.

Create the Logic App

  • Login to the Azure portal.
  • Click “New” → “Enterprise Integration” → “Logic App”.
  • Enter the name (ex: order-integration), select the subscription, resource group and location. Replace the input place holders with actual values.
  • Open the Logic App, the designer opens with the templates. Select “Blank Logic App”.

Add the trigger

  • Search for “Service Bus” and select “Service Bus - When a message is received in a queue (auto-complete)”.
  • Create the connection with the Service Bus connection string, select the orders queue and set the polling interval (ex: every 1 minute).

Read the message

  • The message content from Service Bus is base64 encoded. Add a “Compose” action, and in the code view, set its input with an expression which decodes the content into JSON.

    @json(base64ToString(triggerBody()?['ContentData']))
  • The output of this action is the order object, and we can refer its properties in the next steps, for example @outputs('Compose')?['amount'].

Add the condition

  • Click “New step” → “Add a condition”. Set the condition to check whether the order amount is greater than 10,000.

    @greater(outputs('Compose')?['amount'], 10000)
  • In the “If yes” branch, add “Office 365 Outlook - Send approval email”. Set the recipient to the approvers group, the subject with the order id and the options “Approve, Reject”.

  • In the “If no” branch, add an “HTTP” action. Set the method to POST, the URI to the ERP API, the headers and the body to the order JSON.

    Method : POST
    URI    : https://<erp api host>/api/orders
    Headers: { "Content-Type": "application/json" }
    Body   : @outputs('Compose')

    Note: Don’t put the ERP API key directly in the workflow. Use the authentication options of the HTTP action, or keep the key as a parameter of the ARM template filled from Key Vault during deployment.

Archive the order

  • After the condition, add “Azure Blob Storage - Create blob”. Set the folder path to processed-orders, the blob name to the order id and the content to the order JSON.

Save and pulse check

  • Save the Logic App.
  • Just to pulse check, send a test message to the orders queue (ex: with Service Bus Explorer) with an amount below and above 10,000.
  • Open the Logic App → “Runs history”. Click a run to see each step, its status, inputs and outputs.

Source control and deployment

The designer is great to build the flow, but for production, the workflow must be in source control and deployed through our release pipeline like any other application.

  • Open “Code view” to see the workflow definition as JSON.
  • Use the “Azure Resource Group” project in Visual Studio with the Logic Apps Tools for Visual Studio to edit and keep the definition as an ARM template.
  • Deploy the template through VSTS Release Management, with the connection strings and endpoints as parameters per environment.

When not to use Logic Apps?

Logic Apps is an orchestration tool, not a programming language. Because of the following reasons, we don’t use it for everything,

  • Complex business rules and calculations are hard to read and test in a workflow designer. Put them in an Azure Function or a Web API and call it from the Logic App.
  • Very high volume, low latency processing (thousands of messages per second) can become expensive with per action billing.
  • When a flow is only between our own two services, a simple Service Bus subscription can be enough.

Must Do and Don’ts

  • Must Do: Keep the workflow definition in source control and deploy it with ARM templates.
  • Must Do: Configure retry policies and a failure path (ex: send to a dead letter queue or alert) for important flows.
  • Must Do: Use separate Logic Apps and connections per environment.
  • Should: Keep each Logic App focused on one business flow.
  • Don’t: store secrets in the workflow definition.
  • Don’t: implement complex logic with nested conditions and expressions, it becomes unmaintainable.

Wrap up

In this article, we have looked at the challenges of custom integrations, what is Azure Logic Apps, the key concepts and the step by step instructions to build a workflow with a Service Bus trigger, a condition, an HTTP call, an approval email and a blob archive, and how to keep it in source control. I really appreciate that you took the time to read this article. In the next article, I will walk you through how to set up Cloudflare in front of an Azure Web App.

Related Posts