How to send data from Azure IoT hub to Cosmos db

Debashis Jena 71 Reputation points
2023-03-24T11:35:37.93+00:00

We have a requirement to send data from IoT hub to Cosmos db. We are currently using Stream analytics jobs to solve the purpose. But we want to put all the Azure services inside private vnet and we are unable to add stream analytics jobs in vnet.

What are the other options available instead of stream analytics jobs to send data from IoT hub to cosmos db?

We are also trying with Event hub, but we are not able to add cosmos db as endpoint in event hub.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,115 questions
{count} votes

Accepted answer
  1. Sander van de Velde 28,311 Reputation points MVP
    2023-03-27T11:09:59.7533333+00:00

    Hello @Debashis Jena,

    because the CosmosDB route for IoT Hub is still in preview, you are looking for a production-ready alternative.

    You need to put some logic between the IoT Hub and CosmosDB so the obvious alternative is Azure Functions.

    Azure Functions can be seen as 'code logic with an endpoint'.

    The logic is triggered by some external event, here it would be a telemetry message coming from an IoT Hub.

    Check out the IoT Hub trigger for Azure Functions.

    Once the telemetry is read in code, you need to persist it in CosmosDB.

    You could either program against CosmosDB using the available SDKs or use the Azure Function Output binding.

    The output binding is more convenient, I suppose.

    Notice it is recommended to use a separate consumer group for your Azure Function trigger (see the comments in the other question).

    I recommend starting with the Azure Function Consumption plan and start calculating when it's more cost-effective to switch over to a 'self-hosted' service plan. Especially with high volumes or heavy computing, this is recommended. You are also more in control regarding response times when the service plan is not based on the consumption plan.


    If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.


1 additional answer

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 13,456 Reputation points
    2023-03-24T15:53:37.0666667+00:00

    Hi @Debashis Jena Greetings! Thank you for posting the question on Microsoft Q&A forum. We are glad to let you know that Azure IoT Hub now supports Cosmos DB as one of the supported built-in end points to route the data. You do not have to setup a custom application or use other tools like Azure Functions or Azure Stream Analytics to collect the streaming data and then write to Azure Cosmos DB. You can find more information on this through the following link Azure Cosmos DB as a routing endpoint. Kindly note that the feature is still in Preview.

    To enable this routing, you would need to create a synthetic partition key on the Cosmos DB at the container level and refer the same when creating a custom route in IoT Hub. Please refer the following resource that explains the procedure to Create a synthetic partition key

    Once you have the key created, you can add the Cosmos DB as a custom end point to IoT Hub using the steps provided in the article Set up an Azure Cosmos DB custom endpoint directly from IoT Hub After creating the end point, you can create route with appropriate routing query to enable data routing to the Cosmo DB.

    Please let us know if you have any further questions or clarifications needed on this in the comments and we would be glad to help you further.


    If this answers your query, do click Accept Answer and Yes for this answer as helpful. Doing so would benefit other community members who are facing the same issue to find the correct solution.

    1 person found this answer helpful.