Search code examples
pythonazure-functionsazure-eventhub

Running default eventhub function returns "unexpected status code: 400"


Running the basic azure function template for "Azure Event Hub trigger":

import logging

import azure.functions as func


def main(event: func.EventHubEvent):
    logging.info('Python EventHub trigger processed an event: %s',
                 event.get_body().decode('utf-8'))

I'm getting unexpected status code: 400 when executing the function locally in VSC. I've got local functions running: enter image description here And confirmed the EH instance name is correct in my function.json

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "event",
      "direction": "in",
      "eventHubName": "homeeventhub",
      "connection": "",
      "cardinality": "many",
      "consumerGroup": "$Default"
    }
  ]
}

The full log error:

[2022-11-02T23:14:53.401Z] Executing HTTP request: {
[2022-11-02T23:14:53.403Z]   requestId: "0000-0000-0000-0000-0000",
[2022-11-02T23:14:53.404Z]   method: "GET",
[2022-11-02T23:14:53.406Z]   userAgent: "ms-rest-js/2.6.0 Node/v16.14.2 OS/(x64-Windows_NT-10.0.22000) vscode-azurefunctions/1.8.3",
[2022-11-02T23:14:53.407Z]   uri: "/"
[2022-11-02T23:14:53.408Z] }
[2022-11-02T23:14:53.511Z] Executed HTTP request: {
[2022-11-02T23:14:53.512Z] Executing HTTP request: {
[2022-11-02T23:14:53.514Z]   requestId: "0000-0000-0000-0000-0000",
[2022-11-02T23:14:53.512Z]   requestId: "0000-0000-0000-0000-0000",
[2022-11-02T23:14:53.515Z]   method: "POST",
[2022-11-02T23:14:53.516Z]   identities: "",
[2022-11-02T23:14:53.517Z]   userAgent: "ms-rest-js/2.6.0 Node/v16.14.2 OS/(x64-Windows_NT-10.0.22000) vscode-azurefunctions/1.8.3",
[2022-11-02T23:14:53.518Z]   status: "200",
[2022-11-02T23:14:53.518Z]   uri: "/admin/functions/ehtest2"
[2022-11-02T23:14:53.519Z]   duration: "108"
[2022-11-02T23:14:53.520Z] }
[2022-11-02T23:14:53.520Z] }
[2022-11-02T23:14:53.599Z] Executing StatusCodeResult, setting HTTP status code 400
[2022-11-02T23:14:53.603Z] Executed HTTP request: {
[2022-11-02T23:14:53.604Z]   requestId: "0000-0000-0000-0000-0000",
[2022-11-02T23:14:53.605Z]   identities: "(WebJobsAuthLevel:Admin, WebJobsAuthLevel:Admin)",
[2022-11-02T23:14:53.606Z]   status: "400",
[2022-11-02T23:14:53.606Z]   duration: "91"
[2022-11-02T23:14:53.607Z] }

I'm sure it's just some kind of local configuration but I don't know what else I can troubleshoot. I have an additionall HTTP function that is running fine locally. Is there something else in the config that needs to be set up?


Solution

  • Ensure you have all the configuration settings configured properly such as:

    function.json:

    {
    "scriptFile": "__init__.py",
    "bindings": [
    {
    "type": "eventHubTrigger",
    "name": "event",
    "direction": "in",
    "eventHubName": "kdemoeventhub",
    "connection": "krisheventhubns_RootManageSharedAccessKey_EVENTHUB",
    "cardinality": "many",
    "consumerGroup": "$Default"
        }
      ]
    }
    

    krisheventhubns_RootManageSharedAccessKey_EVENTHUB is the Azure Event Hub Namespace Connection string available in Shared access policies of the Azure portal Event Hub Namespace.

    local.settings.json:

    {
    
    "IsEncrypted": false,
    "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "krisheventhubns_RootManageSharedAccessKey_EVENTHUB": "<Your-Event-Hub-NS-ConnStr"
    }
    }
    

    enter image description here

    • To run the Azure Functions Python Event Hub Trigger, you need to pass/send some events so that you can observer the default function is capturing and logging the events.
    • Create Sender.py file and get the send events code from this MS Doc of send events to Azure Event Hub using Python.
    • Then run the Azure Functions Project using the terminal code func host start or Run > Run without debugging. Once the host is initialized, then run the sender.py file with the cmdlet python send.py and then observe the events in Azure portal Event Hub: enter image description here