IoT Hub Interceptor In Azure

Shishir Kumar Mishra
3 min readNov 2, 2020

When you are working on serverless applications that consume data from IoT Hub, it really becomes very tricky to modify the functionality and test it before pushing it to the PROD environment. The situation becomes more complicated if you don't have any test device which communicates to the DEV environment and there is two possible way to solve a particular problem.

A) Device Simulations

It allows you to generate production-quality (sort of) device data that can be sent to IoT Hub. I won’t get into details as we have plenty of examples on the internet like this or this.

B) IoT Hub Interceptor

The other possible way is to get a copy of data coming to IoT Hub and route it to some other serverless application. Deep down, IoT Hub is implemented on Azure Event Hub and thus inherits all its good features of it like partitions and consumer groups. It is a consumer group feature of IoT Hub which allows us to get a copy of data coming to, modify it if required, and then route it to other serverless applications. More details on “Consumer Groups” can be found here. “IoT Hub Interceptor” is not an official term from Microsoft and has been coined by me :-)

The steps to create an additional consumer group in IoT Hub are as follows :
a) From the Azure Resource group, select the IoT Hub resource
b) Select Built-in endpoints from the left blade as highlighted by 1 in the following screenshot

c) Enter a new desired consumer group as highlighted by 2.
d) Newly Saved Consumer group will appear under the Consumer Groups as highlighted by 3.
e) Once the Consumer group is created we need to copy the information of Event Hub-compatible name (4) and endpoints (5).

We need to create a function app that can receive data from the above event hub details.

a) From Visual Studio (2019), follow New Project Wizard steps to create a Function App

b) Select Event Hub trigger options from Wizard

and enter Event Hub-compatible name and endpoints (4 and 5 from previous steps) to 1 and 2 respectively. Unfortunately, the wizard does not allow us to enter the consumer group name so we have to introduce variables in the code directly.

We have to create the following environment variables in local.settings.json or local OS.

SourceIoTEventHubName: Should have the same value as in
Event Hub-compatible name (point 4)
SourceConsumerGroupName:
Should have the same value as in
Consumer Group name (point 3)
SourceIoTEventHubConnectionString:
Should have the same value as in
Event Hub-compatible endpoints (point 5)

We can loop through the message and enrich it if required like assigning deviceId explicitly like the following.

After enrichment, we can provide the message to business class methods like publishers. PublishAsync. In the above example publisher is pushing the message to another queue from where another Azure function can dequeue and perform core business functionality.

I found it very helpful in the situation where none of our devices were communicating with DEV IoT Hub and we wanted to be absolutely confident about our code changes before pushing into the PROD environment.

--

--