You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
title: Azure Event Grid bindings for Azure Functions
3
3
description: Understand how to handle Event Grid events in Azure Functions.
4
4
author: craigshoemaker
5
5
6
6
ms.topic: reference
7
-
ms.date: 09/04/2018
7
+
ms.date: 02/03/2020
8
8
ms.author: cshoe
9
+
ms.custom: fasttrack-edit
9
10
---
10
11
11
-
# Event Grid trigger for Azure Functions
12
+
# Azure Event Grid bindings for Azure Functions
12
13
13
14
This article explains how to handle [Event Grid](../event-grid/overview.md) events in Azure Functions. For details on how to handle Event Grid messages in an HTTP end point, read [Receive events to an HTTP endpoint](../event-grid/receive-events.md).
14
15
15
16
Event Grid is an Azure service that sends HTTP requests to notify you about events that happen in *publishers*. A publisher is the service or resource that originates the event. For example, an Azure blob storage account is a publisher, and [a blob upload or deletion is an event](../storage/blobs/storage-blob-event-overview.md). Some [Azure services have built-in support for publishing events to Event Grid](../event-grid/overview.md#event-sources).
16
17
17
-
Event *handlers* receive and process events. Azure Functions is one of several [Azure services that have built-in support for handling Event Grid events](../event-grid/overview.md#event-handlers). In this article, you learn how to use an Event Grid trigger to invoke a function when an event is received from Event Grid.
18
+
Event *handlers* receive and process events. Azure Functions is one of several [Azure services that have built-in support for handling Event Grid events](../event-grid/overview.md#event-handlers). In this article, you learn how to use an Event Grid trigger to invoke a function when an event is received from Event Grid, and to use the output binding to send events to an [Event Grid custom topic](../event-grid/post-to-custom-topic.md).
18
19
19
20
If you prefer, you can use an HTTP trigger to handle Event Grid Events; see [Receive events to an HTTP endpoint](../event-grid/receive-events.md). Currently, you can't use an Event Grid trigger for an Azure Functions app when the event is delivered in the [CloudEvents schema](../event-grid/cloudevents-schema.md#azure-functions). Instead, use an HTTP trigger.
The Event Grid trigger is provided in the [Microsoft.Azure.WebJobs.Extensions.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventGrid) NuGet package, version 2.x. Source code for the package is in the [azure-functions-eventgrid-extension](https://github.com/Azure/azure-functions-eventgrid-extension/tree/v2.x) GitHub repository.
26
+
The Event Grid bindings are provided in the [Microsoft.Azure.WebJobs.Extensions.EventGrid](https://www.nuget.org/packages/Microsoft.Azure.WebJobs.Extensions.EventGrid) NuGet package, version 2.x. Source code for the package is in the [azure-functions-eventgrid-extension](https://github.com/Azure/azure-functions-eventgrid-extension/tree/v2.x) GitHub repository.
For more information, see Packages, [Attributes](#attributes), [Configuration](#configuration), and [Usage](#usage).
131
+
For more information, see Packages, [Attributes](#trigger---attributes), [Configuration](#trigger---configuration), and [Usage](#trigger---usage).
127
132
128
133
### Version 1.x
129
134
@@ -280,7 +285,7 @@ In the [Java functions runtime library](/java/api/overview/azure/functions/runti
280
285
281
286
---
282
287
283
-
## Attributes
288
+
## Trigger - attributes
284
289
285
290
# [C#](#tab/csharp)
286
291
@@ -312,11 +317,11 @@ Attributes are not supported by Python.
312
317
313
318
# [Java](#tab/java)
314
319
315
-
The [EventGridTrigger](https://github.com/Azure/azure-functions-java-library/blob/master/src/main/java/com/microsoft/azure/functions/annotation/EventGridTrigger.java) annotation allows you to declaratively configure an Event Grid binding by providing configuration values. See the [example](#example) and [configuration](#configuration) sections for more detail.
320
+
The [EventGridTrigger](https://github.com/Azure/azure-functions-java-library/blob/master/src/main/java/com/microsoft/azure/functions/annotation/EventGridTrigger.java) annotation allows you to declaratively configure an Event Grid binding by providing configuration values. See the [example](#trigger---example) and [configuration](#trigger---configuration) sections for more detail.
316
321
317
322
---
318
323
319
-
## Configuration
324
+
## Trigger - configuration
320
325
321
326
The following table explains the binding configuration properties that you set in the *function.json* file. There are no constructor parameters or properties to set in the `EventGridTrigger` attribute.
322
327
@@ -326,7 +331,7 @@ The following table explains the binding configuration properties that you set i
326
331
|**direction**| Required - must be set to `in`. |
327
332
|**name**| Required - the variable name used in function code for the parameter that receives the event data. |
328
333
329
-
## Usage
334
+
## Trigger - usage
330
335
331
336
# [C#](#tab/csharp)
332
337
@@ -366,11 +371,11 @@ The Event Grid instance is available via the parameter configured in the *functi
366
371
367
372
# [Java](#tab/java)
368
373
369
-
The Event Grid event instance is available via the parameter associated to the `EventGridTrigger` attribute, typed as an `EventSchema`. See the [example](#example) for more detail.
374
+
The Event Grid event instance is available via the parameter associated to the `EventGridTrigger` attribute, typed as an `EventSchema`. See the [example](#trigger---example) for more detail.
370
375
371
376
---
372
377
373
-
## Event schema
378
+
## Trigger - event schema
374
379
375
380
Data for an Event Grid event is received as a JSON object in the body of an HTTP request. The JSON looks similar to the following example:
376
381
@@ -408,7 +413,7 @@ For explanations of the common and event-specific properties, see [Event propert
408
413
409
414
The `EventGridEvent` type defines only the top-level properties; the `Data` property is a `JObject`.
410
415
411
-
## Create a subscription
416
+
## Trigger - create a subscription
412
417
413
418
To start receiving Event Grid HTTP requests, create an Event Grid subscription that specifies the endpoint URL that invokes the function.
414
419
@@ -508,7 +513,7 @@ For more information, see [Authorization keys](functions-bindings-http-webhook-t
508
513
509
514
Alternatively, you can send an HTTP PUT to specify the key value yourself.
510
515
511
-
## Local testing with viewer web app
516
+
## Trigger - local testing with viewer web app
512
517
513
518
To test an Event Grid trigger locally, you have to get Event Grid HTTP requests delivered from their origin in the cloud to your local machine. One way to do that is by capturing requests online and manually resending them on your local machine:
514
519
@@ -582,6 +587,241 @@ The Event Grid trigger function executes and shows logs similar to the following
582
587
583
588

584
589
590
+
## Output
591
+
592
+
Use the Event Grid output binding to write events to a custom topic. You must have a valid [access key for the custom topic](../event-grid/security-authentication.md#custom-topic-publishing).
593
+
594
+
> [!NOTE]
595
+
> The Event Grid output binding does not support shared access signatures (SAS tokens). You must use the topic's access key.
596
+
597
+
Make sure the required package references are in place before you try to implement an output binding.
598
+
599
+
> [!IMPORTANT]
600
+
> The Event Grid output binding is only available for Functions 2.x and higher.
601
+
602
+
# [C#](#tab/csharp)
603
+
604
+
The following example shows a [C# function](functions-dotnet-class-library.md) that writes a message to an Event Grid custom topic, using the method return value as the output:
The following example shows the Event Grid output binding data in the *function.json* file.
678
+
679
+
```json
680
+
{
681
+
"type": "eventGrid",
682
+
"name": "outputEvent",
683
+
"topicEndpointUri": "MyEventGridTopicUriSetting",
684
+
"topicKeySetting": "MyEventGridTopicKeySetting",
685
+
"direction": "out"
686
+
}
687
+
```
688
+
689
+
Here's JavaScript code that creates a single event:
690
+
691
+
```javascript
692
+
module.exports=asyncfunction (context, myTimer) {
693
+
var timeStamp =newDate().toISOString();
694
+
695
+
context.bindings.outputEvent= {
696
+
id:'message-id',
697
+
subject:'subject-name',
698
+
dataVersion:'1.0',
699
+
eventType:'event-type',
700
+
data:"event-data",
701
+
eventTime: timeStamp
702
+
};
703
+
context.done();
704
+
};
705
+
```
706
+
707
+
Here's JavaScript code that creates multiple events:
708
+
709
+
```javascript
710
+
module.exports=function(context) {
711
+
var timeStamp =newDate().toISOString();
712
+
713
+
context.bindings.outputEvent= [];
714
+
715
+
context.bindings.outputEvent.push({
716
+
id:'message-id-1',
717
+
subject:'subject-name',
718
+
dataVersion:'1.0',
719
+
eventType:'event-type',
720
+
data:"event-data",
721
+
eventTime: timeStamp
722
+
});
723
+
context.bindings.outputEvent.push({
724
+
id:'message-id-2',
725
+
subject:'subject-name',
726
+
dataVersion:'1.0',
727
+
eventType:'event-type',
728
+
data:"event-data",
729
+
eventTime: timeStamp
730
+
});
731
+
context.done();
732
+
};
733
+
```
734
+
735
+
# [Python](#tab/python)
736
+
737
+
The Event Grid output binding is not available for Python.
738
+
739
+
# [Java](#tab/java)
740
+
741
+
The Event Grid output binding is not available for Java.
742
+
743
+
---
744
+
745
+
## Output - attributes and annotations
746
+
747
+
# [C#](#tab/csharp)
748
+
749
+
For [C# class libraries](functions-dotnet-class-library.md), use the [EventGridAttribute](https://github.com/Azure/azure-functions-eventgrid-extension/blob/dev/src/EventGridExtension/OutputBinding/EventGridAttribute.cs) attribute.
750
+
751
+
The attribute's constructor takes the name of an app setting that contains the name of the custom topic, and the name of an app setting that contains the topic key. For more information about these settings, see [Output - configuration](#output---configuration). Here's an `EventGrid` attribute example:
|**direction**| n/a | Must be set to "out". This parameter is set automatically when you create the binding in the Azure portal. |
790
+
|**name**| n/a | The variable name used in function code that represents the event. |
791
+
|**topicEndpointUri**|**TopicEndpointUri**| The name of an app setting that contains the URI for the custom topic, such as `MyTopicEndpointUri`. |
792
+
|**topicKeySetting**|**TopicKeySetting**| The name of an app setting that contains an access key for the custom topic. |
793
+
794
+
[!INCLUDE [app settings to local.settings.json](../../includes/functions-app-settings-local.md)]
795
+
796
+
> [!IMPORTANT]
797
+
> Ensure that you set the value of the `TopicEndpointUri` configuration property to the name of an app setting that contains the URI of the custom topic. Do not specify the URI of the custom topic directly in this property.
798
+
799
+
## Output - usage
800
+
801
+
# [C#](#tab/csharp)
802
+
803
+
Send messages by using a method parameter such as `out EventGridEvent paramName`. To write multiple messages, you can use `ICollector<EventGridEvent>` or
804
+
`IAsyncCollector<EventGridEvent>` in place of `out EventGridEvent`.
805
+
806
+
# [C# Script](#tab/csharp-script)
807
+
808
+
Send messages by using a method parameter such as `out EventGridEvent paramName`. In C# script, `paramName` is the value specified in the `name` property of *function.json*. To write multiple messages, you can use `ICollector<EventGridEvent>` or
809
+
`IAsyncCollector<EventGridEvent>` in place of `out EventGridEvent`.
810
+
811
+
# [JavaScript](#tab/javascript)
812
+
813
+
Access the output event by using `context.bindings.<name>` where `<name>` is the value specified in the `name` property of *function.json*.
814
+
815
+
# [Python](#tab/python)
816
+
817
+
The Event Grid output binding is not available for Python.
818
+
819
+
# [Java](#tab/java)
820
+
821
+
The Event Grid output binding is not available for Java.
0 commit comments