แก้ไข

แชร์ผ่าน


Use a function as an event handler for Event Grid events

An event handler is the place where the event is sent. The handler takes an action to process the event. Several Azure services are automatically configured to handle events and Azure Functions is one of them.

To use a function in Azure as a handler for events, follow one of these approaches:

  • Use Event Grid trigger. Specify Azure Function as the endpoint type. Then, specify the function app and the function that will handle events.
  • Use HTTP trigger. Specify Web Hook as the endpoint type. Then, specify the URL for the function that will handle events.

We recommend that you use the first approach (Event Grid trigger) as it has the following advantages over the second approach:

  • Event Grid automatically validates Event Grid triggers. With generic HTTP triggers, you must implement the validation response yourself.
  • Event Grid automatically adjusts the rate at which events are delivered to a function triggered by an Event Grid event based on the perceived rate at which the function can process events. This rate match feature averts delivery errors that stem from the inability of a function to process events as the function’s event processing rate can vary over time. To improve efficiency at high throughput, enable batching on the event subscription. For more information, see Enable batching.

Note

  • When you use an Event Grid trigger to add an event subscription using an Azure function, Event Grid fetches the access key for the target function using Event Grid service principal's credentials. The permissions are granted to Event Grid when you register the Event Grid resource provider in their Azure subscription.
  • If you protect your Azure function with an Microsoft Entra ID application, you'll have to take the generic webhook approach using the HTTP trigger. Use the Azure function endpoint as a webhook URL when adding the subscription.

Tutorials

Title Description
Quickstart: Handle events with function Sends a custom event to a function for processing.
Tutorial: stream big data into a data warehouse When Event Hubs creates a Capture file, Event Grid sends an event to a function app. The app retrieves the Capture file and migrates data to a data warehouse.
Tutorial: Azure Service Bus to Azure Event Grid integration examples Event Grid sends messages from Service Bus topic to a function app and a logic app.

REST example (for PUT)

{
	"properties": 
	{
		"destination": 
		{
			"endpointType": "AzureFunction",
			"properties": 
			{
				"resourceId": "/subscriptions/<AZURE SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Web/sites/<FUNCTION APP NAME>/functions/<FUNCTION NAME>",
				"maxEventsPerBatch": 10,
				"preferredBatchSizeInKilobytes": 64
			}
		},
		"eventDeliverySchema": "EventGridSchema"
	}
}

Enable batching

For a higher throughput, enable batching on the subscription. If you're using the Azure portal, you can set maximum events per batch and the preferred batch size in kilo bytes at the time of creating a subscription or after the creation.

You can configure batch settings using the Azure portal, PowerShell, CLI, or Resource Manager template.

Azure portal

At the time creating a subscription in the UI, on the Create Event Subscription page, switch to the Advanced Features tab, and set values for Max events per batch and Preferred batch size in kilobytes.

Enable batching at the time of creating a subscription

You can update these values for an existing subscription on the Features tab of the Event Grid Topic page.

Enable batching after creation

Azure Resource Manager template

You can set maxEventsPerBatch and preferredBatchSizeInKilobytes in an Azure Resource Manager template. For more information, see Microsoft.EventGrid eventSubscriptions template reference.

Azure CLI

You can use the az eventgrid event-subscription create command to configure batch-related settings using the following parameters: --max-events-per-batch or --preferred-batch-size-in-kilobytes.

Azure PowerShell

You can use the New-AzEventGridSubscription or Update-AzEventGridSubscription cmdlet to configure batch-related settings using the following parameters: -MaxEventsPerBatch or -PreferredBatchSizeInKiloBytes.

Note

When you use Event Grid Trigger, the Event Grid service fetches the client secret for the target Azure function, and uses it to deliver events to the Azure function. If you protect your Azure function with a Microsoft Entra application, you have to take the generic web hook approach and use the HTTP Trigger.

Next steps

See the Event handlers article for a list of supported event handlers.