Search code examples
azureazure-functionsazure-eventhub

Event Hub Trigger Azure Function Not Being Triggered When Using Non-Default Consumer Group


So I have a basic setup for listening on events coming in to Event Hub, using an Event Hub Trigger function, defined as follows:

[FunctionName("myfunction")]
public async Run([EventHubTrigger(myeventhub, Connection = "EventHubConnectionAppSetting", ConsumerGroup = %myconsumergroup%)], EventData eventHubMessage, ILogger logger)

Where the values for the connection and consumergroup parameters are in my local settings.

Running my function and sending events via Postman (properly authenticated), the function is not triggered.

The only way the function is every triggered is if I remove the ConsumerGroup from the parameters, which will cause it to point to the $Default consumer group for the event hub:

[FunctionName("myfunction")]
public async Run([EventHubTrigger(myeventhub, Connection = "EventHubConnectionAppSetting"], EventData eventHubMessage, ILogger logger)

My goal is to keep my custom consumer group and have the function trigger for events coming into that consumer group.

I will mention that I'm testing this out locally, and using local storage:

AzureWebJobsStorage="UseDevelopmentStorage=true"

But obviously the event hub in question is an actual created resource on Azure, with the relevant consumer group existing under it as well.


Solution

  • You can directly use the consumer group name in the function, like below:

    [FunctionName("myfunction")]
    public async Run([EventHubTrigger(myeventhub, Connection = "EventHubConnectionAppSetting", ConsumerGroup = "myconsumergroup_name")], EventData eventHubMessage, ILogger logger)
    

    Or if you want to use this mode %myconsumergroup%, you can refer to this steps as below:

    In local.settings.json(remember right click the file -> select properties -> set "Copy to Output Directory" as "copy if newer"), define the key-values like below:

    {
        "IsEncrypted": false,
      "Values": {
        "AzureWebJobsStorage": "UseDevelopmentStorage=true",
        "FUNCTIONS_WORKER_RUNTIME": "dotnet",
        "EventHubConnectionAppSetting": "xxxx",
        "EventHubConsumerGroup": "your_consumergroup_name"
      }
    }
    

    then in the function method, define the function like below:

        [FunctionName("Function1")]
        public static async Task Run([EventHubTrigger("eventhub_name", Connection = "EventHubConnectionAppSetting",ConsumerGroup = "%EventHubConsumerGroup%")] EventData[] events, ILogger log)
    

    Note: I use the latest packages for the function.