0
votes

I am new to Azure and currently looking into the quickstart of the IoT Hub here.

Sending telemetry from a device to an IoT hub and reading it with a back-end application, works so far.

I installed the vs code extension and completed the HTTP trigger example for Azure functions here successfully.

In the next step I tried to configure an IoT Hub (Event Hub) Azure function. This results in the following error when testing locally:

Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.funcexample'. Microsoft.WindowsAzure.Storage: No valid combination of account information found.

I added the Event Hub-compatible endpoint string of the IoT Hub in local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "Endpoint=sb://.../;SharedAccessKeyName=iothubowner;SharedAccessKey=...;EntityPath=...",
    "FUNCTIONS_WORKER_RUNTIME": "node"
  }
}

I noticed that the SharedAccessKey via az iot hub policy show --name service --query primaryKey --hub-name {your IoT Hub name} does not match the key in the Azure portal. Both keys lead to the above mentioned warning. Edit1: SharedAccessKeys are different for service and iothubowner.

Edit2: I think my main problem is related to the connection string. What connection string is required and how is it formated. And, in which file/setting should the connection string be placed?

Here are other related files (mostly untouched). function.json

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "IoTHubMessages",
      "direction": "in",
      "eventHubName": "samples-workitems",
      "connection": "AzureWebJobsStorage",
      "cardinality": "many",
      "consumerGroup": "$Default"
    }
  ]
}

index.js

module.exports = function (context, IoTHubMessages) {
    context.log(`JavaScript eventhub trigger function called for message array: ${IoTHubMessages}`);
    
    IoTHubMessages.forEach(message => {
        context.log(`Processed message: ${message}`);
    });

    context.done();
};

host.json:

{
  "version": "2.0",
  "logging": {
    "applicationInsights": {
      "samplingSettings": {
        "isEnabled": true,
        "excludedTypes": "Request"
      }
    }
  },
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[2.*, 3.0.0)"
  }
}

proxies.json:

{
  "$schema": "http://json.schemastore.org/proxies",
  "proxies": {}
}

package.json:

{
  "name": "funcexample",
  "version": "1.0.0",
  "description": "",
  "scripts": {
    "start": "func start",
    "test": "echo \"No tests yet...\""
  },
  "dependencies": {},
  "devDependencies": {}
}

What am I missing?

1
Try adding @azure/event-hubs package, and for connection-string related info refer this (link) [docs.microsoft.com/en-us/azure/iot-hub/…Hemant Halwai

1 Answers

0
votes

I had some general misunderstandings regarding Storage accounts, the actual IoT Hub and their connection strings in the context of Azure Functions in production and development (testing them locally).

In order to test locally, I installed the local storage emulator Azurite via npm install -g azurite and went with the following setting for the local.settings.json

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "AzureWebJobsDashboard": "UseDevelopmentStorage=true",
    "ConnectionString": "Endpoint=sb://.../;SharedAccessKeyName=...;SharedAccessKey=...;EntityPath=...",
    "FUNCTIONS_WORKER_RUNTIME": "node"
  }
}

and function.json.

{
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "IoTHubMessages",
      "direction": "in",
      "eventHubName": "samples-workitems",
      "connection": "ConnectionString",
      "cardinality": "many",
      "consumerGroup": "$Default"
    }
  ]
}