How to use Managed Identity to connect to IoT Hub from Azure function

Satyam Chauhan 607 Reputation points
2023-07-11T05:29:44.9666667+00:00

We have a IoTHub trigger function, which will be triggered when data will be received in IoT hub and it will push the data to Cosmos DB.

How we can use managed identity instead of connection string to connect to IoT Hub.

A Connection string must a constant variable, we are not able to add connection string at runtime. Please let me know can we resolve this?

[FunctionName("FunctionName")]         
public async Task RunAsync([IoTHubTrigger("messages/events", Connection = "IoTHubConnectionString")] EventData message, [CosmosDB( databaseName: "%DatabaseName%",                 containerName: "%ContainerName%", Connection =  "DBConnection")]                 IAsyncCollector<object> RawDataContainer, ILogger log)
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,936 questions
Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,274 questions
0 comments No comments
{count} votes

Accepted answer
  1. AirGordon 7,150 Reputation points
    2023-07-11T07:14:22.15+00:00

    Many of the native bindings in Azure Functions do not yet support Managed Identity.

    If you require more flexibility, such as altering the connection string to target different azure endpoints, or using Managed Identity - then you shouldn't use Native bindings, instead leverage the appropriate SDK in your code.

    https://github.com/Azure/azure-iot-sdk-csharp

    3 people found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.