Tutorial: Respond to Azure Service Bus events received via Azure Event Grid by using Azure Logic Apps

In this tutorial, you learn how to respond to Azure Service Bus events that are received via Azure Event Grid by using Azure Logic Apps.

Prerequisites

If you don't have an Azure subscription, create a free account before you begin.

Create a Service Bus namespace

Follow instructions in this tutorial: Quickstart: Use the Azure portal to create a Service Bus topic and subscriptions to the topic to do the following tasks:

  • Create a premium Service Bus namespace.
  • Get the connection string.
  • Create a Service Bus topic.
  • Create a subscription to the topic. You need only one subscription in this tutorial, so no need to create subscriptions S2 and S3.

Send messages to the Service Bus topic

In this step, you use a sample application to send messages to the Service Bus topic you created in the previous step.

  1. Clone the GitHub azure-service-bus repository.

  2. In Visual Studio, go to the \samples\DotNet\Azure.Messaging.ServiceBus\ServiceBusEventGridIntegrationV2 folder, and then open the SBEventGridIntegration.sln file.

  3. In the Solution Explorer window, expand the MessageSender project, and select Program.cs.

  4. Replace <SERVICE BUS NAMESPACE - CONNECTION STRING> with the connection string to your Service Bus namespace and <TOPIC NAME> with the name of the topic.

    const string ServiceBusConnectionString = "<SERVICE BUS NAMESPACE - CONNECTION STRING>";
    const string TopicName = "<TOPIC NAME>";
    
  5. Build and run the program to send 5 test messages (const int numberOfMessages = 5;) to the Service Bus topic.

    Console app output

Receive messages by using Logic Apps

In this step, you create an Azure logic app that receives Service Bus events via Azure Event Grid.

  1. Create a logic app in the Azure portal.
    1. Select + Create a resource, select Integration, and then select Logic App.
    2. On the Logic App - Create page, enter a name for the logic app.
    3. Select your Azure subscription.
    4. Select Use existing for the Resource group, and select the resource group that you used for other resources (like Azure function, Service Bus namespace) that you created earlier.
    5. Select the Location for the logic app.
    6. Select Review + Create.
    7. On the Review + Create page, select Create to create the logic app.
  2. On the Logic Apps Designer page, select Blank Logic App under Templates.

Add a step receive messages from Service Bus via Event Grid

  1. On the designer, do the following steps:
    1. Search for Event Grid.

    2. Select When a resource event occurs - Azure Event Grid.

      Logic Apps Designer - select Event Grid trigger

  2. Select Sign in, enter your Azure credentials, and select Allow Access.
  3. On the When a resource event occurs page, do the following steps:
    1. Select your Azure subscription.
    2. For Resource Type, select Microsoft.ServiceBus.Namespaces.
    3. For Resource Name, select your Service Bus namespace.
    4. Select Add new parameter, and select Suffix Filter.
    5. For Suffix Filter, enter the name of your Service Bus topic subscription. Logic Apps Designer - configure event
  4. Select + New Step in the designer, and do the following steps:
    1. Search for Service Bus.

    2. Select Service Bus in the list.

    3. Select for Get messages in the Actions list.

    4. Select Get messages from a topic subscription (peek-lock).

      Logic Apps Designer - get messages action

    5. Enter a name for the connection. For example: Get messages from the topic subscription, and select the Service Bus namespace.

      Logic Apps Designer - select the Service Bus namespace

    6. Select RootManageSharedAccessKey, and then select Create.

      Logic Apps Designer - select the shared access key

    7. Select your topic and subscription.

      Screenshot that shows where you select your topic and subscription.

Add a step to process and complete received messages

In this step, you'll add steps to send the received message in an email and then complete the message. In a real-world scenario, you'll process a message in the logic app before completing the message.

Add a foreach loop

  1. Select + New step.

  2. Search for and then select Control.

    Image showing selection of Control category

  3. In the Actions list, select For each.

    Image showing selection of For each control

  4. For Select an output from previous steps (click inside text box if needed), select Body under Get messages from a topic subscription (peek-lock).

    Image showing the selection of input to For each

Add a step inside the foreach loop to send an email with the message body

  1. Within For Each loop, select Add an action.

    Image showing the selection of add an action button inside the for each loop

  2. In the Search connectors and actions text box, enter Office 365.

  3. Select Office 365 Outlook in the search results.

  4. In the list of actions, select Send an email (V2).

  5. Select inside the text box for Body, and follow these steps:

    1. Switch to Expression.

    2. Enter base64ToString(items('For_each')?['ContentData']).

    3. Select OK.

      Image showing the expression for Body of the Send an email activity

  6. For Subject, enter Message received from Service Bus topic's subscription.

  7. For To, enter an email address.

    Image showing the Send email activity configured

Add another action in the foreach loop to complete the message

  1. Within For Each loop, select Add an action.

    1. Select Service Bus in the Recent list.

    2. Select Complete the message in a topic subscription from the list of actions.

    3. Select your Service Bus topic.

    4. Select a subscription to the topic.

    5. For Lock token of the message, select Lock Token from the Dynamic content.

      Logic Apps Designer - complete the message

  2. Select Save on the toolbar on the Logic Apps Designer to save the logic app.

    Save logic app

Test the app

  1. If you haven't already sent test messages to the topic, follow instructions in the Send messages to the Service Bus topic section to send messages to the topic.

  2. Switch to the Overview page of your logic app. You see the logic app runs in the Runs history for the messages sent. It could take a few minutes before you see the logic app runs. Select Refresh on the toolbar to refresh the page.

    Logic Apps Designer - logic app runs

  3. Select a logic app run to see the details. Notice that it processed 5 messages in the for loop.

    Logic app run details

  4. You should get an email for each message that's received by the logic app.

Troubleshoot

If you don't see any invocations after waiting and refreshing for sometime, follow these steps:

  1. Confirm that the messages reached the Service Bus topic. See the incoming messages counter on the Service Bus Topic page. In this case, I ran the MessageSender application twice, so I see 10 messages (5 messages for each run).

    Service Bus Topic page - incoming messages

  2. Confirm that there are no active messages at the Service Bus subscription. If you don't see any events on this page, verify that the Service Bus Subscription page doesn't show any Active message count. If the number for this counter is greater than zero, the messages at the subscription aren't forwarded to the handler function (event subscription handler) for some reason. Verify that you've set up the event subscription properly.

    Active message count at the Service Bus subscription

  3. You also see delivered events on the Events page of the Service Bus namespace.

    Events page - delivered events

  4. You can also see that the events are delivered on the Event Subscription page. You can get to this page by selecting the event subscription on the Events page.

    Event subscription page - delivered events

Next steps