serverless-azure-functions icon indicating copy to clipboard operation
serverless-azure-functions copied to clipboard

eventHub trigger not working on azure portal

Open leopuleo opened this issue 5 years ago • 7 comments

This is a Bug Report

Description

  • What went wrong? I successfully deploy my serverless functions to Azure, setting eventHub as binding. While I visit the function page on azure an error appears and the function does not work:
Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.myfunc'. Microsoft.Azure.WebJobs.EventHubs: Value cannot be null. Parameter name: receiverConnectionString.
  • What did you expect should have happened? The function to work correctly
  • What was the config you used? serverless.yml
myfunc:
        handler: src/entrypoint.myfunc
        events:
            - eventHub:
              x-azure-settings:
                  name: items # Specifies which name it's available on `context`
                  eventHubName: myfunc-events # Specifies the Name of the Event Hub
                  consumerGroup: myfuncchannel # Specifies the consumerGroup to listen with
                  connection: ${opt:eh} # App Setting/environment variable which contains Event Hubs Namespace Connection String
  • What stacktrace or error message from your provider did you see? function.json
{
  "disabled": false,
  "bindings": [
    {
      "type": "eventHubTrigger",
      "direction": "in",
      "name": "items",
      "eventHubName": "myfunc-events",
      "consumerGroup": "myfuncchannel",
      "connection": "Endpoint={myurl}"
    }
  ],
  "entryPoint": "myfunc",
  "scriptFile": "../src/entrypoint.js"
}

error

Error: Microsoft.Azure.WebJobs.Host: Error indexing method 'Functions.myfunc'. Microsoft.Azure.WebJobs.EventHubs: Value cannot be null. Parameter name: receiverConnectionString.

Similar or dependent issues:

  • #352

Additional Data

  • Serverless Framework Version you're using: 1.54.0
  • Serverless CLI Version you're using: 1.2.3
  • Serverless Azure Plugin Version you're using: 1.0.2
  • Operating System: darwin

leopuleo avatar Oct 11 '19 09:10 leopuleo

@leopuleo

  1. can you share your src/entrypoint.js?
  2. When you say "visit the function page on azure", do you mean accessing the function url?
  3. The error is saying that you did not provide the connection string for your EventHub service.
    • Base on your serverless.yml snippet, you're trying to pass in the connection string from the command line.
    • How are you deploying the app? Can you provide the full command used (masking any secrets)?

mydiemho avatar Oct 16 '19 20:10 mydiemho

Hi @mydiemho,

  1. can you share your src/entrypoint.js?

/src/entrypoint.js

import { myfuncChannel } from "./channels/myfunc";

export function myfunc(context, items) {
    return myfuncChannel(context, items);
}

/channels/myfunc.js

export const myfuncChannel = async (context, items) => {
    try {
        const messages = Array.isArray(items) ? items : [items];
        ...
        context.done();
    } catch (error) {
        context.done(error.message, { status: 500 });
    }
};
  1. When you say "visit the function page on azure", do you mean accessing the function url?

No, the actual page on Azure Portal (see screenshot attached).

Screenshot 2019-10-17 at 09 25 25

Also, I noticed inside the "Integrate" tab the "EventHub connection" input is empty

Screenshot 2019-10-17 at 09 25 51

  1. The error is saying that you did not provide the connection string for your EventHub service.

serverless.yml

service: channels

provider:
    name: azure
    runtime: nodejs10.x
    region: ${opt:region, 'westeurope'}
    stage: ${opt:stage, 'dev'}
    prefix: ${opt:prefix, 'ncs'}
    environment:
        AZURE_EH_CONNECTION_STRING: ${opt:eh}
        BRANCH_NAME: ${opt:stage}

plugins:
    - serverless-azure-functions
    - serverless-webpack

custom:
    webpack:
        webpackConfig: "webpack.config.js" # Name of webpack configuration file
        includeModules: false # Node modules configuration for packaging
        packager: "npm" # Packager that will be used to package your external modules
        excludeFiles: "tests/**/**/*.js" # Provide a glob for files to ignore

functions:
    myfunc:
        handler: src/entrypoint.myfunc
        events:
            - eventHub:
              x-azure-settings:
                  name: items
                  eventHubName: myfunc-events
                  consumerGroup: myfuncchannel 
                  connection: ${opt:eh} 

serverless cmd

serverless deploy --region westeurope --stage dev --eh "Endpoint=sb://{myEHconnectionString}" --verbose

I also tried to set the full connection string inside the `serverless.yml, with no success. Thank you!

leopuleo avatar Oct 17 '19 07:10 leopuleo

  1. Where did you set the connection string inside the serverless.yml file?

    • Replace the connection: ${opt:eh} with connection: AZURE_EH_CONNECTION_STRING
    • When you set AZURE_EH_CONNECTION_STRING in the environment section, this create an app setting with that key and the assigned value (whatever you gave ${opt:eh}). Thus, you want to reference that app setting in your bindings.
  2. This is eventhub trigger event, which means items is only available when it's trigger by an event being added to the eventhub. When you "trigger" it from the portal, it's doing an http trigger, and items is null at this point.

You'll have to send the events separately https://docs.microsoft.com/en-us/azure/event-hubs/event-hubs-node-get-started-send#send-events

mydiemho avatar Oct 17 '19 22:10 mydiemho

Where did you set the connection string inside the serverless.yml file?

Inside the deploy command:

serverless deploy --region westeurope --stage dev --eh "Endpoint=sb://{myEHconnectionString}" --verbose

Replace the connection: ${opt:eh} with connection: AZURE_EH_CONNECTION_STRING When you set AZURE_EH_CONNECTION_STRING in the environment section, this create an app setting with that key and the assigned value (whatever you gave ${opt:eh}). Thus, you want to reference that app setting in your bindings.

Done! The script saves a new AZURE_EH_CONNECTION_STRING environment variable. Screenshot 2019-10-18 at 09 45 00

But the situation does not change:

  1. I still have the red banner with error.
  2. The "Event Hub Connection" string is still empty.

This is eventhub trigger event, which means items is only available when it's trigger by an event being added to the eventhub. When you "trigger" it from the portal, it's doing an http trigger, and items is null at this point.

I got the error while a visit the function page, I'm not triggering the function via the azure portal. Sending event to the connected EH, it's not consumed by the receiver function

leopuleo avatar Oct 18 '19 07:10 leopuleo

@leopuleo did you solve this problem? I am having the same.

williampenna avatar Sep 30 '21 19:09 williampenna

Unfortunately, I don't know...I moved away from serverless framework. I'm deploying my functions using azure-cli

leopuleo avatar Oct 01 '21 07:10 leopuleo

Yesterday, I got to do. Follow an example:

example: handler: src/app/example.handler events: - eventHub: true x-azure-settings: name: eventHubMessages eventHubName: example consumerGroup: $Default connection: EVENT_HUB_CONNECTION_STRING cardinality: many

williampenna avatar Oct 01 '21 08:10 williampenna