Store IoT Data with Cosmos DB
In this IT Pro Challenges virtual lab, learners are introduced to skills required to code, configure a function to move data from the Azure IoT hub to Cosmos Db. Exercises in this lab teach users how to process IoT data using function apps. The topics covered in this lab are critical for learners to be effective in system administrator roles in IT...
Already have an account? Sign In »

Cosmos DB is perfectly fit for IoT solutions because it can ingest device telemetry data at high speeds and can assist indexed queries back with low latency and high availability. Cosmos DB is a multi-model database with compatible API endpoints for various non-relational databases too.
In this hands-on lab, learners will learn how to store IoT data with Cosmos DB. To accomplish this, First, they will configure an Azure IoT hub. Next, they will configure a function app, and then add a function to move data from the hub to a Cosmos DB account that uses the SQL (DocumentDB) API. Finally, they will test the function. The other guided and advanced challenges in this series are “Use Azure Time Series Insights to Analyze IoT Data” and “Can You Store and Analyze IoT Data in Azure?” respectively.
Understand the Scenario
In this challenge, you are a system administrator for a company that is rolling out an Internet of Things (IoT) data solution. Your job is to configure a data path between an Azure IoT hub and a Cosmos DB database. You will use an Azure resource group named IoTlod12634736 that contains a preconfigured web app named lods12634736 in an App Service plan named iotChallenge-ASP, an Azure IoT hub named iot12634736, a Cosmos DB account named cdb12634736, and a storage account named lods12634736.
Configure the IoT hub
IoT Hub is a distributed service, hosted in the cloud, that works as a fundamental message hub for bi-directional interaction between the IoT application and the machines it controls. You can utilize Azure IoT Hub to develop IoT solutions with stable and reliable connections between numerous IoT devices and a cloud-hosted solution backend. You can attach virtually any device to IoT Hub. In this first section of the lab, you will learn how to configure the IoT hub. First, you will sign in to the Azure portal and select the Copy to clipboard icon to copy the text string to the clipboard and copy the Connection string - primary key for the iothubowner policy of the iot12634736 hub, and then record it in the IoT Connection text box. Next, you will copy the Event Hub-compatible endpoint for the Events endpoint of the iot12634736 hub, and then record it in the Event Hub Endpoint text box. Finally, you will add three IoT devices to the Azure IoT hub and configure Symmetric key authentication, and configure the option to Auto-generate keys.
Configure a function app
In this section of the lab, you will add a function app named processiot12634736 by using the IoTlod12634736 resource group, the .NET Core runtime stack, and the East US region. Next, you will configure the function app to use Windows and a Consumption (Serverless) hosting plan and then disable Application Insights. Several services can move data from an Azure IoT hub to data stores such as Cosmos DB. In addition to function apps, you can use logic apps, Stream Analytics, and custom code to move data between an Azure IoT hub and a Cosmos DB account. Finally, you will check and confirm that you configured a function app named processiot12634736 that is hosted in Windows and uses a consumption hosting plan and that you disabled Application Insights in the function app.
Add a function
After configuring a function app, in this section of the lab, learners will learn how to add a function. First, on the processiot12634736 page, learners will add a new function named xferiot to the processiot12634736 function app by using the IoT Hub (Event Hub) template and then configure a new IoT Hub Event Hub connection that uses the iot12634736 IoT hub. Finally, they will confirm that they created a function named xferiot that is triggered by events in the iot12634736 Azure IoT hub.
Define the function app
In this section of the lab, you will define the function app. First, you will configure an Azure Event Hubs trigger by using an event parameter and an Event Hub. Next, you will configure the function output by using the Azure Cosmos DB binding type, a database named iot, a collection named events, and a new Azure Cosmos DB account connection that uses the cdb12634736 database account, and then configure the output to create the Azure Cosmos DB and the collection. Finally, you will replace the existing code in the function with the new code and then expand Logs to view the active log, save the function, and verify that it compiles successfully.
Test the Azure function
After defining the function app, learners will now test the Azure function. To accomplish this, they will open a new browser tab and then go to https://lods12634736 and enter an IoT hub connection string and an event hub connection string and then select Emulate Devices. They will verify that IoT telemetry was generated and then select Verify Cosmos DB Data, and then observe the results to review the processiot12634736 function log. There should be several processed events in the function log. In this challenge, you will emulate IoT device interactions by using a web app. The device emulation in the web app is based on the same IoT code that would be used on an actual IoT device, and the emulation interacts with the Azure IoT Hub in the same way as an IoT device would. Finally, they will check and confirm that they loaded data to the cdb12634736 Cosmos DB account and that they viewed document data from Cosmos DB.
Lab Summary Conclusion
After completing the “Store IoT Data with Cosmos DB” virtual lab, you will have accomplished the following:
- Provisioned a Cosmos DB database and document collection.
- Provisioned a function app triggered by Azure IoT hub events.
- Added a Cosmos DB output to a function app function.
- Added code to a function app function to move data from the Azure IoT hub to the Cosmos DB document collection.
- Processed IoT data into a Cosmos DB document collection.