Skip to main content

Azure Function with IoT Hub Trigger

Summary

Configure Javascript Function Apps

Azure IoT Hub trigger for Azure Functions

Create a Function App with VS Code

Develop Azure Functions by using Visual Studio Code

Connection Strings

There are two critical strings available from the Azure Portal that are required to connect your Function App to an IoT Hub:

  1. Event Hub-compatible name
  2. Event Hub compatible endpoint

Both of these strings can be found under the IoT Hub > Hub settings > Built-in endpoints.

To configure your Function App with these strings you must:

  1. Edit function.json and specify the binding "eventHubName" with string (1) from above.
  2. Edit function.json and specify the binding "connection" with a variable name that you will define in the next step (for example, eventHubConnectionAppSetting)
  3. Define the connection variable:
    • If debugging locally, add a value to your local.settings.json with the key defined previously and the endpoint string (2) from above.
    • If hosted in Azure, add an AppSetting to the Function App matching the key defined previously and the endpoint string (2) from above.

Sample functions.json

{
"bindings": [
{
"type": "eventHubTrigger",
"name": "IoTHubMessages",
"direction": "in",
"eventHubName": "iothub-ehub-gqciothub-24046002-aaec6b17f1",
"connection": "eventHubConnectionAppSetting",
"cardinality": "many",
"consumerGroup": "$Default",
"dataType": "string"
}
]
}

Sample local.settings.json

{
"IsEncrypted": false,
"Values": {
"FUNCTIONS_WORKER_RUNTIME": "node",
"APPINSIGHTS_INSTRUMENTATIONKEY": "xx",
"APPLICATIONINSIGHTS_CONNECTION_STRING": "xx",
"FUNCTIONS_EXTENSION_VERSION": "~4",
"__WEBSITE_CONTENTAZUREFILECONNECTIONSTRING": "xx",
"WEBSITE_CONTENTSHARE": "xx",
"eventHubConnectionAppSetting": "Endpoint=sb://xx.servicebus.windows.net/;SharedAccessKeyName=xx;SharedAccessKey=xxxx"
}
}