Vivek Ranjan
Vivek Ranjan

Reputation: 1492

EventHubTrigger function app, pass event hub name and consumer group name dynamically to event hub attribute

We know the Connection string for event hub can be used from local.setting.json file. So for the same function app in different environments, I can add the event hub connection string setting in Application settings in the azure portal.

As the EventHubTrigger function app also expects event name and consumer group(optional) as attributes parameters, I was wondering how the event hub name and consumer group can be used from app settings?

  public static void EventHubTriggerFunc([EventHubTrigger("myeventhubname", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "myconsumergroupname")] EventData myEventHubMessage, DateTime enqueuedTimeUtc, Int64 sequenceNumber, string offset, ILogger log)
 {
   // Here EventHubConnectionAppSetting is specified in local.setting.json file
   //myeventhubname & myconsumergroupname are hard coded string
 }

local.settings.Json

   {
    "IsEncrypted": false,
   "Values": {
   "AzureWebJobsStorage": "UseDevelopmentStorage=true",
   "FUNCTIONS_WORKER_RUNTIME": "dotnet",
   "EventHubConnectionAppSetting": "Endpoint=.....",
   "EventHubConsumerGroup": "myconsumergroup"
  }
   }

Upvotes: 7

Views: 6486

Answers (2)

David
David

Reputation: 2262

Tried @Roman Kiss answer, and applied it to Python Azure Functions and it works.

In function.json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "type": "eventHubTrigger",
      "name": "events",
      "direction": "in",
      "eventHubName": "%EVENT_HUB_NAME%",
      "connection": "EVENT_HUB_CONN_STR",
      "cardinality": "many",
      "consumerGroup": "$Default",
      "dataType": "binary"
    }
  ]
}

Notice that connection string does not need %

In local.settings.json:

{
  ...
  "Values": {
    ...
    "EVENT_HUB_NAME": "<actual name of event hub>",
    "EVENT_HUB_CONN_STR": "Endpoint=sb://...;SharedAccessKeyName=...;SharedAccessKey=...",
    ...
  },
}

Upvotes: 1

Roman Kiss
Roman Kiss

Reputation: 8265

([EventHubTrigger("%myeventhubname%", Connection = "EventHubConnectionAppSetting", ConsumerGroup = "%myconsumergroupname%")]

Upvotes: 20

Related Questions