In this quick sample you will learn how to capture data from your devices or sensors, perform aggregation, filtering or some other custom processing on this data, and store it on a database. All this will be done by setting up the integration between IoT Hub and Azure Functions, while learning a bit about triggers and bindings for Azure Functions, and drop your processed data on Cosmos DB.
This sample project will cover the technologies and services below: * Azure IoT Hub * Azure Functions (triggers and bindings) * Cosmos DB
Step 1: IoT Hub
First, we need to set up an IoT Hub and start sending messages to it. For simplicity, we chose to simulate a real device using our PC. If you’d like to do the same, follow the detailed instructions here
This one is super easy, since there’s such great documentation already. All you have to do is choose your preferred stack, create an IoT Hub, copy-paste some code, and run. The only potential snag is authentication – make sure you get those connection strings right!
When we ran through this, we chose to use Node. After completing the instructions above, we left the device simulation active on our machine, which kept pushing events to IoT Hub. Next, we’ll have something react to these events.
Step 2: Azure Functions triggered by IoT Hub
Azure Functions has integration with IoT Hub out of the box, so it is very simple to get this up and running. Follow the steps below to get an Azure Function reacting to IoT Hub events:
- Create a Function App by clicking the below in the Azure Portal “Create New” blade. This is the “container” which holds your functions.
- Once the Function is created, navigate to the “Create new function from template page”:
- Conveniently, Azure Functions will create the connection with IoT Hub for you after selecting this template, so you just have to click on the "new" button and select the proper IoT Hub instance:
- Click "Create" and the Function just created will be triggered by the IoT Hub events
Step 3: Cosmos DB creation
Now we need a place to store the events sent to IoT Hub after the processing occur on the Function code, so we followed the steps below to create a Cosmos DB database:
- Navigate back to the "Create new" blade in the Azure Portal, and this time select "Cosmos DB"
Select SQL as the API to use on Cosmos DB, as it's important to note that Azure Functions only works with Cosmos DB if the SQL or Graph APIs are selected.
That's it! It takes a bit for the resource to be provisioned, so give it a few minutes
Step 4: Cosmos DB output binding
Now it's time to hook everything together using the Azure Functions Cosmos DB output binding, and integrate our Function with it following the steps below:
- Navigate to the "Integrate" tab of your Azure Function
- Create a new "Cosmos DB output" binding
- As with IoT Hub, we have to create a new Cosmos DB Account connection, but again it's quite easy, as you just have to click on "new" and select the proper Cosmos DB account
- Rather than creating it in Cosmos DB, we opt for the default parameter values and the "Create database and collection for me" option for this sample
This will be slightly different if you select a different language (for full reference on how to use Cosmos DB output bindings in Azure Functions see here)
Note that all we did was to aggregate data calculating average values and output to Cosmos DB, but it's easy to modify the Function code to manipulate the data on any other way before storing on Cosmos DB.
- To validate that our Function is being properly triggered and storing the IoT Hub messages, navigate to Cosmos DB and watch the messages being stored