Skip to content

Commit 37cacec

Browse files
authored
Merge pull request #216972 from spelluru/egridfunc1102
updated steps and screenshots
2 parents 243e9c3 + 136422f commit 37cacec

17 files changed

+80
-22
lines changed

articles/event-grid/custom-event-to-function.md

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,105 @@
11
---
22
title: 'Quickstart: Send custom events to Azure Function - Event Grid'
33
description: 'Quickstart: Use Azure Event Grid and Azure CLI or portal to publish a topic, and subscribe to that event. An Azure Function is used for the endpoint.'
4-
ms.date: 09/28/2021
4+
ms.date: 11/02/2022
55
ms.topic: quickstart
66
ms.custom: mode-other, devx-track-azurecli
77
ms.devlang: azurecli
88
---
99

1010
# Quickstart: Route custom events to an Azure Function with Event Grid
1111

12-
Azure Event Grid is an eventing service for the cloud. Azure Functions is one of the supported event handlers. In this article, you use the Azure portal to create a custom topic, subscribe to the custom topic, and trigger the event to view the result. You send the events to an Azure Function.
12+
[Azure Event Grid](overview.md) is an eventing service for the cloud. Azure Functions is one of the [supported event handlers](event-handlers.md). In this article, you use the Azure portal to create a custom topic, subscribe to the custom topic, and trigger the event to view the result. You send the events to an Azure Function.
1313

1414
[!INCLUDE [quickstarts-free-trial-note.md](../../includes/quickstarts-free-trial-note.md)]
1515

16-
## Create Azure Function
16+
## Create Azure function app
17+
18+
1. Sign in to the [Azure portal](https://portal.azure.com).
19+
1. On the left navigational menu, select **All services**.
20+
1. Select **Compute** in the list of **Categories**.
21+
1. Hover (not select) the mouse over **Function App**, and select **Create**.
22+
23+
:::image type="content" source="./media/custom-event-to-function/create-function-app-link.png" lightbox="./media/custom-event-to-function/create-function-app-link.png" alt-text="Screenshot showing the select of Create link for a Function App.":::
24+
1. On the **Basics** page of the **Create Function App** wizard, follow these steps:
25+
1. Select your **Azure subscription** in which you want to create the function app.
26+
1. Create a new **resource group** or select an existing resource group.
27+
1. Specify a **name** for the function app.
28+
1. Select **.NET** for **Runtime stack**.
29+
1. Select the **region** closest to you.
30+
1. Select **Next: Hosting** at the bottom of the page.
31+
32+
:::image type="content" source="./media/custom-event-to-function/create-function-app-page.png" alt-text="Screenshot showing the Basics tab of the Create Function App page.":::
33+
1. On the **Hosting** page, create a new storage account or select an existing storage account to be associated with the function app, and then select **Review + create** at the bottom of the page.
34+
35+
:::image type="content" source="./media/custom-event-to-function/create-function-app-hosting-page.png" alt-text="Screenshot showing the Hosting tab of the Create Function App page.":::
36+
1. On the **Review + create** page, review settings, and select **Create** at the bottom of the page to create the function app.
37+
1. Once the deployment is successful, select **Go to resource** to navigate to the home page for the function app.
38+
39+
## Create a function
1740
Before subscribing to the custom topic, create a function to handle the events.
1841

19-
1. Create a function app using instructions from [Create a function app](../azure-functions/functions-get-started.md).
2042
1. On the **Function App** page, select **Functions** on the left menu.
21-
1. Select **+Create** on the toolbar to create a function.
43+
1. Select **+ Create** on the toolbar to create a function.
44+
45+
:::image type="content" source="./media/custom-event-to-function/create-function-link.png" alt-text="Screenshot showing the selection of Create function link.":::
46+
2247
1. On the **Create Function** page, follow these steps:
2348
1. This step is optional. For **Development environment**, select the development environment that you want to use to work with the function code.
24-
1. Select **Azure Event Grid Trigger** in the **Select a template** section.
25-
1. Enter a name for the function. In this example, it's **HandleEventsFunc**.
49+
1. Select **Azure Event Grid Trigger** in the **Select a template** section. Use the scroll bar right to the list to scroll down if needed.
50+
1. In the **Template details** section in the bottom pane, enter a name for the function. In this example, it's **HandleEventsFunc**.
2651

2752
:::image type="content" source="./media/custom-event-to-function/function-event-grid-trigger.png" lightbox="./media/custom-event-to-function/function-event-grid-trigger.png" alt-text="Select Event Grid trigger.":::
28-
4. Use the **Code + Test** page to see the existing code for the function and update it.
53+
4. On the **Function** page for the **HandleEventsFunc**, select **Code + Test** on the left navigational menu.
2954

3055
:::image type="content" source="./media/custom-event-to-function/function-code-test-menu.png" alt-text="Image showing the selection Code + Test menu for an Azure function.":::
56+
5. Replace the code with the following code.
3157

32-
[!INCLUDE [event-grid-register-provider-portal.md](../../includes/event-grid-register-provider-portal.md)]
58+
```csharp
59+
#r "Azure.Messaging.EventGrid"
60+
#r "System.Memory.Data"
61+
62+
using Azure.Messaging.EventGrid;
63+
using System;
64+
65+
public static void Run(EventGridEvent eventGridEvent, ILogger log)
66+
{
67+
log.LogInformation(eventGridEvent.Data.ToString());
68+
}
69+
```
70+
:::image type="content" source="./media/custom-event-to-function/function-updated-code.png" alt-text="Screenshot showing the Code + Test view of an Azure function with the updated code.":::
71+
6. Select **Monitor** on the left menu, and then select **Logs**.
72+
73+
:::image type="content" source="./media/custom-event-to-function/monitor-page.png" alt-text="Screenshot showing the Monitor view the Azure function.":::
74+
7. Keep this window or tab of the browser open so that you can see the received event information.
3375

3476
## Create a custom topic
3577

36-
An event grid topic provides a user-defined endpoint that you post your events to.
78+
An Event Grid topic provides a user-defined endpoint that you post your events to.
3779

38-
1. Sign in to [Azure portal](https://portal.azure.com/).
39-
2. Select **All services** on the left navigational menu, search for **Event Grid**, and select **Event Grid Topics**.
80+
1. On a new tab of the web browser window, sign in to [Azure portal](https://portal.azure.com/).
81+
2. In the search bar at the topic, search for **Event Grid Topics**, and select **Event Grid Topics**.
4082

41-
:::image type="content" source="./media/custom-event-to-function/select-event-grid-topics.png" alt-text="Image showing the selection of Event Grid Topics.":::
42-
3. On the **Event Grid Topics** page, select **+ Add** on the toolbar.
83+
:::image type="content" source="./media/custom-event-to-function/select-event-grid-topics.png" alt-text="Image showing the selection of Event Grid topics.":::
84+
3. On the **Event Grid Topics** page, select **+ Create** on the command bar.
4385

44-
:::image type="content" source="./media/custom-event-to-function/add-event-grid-topic-button.png" alt-text="Image showing the Create button to create an event grid topic.":::
86+
:::image type="content" source="./media/custom-event-to-function/add-event-grid-topic-button.png" alt-text="Screenshot showing the Create button to create an Event Grid topic.":::
4587
4. On the **Create Topic** page, follow these steps:
4688
1. Select your **Azure subscription**.
4789
2. Select the same **resource group** from the previous steps.
4890
3. Provide a unique **name** for the custom topic. The topic name must be unique because it's represented by a DNS entry. Don't use the name shown in the image. Instead, create your own name - it must be between 3-50 characters and contain only values a-z, A-Z, 0-9, and "-".
49-
4. Select a **location** for the event grid topic.
91+
4. Select a **location** for the Event Grid topic.
5092
5. Select **Review + create**.
5193

5294
:::image type="content" source="./media/custom-event-to-function/create-custom-topic.png" alt-text="Image showing the Create Topic page.":::
5395
1. On the **Review + create** page, review settings and select **Create**.
54-
5. After the custom topic has been created, select **Go to resource** link to see the following Event Grid Topic page for the topic you created.
96+
5. After the custom topic has been created, select **Go to resource** link to see the following Event Grid topic page for the topic you created.
5597

56-
:::image type="content" source="./media/custom-event-to-function/event-grid-topic-home-page.png" alt-text="Image showing the home page for your Event Grid custom topic.":::
98+
:::image type="content" source="./media/custom-event-to-function/event-grid-topic-home-page.png" lightbox="./media/custom-event-to-function/event-grid-topic-home-page.png" alt-text="Image showing the home page for your Event Grid custom topic.":::
5799

58100
## Subscribe to custom topic
59101

60-
You subscribe to an event grid topic to tell Event Grid which events you want to track, and where to send the events.
102+
You subscribe to an Event Grid topic to tell Event Grid which events you want to track, and where to send the events.
61103

62104
1. Now, on the **Event Grid Topic** page for your custom topic, select **+ Event Subscription** on the toolbar.
63105

@@ -89,15 +131,28 @@ The first example uses Azure CLI. It gets the URL and key for the custom topic,
89131
1. In the Azure portal, select **Cloud Shell**. Select **Bash** in the top-left corner of the Cloud Shell window.
90132

91133
:::image type="content" source="./media/custom-event-quickstart-portal/cloud-shell-bash.png" alt-text="Image showing Cloud Shell - Bash window":::
134+
1. Set the `topicname` and `resourcegroupname` variables that will be used in the commands.
135+
136+
Replace `TOPICNAME` with the name of your Event Grid topic.
137+
138+
```azurecli
139+
topicname="TOPICNAME"
140+
```
141+
142+
Replace `RESOURCEGROUPNAME` with the name of the Azure resource group that contains the Event Grid topic.
143+
144+
```azurecli
145+
resourcegroupname="RESOURCEGROUPNAME"
146+
```
92147
1. Run the following command to get the **endpoint** for the topic: After you copy and paste the command, update the **topic name** and **resource group name** before you run the command.
93148

94149
```azurecli
95-
endpoint=$(az eventgrid topic show --name <topic name> -g <resource group name> --query "endpoint" --output tsv)
150+
endpoint=$(az eventgrid topic show --name $topicname -g $resourcegroupname --query "endpoint" --output tsv)
96151
```
97152
2. Run the following command to get the **key** for the custom topic: After you copy and paste the command, update the **topic name** and **resource group** name before you run the command.
98153

99154
```azurecli
100-
key=$(az eventgrid topic key list --name <topic name> -g <resource group name> --query "key1" --output tsv)
155+
key=$(az eventgrid topic key list --name $topicname -g $resourcegroupname --query "key1" --output tsv)
101156
```
102157
3. Copy the following statement with the event definition, and press **ENTER**.
103158

@@ -118,6 +173,9 @@ The second example uses PowerShell to perform similar steps.
118173

119174
```powershell
120175
$resourceGroupName = <resource group name>
176+
```
177+
178+
```powershell
121179
$topicName = <topic name>
122180
```
123181
3. Run the following commands to get the **endpoint** and the **keys** for the topic:
@@ -160,7 +218,7 @@ The second example uses PowerShell to perform similar steps.
160218
### Verify that function received the event
161219
You've triggered the event, and Event Grid sent the message to the endpoint you configured when subscribing. Navigate to your Event Grid triggered function and open the logs. You should see a copy of the data payload of the event in the logs. If you don't make sure you open the logs window first, or hit reconnect, and then try sending a test event again.
162220

163-
:::image type="content" source="./media/custom-event-to-function/successful-function.png" alt-text="Image showing the Monitor view of the Azure function with a log.":::
221+
:::image type="content" source="./media/custom-event-to-function/successful-function.png" lightbox="./media/custom-event-to-function/successful-function.png" alt-text="Image showing the Monitor view of the Azure function with a log.":::
164222

165223
## Clean up resources
166224
If you plan to continue working with this event, don't clean up the resources created in this article. Otherwise, delete the resources you created in this article.
28.2 KB
Loading
6.68 KB
Loading
24.3 KB
Loading
75 KB
Loading
62.1 KB
Loading
41.4 KB
Loading
2.14 KB
Loading
-3.33 KB
Loading
1.43 KB
Loading

0 commit comments

Comments
 (0)