Microsoft Azure Stack is an extension of Azure – bringing the agility and innovation of cloud computing to your on-premises environment and enabling the only hybrid cloud that allows you to build and deploy hybrid apps anywhere. We bring together the best of the edge and cloud to deliver Azure services anywhere in your environment.

How to use Azure Functions with IoT Hub message routing

I get a lot of asks for new routing endpoints in Azure IoT Hub, and one of the more common asks is to be able to route directly to Azure Functions. Having the power of serverless compute at your fingertips is very powerful and allows you to do all sorts of amazing things with your IoT data.

(Quick refresher: back in December 2016 we released message routing in IoT Hub. Message routing allows customers to setup automatic routing of events to different systems, and we take care of all of the difficult implementation architecture for you. Today you can configure your IoT Hub to route messages to your backend processing services via Service Bus queues, topics, and Event Hubs as custom endpoints for routing rules.)

One quick note: if you want to trigger an Azure Function on every message sent to IoT Hub, you can do that already! Just use the Event Hubs trigger and specify IoT Hub's built-in Event Hub-compatible endpoint as the trigger in the function. You can get the IoT Hub built-in endpoint information in the portal under Endpoints –> Events:

Here’s where you enter that information when setting up your Function:

If you’re looking to do more than that, read on.

I have good news and I have bad news. The bad news first: this blog post is not announcing support for Functions as a custom endpoint in IoT Hub (it's on the backlog). The good news is that it's really easy to use an intermediate service to trigger your Azure Function to fire!

Let's take the scenario described in the walkthrough, Process IoT Hub device-to-cloud messages using routes. In the article, a device occasionally sends a critical alert message that requires different processing from the telemetry messages, which comprise the bulk of the traffic through IoT Hub. The article routes messages to a Service Bus queue added to the IoT hub as a custom endpoint. When I demo the message routing feature to customers, I use a Logic app to read from the queue and further process messages, but we can just as easily use an Azure Function to run some custom code. I'm going to assume you've already run through the walkthrough and have created a route to a Service Bus queue, but if you want a quick refresher on how to do that you can jump straight to documentation here. This post will be waiting when you get back!

Next, create a Function to read data off your queue. From the quickstart page, click on “Create your own custom function” and select the template “ServiceBusQueueTrigger-CSharp”:

Follow the steps to add your Service Bus queue connection information to the function, and you’re done setting up the trigger. Now you can use the power of Azure Functions to trigger your custom message processing code whenever there's a new message on the queue.

Service Bus is billed per million operations and it doesn't add an appreciable amount to the cost of your IoT solution. For example, if I send all messages from my 1 unit S1 SKU IoT hub (400k messages/day) through to a function in this manner, I pay less than $0.05 USD for the intermediate queue if I use a Basic SKU queue. I’m not breaking the bank there.