Skip to content

Commit 16ef3e5

Browse files
authored
Merge pull request #104450 from craigshoemaker/crs-functions-eventgrid-restructure
Restructure Event Grid binding ref
2 parents 3ef6253 + f4a8bbd commit 16ef3e5

File tree

4 files changed

+852
-803
lines changed

4 files changed

+852
-803
lines changed

articles/azure-functions/TOC.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,13 @@
345345
href: functions-bindings-cosmosdb-v2.md
346346
displayName: Azure Cosmos DB
347347
- name: Event Grid
348-
href: functions-bindings-event-grid.md
348+
items:
349+
- name: Overview
350+
href: functions-bindings-event-grid.md
351+
- name: Trigger
352+
href: functions-bindings-event-grid-trigger.md
353+
- name: Output
354+
href: functions-bindings-event-grid-output.md
349355
- name: Event Hubs
350356
href: functions-bindings-event-hubs.md
351357
- name: IoT Hub
Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
---
2+
title: Azure Event Grid output binding for Azure Functions
3+
description: Learn to send an Event Grid event in Azure Functions.
4+
author: craigshoemaker
5+
6+
ms.topic: reference
7+
ms.date: 02/14/2020
8+
ms.author: cshoe
9+
ms.custom: fasttrack-edit
10+
---
11+
12+
# Azure Event Grid output binding for Azure Functions
13+
14+
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).
15+
16+
For information on setup and configuration details, see the [overview](./functions-bindings-event-grid.md).
17+
18+
> [!NOTE]
19+
> The Event Grid output binding does not support shared access signatures (SAS tokens). You must use the topic's access key.
20+
21+
> [!IMPORTANT]
22+
> The Event Grid output binding is only available for Functions 2.x and higher.
23+
24+
## Example
25+
26+
# [C#](#tab/csharp)
27+
28+
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:
29+
30+
```csharp
31+
[FunctionName("EventGridOutput")]
32+
[return: EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]
33+
public static EventGridEvent Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
34+
{
35+
return new EventGridEvent("message-id", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
36+
}
37+
```
38+
39+
The following example shows how to use the `IAsyncCollector` interface to send a batch of messages.
40+
41+
```csharp
42+
[FunctionName("EventGridAsyncOutput")]
43+
public static async Task Run(
44+
[TimerTrigger("0 */5 * * * *")] TimerInfo myTimer,
45+
[EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]IAsyncCollector<EventGridEvent> outputEvents,
46+
ILogger log)
47+
{
48+
for (var i = 0; i < 3; i++)
49+
{
50+
var myEvent = new EventGridEvent("message-id-" + i, "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
51+
await outputEvents.AddAsync(myEvent);
52+
}
53+
}
54+
```
55+
56+
# [C# Script](#tab/csharp-script)
57+
58+
The following example shows the Event Grid output binding data in the *function.json* file.
59+
60+
```json
61+
{
62+
"type": "eventGrid",
63+
"name": "outputEvent",
64+
"topicEndpointUri": "MyEventGridTopicUriSetting",
65+
"topicKeySetting": "MyEventGridTopicKeySetting",
66+
"direction": "out"
67+
}
68+
```
69+
70+
Here's C# script code that creates one event:
71+
72+
```cs
73+
#r "Microsoft.Azure.EventGrid"
74+
using System;
75+
using Microsoft.Azure.EventGrid.Models;
76+
using Microsoft.Extensions.Logging;
77+
78+
public static void Run(TimerInfo myTimer, out EventGridEvent outputEvent, ILogger log)
79+
{
80+
outputEvent = new EventGridEvent("message-id", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0");
81+
}
82+
```
83+
84+
Here's C# script code that creates multiple events:
85+
86+
```cs
87+
#r "Microsoft.Azure.EventGrid"
88+
using System;
89+
using Microsoft.Azure.EventGrid.Models;
90+
using Microsoft.Extensions.Logging;
91+
92+
public static void Run(TimerInfo myTimer, ICollector<EventGridEvent> outputEvent, ILogger log)
93+
{
94+
outputEvent.Add(new EventGridEvent("message-id-1", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0"));
95+
outputEvent.Add(new EventGridEvent("message-id-2", "subject-name", "event-data", "event-type", DateTime.UtcNow, "1.0"));
96+
}
97+
```
98+
99+
# [JavaScript](#tab/javascript)
100+
101+
The following example shows the Event Grid output binding data in the *function.json* file.
102+
103+
```json
104+
{
105+
"type": "eventGrid",
106+
"name": "outputEvent",
107+
"topicEndpointUri": "MyEventGridTopicUriSetting",
108+
"topicKeySetting": "MyEventGridTopicKeySetting",
109+
"direction": "out"
110+
}
111+
```
112+
113+
Here's JavaScript code that creates a single event:
114+
115+
```javascript
116+
module.exports = async function (context, myTimer) {
117+
var timeStamp = new Date().toISOString();
118+
119+
context.bindings.outputEvent = {
120+
id: 'message-id',
121+
subject: 'subject-name',
122+
dataVersion: '1.0',
123+
eventType: 'event-type',
124+
data: "event-data",
125+
eventTime: timeStamp
126+
};
127+
context.done();
128+
};
129+
```
130+
131+
Here's JavaScript code that creates multiple events:
132+
133+
```javascript
134+
module.exports = function(context) {
135+
var timeStamp = new Date().toISOString();
136+
137+
context.bindings.outputEvent = [];
138+
139+
context.bindings.outputEvent.push({
140+
id: 'message-id-1',
141+
subject: 'subject-name',
142+
dataVersion: '1.0',
143+
eventType: 'event-type',
144+
data: "event-data",
145+
eventTime: timeStamp
146+
});
147+
context.bindings.outputEvent.push({
148+
id: 'message-id-2',
149+
subject: 'subject-name',
150+
dataVersion: '1.0',
151+
eventType: 'event-type',
152+
data: "event-data",
153+
eventTime: timeStamp
154+
});
155+
context.done();
156+
};
157+
```
158+
159+
# [Python](#tab/python)
160+
161+
The Event Grid output binding is not available for Python.
162+
163+
# [Java](#tab/java)
164+
165+
The Event Grid output binding is not available for Java.
166+
167+
---
168+
169+
## Attributes and annotations
170+
171+
# [C#](#tab/csharp)
172+
173+
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.
174+
175+
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](#configuration). Here's an `EventGrid` attribute example:
176+
177+
```csharp
178+
[FunctionName("EventGridOutput")]
179+
[return: EventGrid(TopicEndpointUri = "MyEventGridTopicUriSetting", TopicKeySetting = "MyEventGridTopicKeySetting")]
180+
public static string Run([TimerTrigger("0 */5 * * * *")] TimerInfo myTimer, ILogger log)
181+
{
182+
...
183+
}
184+
```
185+
186+
For a complete example, see [example](#example).
187+
188+
# [C# Script](#tab/csharp-script)
189+
190+
Attributes are not supported by C# Script.
191+
192+
# [JavaScript](#tab/javascript)
193+
194+
Attributes are not supported by JavaScript.
195+
196+
# [Python](#tab/python)
197+
198+
The Event Grid output binding is not available for Python.
199+
200+
# [Java](#tab/java)
201+
202+
The Event Grid output binding is not available for Java.
203+
204+
---
205+
206+
## Configuration
207+
208+
The following table explains the binding configuration properties that you set in the *function.json* file and the `EventGrid` attribute.
209+
210+
|function.json property | Attribute property |Description|
211+
|---------|---------|----------------------|
212+
|**type** | n/a | Must be set to "eventGrid". |
213+
|**direction** | n/a | Must be set to "out". This parameter is set automatically when you create the binding in the Azure portal. |
214+
|**name** | n/a | The variable name used in function code that represents the event. |
215+
|**topicEndpointUri** |**TopicEndpointUri** | The name of an app setting that contains the URI for the custom topic, such as `MyTopicEndpointUri`. |
216+
|**topicKeySetting** |**TopicKeySetting** | The name of an app setting that contains an access key for the custom topic. |
217+
218+
[!INCLUDE [app settings to local.settings.json](../../includes/functions-app-settings-local.md)]
219+
220+
> [!IMPORTANT]
221+
> 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.
222+
223+
## Usage
224+
225+
# [C#](#tab/csharp)
226+
227+
Send messages by using a method parameter such as `out EventGridEvent paramName`. To write multiple messages, you can use `ICollector<EventGridEvent>` or
228+
`IAsyncCollector<EventGridEvent>` in place of `out EventGridEvent`.
229+
230+
# [C# Script](#tab/csharp-script)
231+
232+
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
233+
`IAsyncCollector<EventGridEvent>` in place of `out EventGridEvent`.
234+
235+
# [JavaScript](#tab/javascript)
236+
237+
Access the output event by using `context.bindings.<name>` where `<name>` is the value specified in the `name` property of *function.json*.
238+
239+
# [Python](#tab/python)
240+
241+
The Event Grid output binding is not available for Python.
242+
243+
# [Java](#tab/java)
244+
245+
The Event Grid output binding is not available for Java.
246+
247+
---
248+
249+
## Next steps
250+
251+
* [Dispatch an Event Grid event](./functions-bindings-event-grid-trigger.md)

0 commit comments

Comments
 (0)