Hello Amie, greetings! Welcome to Microsoft Q&A forum and thank you for posting this question. We would be glad to provide you the steps needed for this. Please note that the following process uses an Event Hub instance end point and not the default built in route for Azure IoT Hub. Please make sure you have an Event Hub end point instance created on the Azure portal and the IoT Hub data is successfully routed to the Event Hub instance to successfully test this process. You can create an Event Hub end point using the tutorial Create an event hub using Azure portal
Follow the steps to develop, test and deploy the Azure Event Hub Trigger function in Visual Studio.
Step 1 Create a new project in Visual Studio and select the Template as Azure Functions and follow the prompts.
Make sure to choose the Function type as Event Hub trigger and provide a Connection string setting name as shown below.
You will see that an Azure function with Event Hub Trigger gets generated.
Step 2 Provide the connection string parameter in local.settings.json file and change method parameter as following.
Change the Event Hub Trigger function parameter to provide an Event Hub name. Your method signature should look like this public static async Task Run([EventHubTrigger(eventHubName: "<eventhubinstancename", Connection = "ConnectionString")] EventData[] events, ILogger log)
Navigate to the Event Hub instance on the Azure portal to copy the primary connection string. Refer the below image for details. If you do not have an access policy, you can create on to generate the keys.
Open local.settings.json file and provide the connection string primary key we have copied from the above step as follows
Note The property name has to be the same as what we have defined for connection string setting name in step 1 during Azure Function creation. You would need to remove the EntityPath=<Event Hub Instance Name> at the end of the connection string as we already provided that in the function name.
Step 3 Debug the code with a break point
Make sure you have an application pushing data to the IoT Hub and routed to the Event Hub end point. You can use the quick start guide and an SDK to Send Telemetry data to the IoT Hub for testing purposes. I have a Python SDK that pushes values to the IoT Hub which is running on VS Code. Here is the telemetry it generates.
On the Event Hub Trigger function, I have put a break point where I am logging events. When I start the debugging of the application, notice the break point gets hit for every event delivered to the Event Hub.
Notice the events captured by the function.
Step 4 Deploy the function to the Azure cloud
You can use the steps provided in the Publish the project to Azure section to guide you with deployment.
Note Once the function is deployed you would have to create an application setting parameter to provide Connection string. Please refer the below image to help you add this setting.
I still have my SDK running on the background pushing events to the Event Hub. I could see the deployed Azure function receive events and gets triggered. Please refer the below image.
Here are some additional resources that will help you to provide more details.
- Understand Azure IoT Hub routing
- Understanding different process models available for Azure functions
Hope this helps you get started. Please let us know if you face any issues with any of the steps.
- Kindly mark the answer as useful if the response is helpful so that it would benefit other community members facing the same issue.
- Original posters help the community find answers faster by identifying the correct answer. Here is how
- I highly appreciate your contribution to the community.