3

I just want to ask if it is possible to execute an Azure Function (Event Hub trigger) purely on my local machine without depending on any Azure Event Hubs resource? I've been following Microsoft's process in developing Azure Functions locally (Link) but it seems that I need to fill in Event Hub connection string and name.

public static async Task Run([EventHubTrigger("samples-workitems", Connection = "eventhub-connectionstring")] EventData[] events, ILogger log)

Is there any way for this to be possible?

TheNinong
  • 48
  • 1
  • 4
  • Check the anwser of this question https://stackoverflow.com/questions/52145719/how-to-debug-eventhubtrigger-locally. You have to create and eventhub on Azure to be able to test your function. – Nacho Martínez-Aedo Sep 11 '20 at 12:48
  • I'm just hoping I could also do pure local development like HTTP trigger Azure Functions (using Postman). If it is really required I think I'll be needing a subscription for this. Thanks! – TheNinong Sep 12 '20 at 03:23

2 Answers2

9

Each binding has an HTTP endpoint for local testing and debugging.

  • https://localhost:7071/admin/functions/{FUNCNAME}

That's available for QueueTrigger, ServiceBusTrigger, TimerTrigger, EventHubTrigger at least.

Send a POST request with the expected data as JSON.

{ "input": "YOUR JSON SERIALIZED AND ESCAPED DATA" }

For triggers that need data put the data as serialized string into "input". See EventHubTrigger example further below.

TimerTrigger

For TimerTrigger use this:

{ "input": null }

EventGridTrigger

To execute this on some triggers it's bit tricky. Here it is for EventGridTrigger:

  • https://localhost:7071/runtime/webhooks/EventGrid/functionName={FUNCNAME}

Send a POST request to execute. See here for details. The object must be an array.

EventHubTrigger

The EventHubTrigger receives data like the other triggers, as single JSON object. The structure follows the EventData class, but the only required field is "SystemProperties". There seems no serializer specific settings, property names do not change case etc.

Post this as body;

{
    "input": "{\"SystemProperties\":{},\"SomeId\":\"123\",\"Status\":\"Available\"}"
}

The event hub's body is the escaped and serialized value of "input".

Note that the same applies for the IoT Hub

Meta Data

For ALL triggers you can get meta data with a GET request. For an EventHubTrigger this could look like this:

{
    "name": "StateChange",
    "script_root_path_href": "http://localhost:7071/admin/vfs/StateChange/",
    "script_href": "http://localhost:7071/admin/vfs/bin/MyApp.Notifications.Functions.dll",
    "config_href": "http://localhost:7071/admin/vfs/StateChange/function.json",
    "test_data_href": null,
    "href": "http://localhost:7071/admin/functions/StateChange",
    "invoke_url_template": null,
    "language": "DotNetAssembly",
    "config": {
        "generatedBy": "Microsoft.NET.Sdk.Functions-3.0.11",
        "configurationSource": "attributes",
        "bindings": [
            {
                "type": "eventHubTrigger",
                "consumerGroup": "regular",
                "connection": "EventHub_Hub_Status",
                "eventHubName": "%EventHub_Status%",
                "name": "statusUpdateMessage"
            }
        ],
        "disabled": false,
        "scriptFile": "../bin/MyApp.Notifications.Functions.dll",
        "entryPoint": "MyApp.Notifications.Functions.StateChange.Run"
    },
    "files": null,
    "test_data": null,
    "isDisabled": false,
    "isDirect": true,
    "isProxy": false
}

And, of course you can use all the paths to retrieve the data, including the binaries. Very handy to write sophisticated integration tests.

Joerg Krause
  • 1,759
  • 1
  • 16
  • 27
  • 1
    I pored over a billion Microsoft articles looking for this info and found nothing. And there you are explaining it nicely in a StackOverflow answer. Wherever you dug this info out — thank you for sharing it! – kaqqao May 31 '22 at 17:42
  • What do you put into the `event_hub_name` and `connection` variables of the Trigger ? I would like to trigger locally, but am not sure how to configure the `local.settings.json`. I am working with the new version v2 model of Azure Functions. Also I posted a question for this on bounty [here](https://stackoverflow.com/questions/76183278/how-to-locally-develop-eventhub-triggered-functions-in-python-programming-model). – DataBach May 08 '23 at 11:42
1

No, It is not possiable.

This is because there is no official simulation tool.

For httptrigger, you can use postman or just use some code to hit the endpoint.

For trigger about azure storage, you can use local azure storage explorer.

But something like eventhub, service bus and so on can not be triggered without creating a azure resource.

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • No.. you are confused. There is no simulator in local machine for eventhub, but it can be called by postman using port localhost:7071... so, you can simulate the message from eventhub before starting it. – Sérgio Thiago Mendonça Jul 30 '21 at 13:36