In this lab we will build a serverless app to respond to events streaming off an Event Hub. It will start with an Event Hub, which we will push to an Azure Function to process, and finally an Azure Logic App will orchestrate as needed.
You should now be at the Azure Dashboard. Here you can spin up the resources you need
Azure Event Hubs is a big-data and high-volume event handler in Azure. We will use it to push data from simulated IoT Devices.
Azure Functions is serverless compute for doing on-demand processing of any event. We will use this to analyze data streaming off our event hub.
Azure Logic Apps provides serverless orchestration and integration. We will use this to to fire off a business workflow whenever an IoT event requires manual intervention
Before we start writing the app, we will be using the local Azure Storage emulator to build. So open the start menu and fine the Azure Storage Emulator and open it so it will start running.
Once that is running, we can continue into VS 2017.
The Azure Function project should now be scaffolded in your local account. A few files you should note:
local.settings.json
file is your local version of application settings (things like environment variables or connection strings).host.json
file configures some advanced host features (like how many messages to grab from Event Hubs at one time).To get the Azure Function connected to the Event Hub, we need to create an app setting with the Event Hub connection string (pulled from the section above).
local.settings.json
fileValues
object for an EventHub
connection string. It should look like the below, only with your Azure Event Hub connection string:NOTE: Your connection string should NOT have an ‘EntityPath=’ near the end. If it does you have a specific hub connection string, and Functions requires a namespace
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"AzureWebJobsDashboard": "UseDevelopmentStorage=true",
"EventHub": "Endpoint=YourEventHubConnectionString"
}
}
One of the unique capabilities of Azure Functions is the ability to run the function runtime locally, set breakpoints, and test.
{ "type": "alert", "device": "readylab", "location": "seattle"}
This will open a website to send a test event. Just paste in your Event Hub connection string (from creating event hub) and submit and it should send one message to your Event Hub.
Switch back over to the Azure Function runtime window. You should see a test event got recieved (sometimes takes a few seconds).
Go back into Visual Studio, set a breakpoint on the log, and send another event. You’ll notice the breakpoint is hit, you can hover over myEventHubMessage
and click Continue to continue.
Click the stop button (or close the Azure Function popout) to close the function.
We want to start a business process whenever an IoT Event is an “Alert.”
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.ServiceBus;
namespace FunctionApp1
{
public static class Function1
{
[FunctionName("Function1")]
public static void Run(
[EventHubTrigger("iot", Connection = "EventHub")]IoTMessage myEventHubMessage,
TraceWriter log)
{
log.Info($"C# Event Hub trigger function processed a message: {myEventHubMessage.device}");
if(myEventHubMessage.type == "alert")
{
log.Info("Alert message received");
// Fire a logic app
}
}
public class IoTMessage
{
public string type { get; set; }
public string device { get; set; }
public string location { get; set; }
}
}
}
Now whenever an IoT message comes in with an alert, it will hit the if statement and (for right now) log an alert. We’ll come back later and add in the logic to call Logic Apps.
Return to the Azure Subscription and open the Logic App created earlier. The designer should open since it’s empty, but if not just click the Edit button on the top menu.
Scroll down and select the Blank Logic App template.
Notice the number of triggers or events that Logic Apps can start from. For our purposes let’s just use the Request trigger and When a HTTP Request is received
Click New step and add an action to Send an Email with the Office 365 Outlook connector. Use your Microsoft AAD account to authenticated - or if you wish use the Office 365 credentials provided to authenticate. DON’T use your Azure credentials which may be the default. Send to any email you wish with any message you prefer. In my testing I found that sending to my personal email from the pre-provisioned O365 account would get spam filtered, so consider sending to your temporary admin account (admin@xxxxx.onmicrosoft.com) and using office.com to check for mail. If you use your Microsoft AAD account to authenticate and connect it should send mail without issue.
Click the Save button and then test it out with the Run button. You can see what happens during a run, and the status of each step. You should also get an email You could continue to add more complex logic, integrate with services like Microsoft Teams or Google Docs, but for now we’ll leave this simple app.
After testing, click the Designer button on the top menu (next to save) and open the HTTP request trigger. You’ll notice you can now copy the URL needed to invoke this logic app. Keep this tab open or copy this into something like “Notepad” so you can paste it into your Azure Function.
Now that we have the URL and Logic App built, we just need to connect the Function to it and publish the function.
Go back into your Azure Function and make the final code look like this (feel free to copy paste). You’ll need to replace “https://YOUR-LOGIC-APP-URL” with your URL copied from above.
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Azure.WebJobs.ServiceBus;
using System.Net.Http;
using System.Threading.Tasks;
namespace FunctionApp1
{
private const string logicappURL = "https://YOUR-LOGIC-APP-URL";
private static HttpClient client = new HttpClient();
public static class Function1
{
[FunctionName("Function1")]
public static async Task Run(
[EventHubTrigger("iot", Connection = "EventHub")]IoTMessage myEventHubMessage,
TraceWriter log)
{
log.Info($"C# Event Hub trigger function processed a message: {myEventHubMessage.device}");
if(myEventHubMessage.type == "alert")
{
log.Info("Alert message received");
// Fire a logic app
await client.PostAsJsonAsync<string>(logicappURL, $"Alert for {myEventHubMessage.device} at {myEventHubMessage.location}");
}
}
public class IoTMessage
{
public string type { get; set; }
public string device { get; set; }
public string location { get; set; }
}
}
}
You can now try running the funciton locally, and you should notice when you send a test event it runs the function which calls the logic app.
It will take some time deploy the bits to the Azure Function. The last piece it won’t yet have is your connection string to the Event Hub. This right now is only in your local.settings.json
. So the last step you need to do is open the Application Settings for your cloud function and paste the connect string there.
local.settings.json
file if you forgot)Now the function app you had published locally is running in Azure. If you continue to simulate messages you should notice your Logic app continues to fire - even if Visual Studio is closed. If 10,000 events were pushed, your Function App would scale to process them all on-demand.
traces
and click “run” to see all tracesHopefully you were successful and understand the power and speed in which you can ship cloud-scale applications with serverless. If you have any questions or need any help feel free to contact me at jehollan@microsoft.com