最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Azure Function (python) won't trigger from Azure IoT Hub event - Stack Overflow

programmeradmin5浏览0评论

I have an IoT Hub and an Azure Function App written in Python. I want the Azure function to trigger on messages received by the hub.

The IoT Hub is publicly accessible and I can successfully send messages to it. I have confirmed the JSON payloads are being received by using the Azure CLI.

I've deployed the Azure function using VS Code (Azure extension) and the function.json looks like this:

{
    "scriptFile": "__init__.py",
    "bindings": [
        {
            "type": "iotHubTrigger",
            "name": "event",
            "direction": "in",
            "eventHubName": "iothub-ehub-[redacted]-36471244-3e33dca112",
            "connection": "IoTHubConnectionString",
            "cardinality": "one",
            "consumerGroup": "mydevices"
        }
    ]
}

My python function (in __init__.py) starts with:

def main(event: func.IoTHubEvent) -> None:
...

In the Function App on the Azure Portal I have gone to Settings > Environmental Variables and set IoTHubConnectionString to Endpoint=sb://ihsuprodlnres017dednamespace.servicebus.windows/;SharedAccessKeyName=iothubowner;SharedAccessKey=[redacted];EntityPath=iothub-ehub-[redacted]-36471244-3e33dca112

I've been struggling for a while now and I can't get the function to trigger, the Function App logs show nothing so I have no idea what's wrong.

How can I troubleshoot this?

I have an IoT Hub and an Azure Function App written in Python. I want the Azure function to trigger on messages received by the hub.

The IoT Hub is publicly accessible and I can successfully send messages to it. I have confirmed the JSON payloads are being received by using the Azure CLI.

I've deployed the Azure function using VS Code (Azure extension) and the function.json looks like this:

{
    "scriptFile": "__init__.py",
    "bindings": [
        {
            "type": "iotHubTrigger",
            "name": "event",
            "direction": "in",
            "eventHubName": "iothub-ehub-[redacted]-36471244-3e33dca112",
            "connection": "IoTHubConnectionString",
            "cardinality": "one",
            "consumerGroup": "mydevices"
        }
    ]
}

My python function (in __init__.py) starts with:

def main(event: func.IoTHubEvent) -> None:
...

In the Function App on the Azure Portal I have gone to Settings > Environmental Variables and set IoTHubConnectionString to Endpoint=sb://ihsuprodlnres017dednamespace.servicebus.windows/;SharedAccessKeyName=iothubowner;SharedAccessKey=[redacted];EntityPath=iothub-ehub-[redacted]-36471244-3e33dca112

I've been struggling for a while now and I can't get the function to trigger, the Function App logs show nothing so I have no idea what's wrong.

How can I troubleshoot this?

Share Improve this question asked Mar 19 at 11:32 gazm2k5gazm2k5 5006 silver badges20 bronze badges 3
  • Can you elaborate? I can only find the Event Hub-compatible endpoint (which is what I have been using, starts with Endpoint=sb://) and the primary connection string on the IoT Hub (starts with HostName=...). Where do I find the Event Hub-compatible connection string? – gazm2k5 Commented Mar 19 at 12:13
  • Can you share your code and local.settings.json in the question? – Dasari Kamali Commented Mar 19 at 12:14
  • I have not been testing locally but created one: ``` { "IsEncrypted": false, "Values": { "AzureWebJobsStorage": "UseDevelopmentStorage=true", "FUNCTIONS_WORKER_RUNTIME": "python", "IoTHubConnectionString": [reacted, same as posted earlier] } } ``` – gazm2k5 Commented Mar 19 at 13:03
Add a comment  | 

1 Answer 1

Reset to default 0

I managed to get this working by changing my function.json to use the event hub structure rather than iot structure. function.json looks like this:

{
    "scriptFile": "__init__.py",
    "bindings": [
        {
            "type": "eventHubTrigger",
            "name": "event",
            "direction": "in",
            "eventHubName": "%IOT_HUB_EVENT_HUB_NAME%",
            "connection": "IOT_HUB_CONNECTION",
            "cardinality": "one",
            "consumerGroup": "edfdevices"
        }
    ]
}

I had to add environmental variables to the Azure Function App with the names IOT_HUB_CONNECTION and IOT_HUB_EVENT_HUB_NAME and keys for the endpoint (eg. iothub-ehub-[redacted]-36471244-3e33dca112 and Endpoint=sb://ihsuprodlnres017dednamespace.servicebus.windows/;SharedAccessKeyName=iothubowner;SharedAccessKey=[redacted];EntityPath=iothub-ehub-[redacted]-36471244-3e33dca112 respectively)

That seems to have done the trick.

发布评论

评论列表(0)

  1. 暂无评论