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
description: 'Quickstart: Use Azure Event Grid and Azure CLI to publish a topic, and subscribe to that event. A storage queue is used for the endpoint.'
4
-
ms.date: 02/02/2021
4
+
ms.date: 11/17/2022
5
5
ms.topic: quickstart
6
6
ms.custom: devx-track-azurecli, mode-api
7
7
---
8
8
9
-
# Quickstart: Route custom events to Azure Queue storage with Azure CLI and Event Grid
9
+
# Quickstart: Route custom events to Azure Queue storage via Event Grid using Azure CLI
10
10
11
-
Azure Event Grid is an eventing service for the cloud. Azure Queue storage is one of the supported event handlers. In this article, you use the Azure CLI to create a custom topic, subscribe to the custom topic, and trigger the event to view the result. You send the events to the Queue storage.
11
+
[Azure Event Grid](overview.md) is a highly scalable and serverless event broker that you can use to integrate applications using events. Events are delivered by Event Grid to [supported event handlers](event-handlers.md) and Azure Queue storage is one of them. In this article, you use Azure CLI for the following steps:
- This article requires version 2.0.56 or later of the Azure CLI. If using Azure Cloud Shell, the latest version is already installed.
18
-
19
-
- If you are using Azure PowerShell on your local machine instead of using Cloud Shell in the Azure portal, ensure that you have Azure PowerShell version 1.1.0 or greater. Download the latest version of Azure PowerShell on your Windows machine from [Azure downloads - Command-line tools](https://azure.microsoft.com/downloads/).
13
+
1. Create an Event Grid custom topic.
14
+
1. Create an Azure Queue subscription for the custom topic.
15
+
1. Send sample events to the custom topic.
16
+
1. Verify that those events are delivered to Azure Queue storage.
20
17
21
-
This article gives you commands for using Azure CLI.
Event Grid topics are Azure resources, and must be placed in an Azure resource group. The resource group is a logical collection into which Azure resources are deployed and managed.
26
23
27
-
Create a resource group with the [az group create](/cli/azure/group#az-group-create) command.
24
+
Create a resource group with the [az group create](/cli/azure/group#az-group-create) command. The following example creates a resource group named **gridResourceGroup** in the **westus2** location.
28
25
29
-
The following example creates a resource group named *gridResourceGroup* in the *westus2* location.
26
+
> [!NOTE]
27
+
> Select **Try it** next to the CLI example to launch Cloud Shell in the right pane. Select **Copy** button to copy the command, paste it in the Cloud Shell window, and then press ENTER to run the command.
30
28
31
29
```azurecli-interactive
32
30
az group create --name gridResourceGroup --location westus2
@@ -38,38 +36,52 @@ az group create --name gridResourceGroup --location westus2
38
36
39
37
An event grid topic provides a user-defined endpoint that you post your events to. The following example creates the custom topic in your resource group. Replace `<topic_name>` with a unique name for your custom topic. The event grid topic name must be unique because it's represented by a DNS entry.
40
38
41
-
```azurecli-interactive
42
-
az eventgrid topic create --name <topic_name> -l westus2 -g gridResourceGroup
43
-
```
39
+
1. Specify a name for the topic.
40
+
41
+
```azurecli-interactive
42
+
topicname="<TOPIC NAME>"
43
+
```
44
+
1. Run the following command to create the topic.
45
+
46
+
```azurecli-interactive
47
+
az eventgrid topic create --name $topicname -l westus2 -g gridResourceGroup
48
+
```
44
49
45
50
## Create Queue storage
46
51
47
52
Before subscribing to the custom topic, let's create the endpoint for the event message. You create a Queue storage for collecting the events.
48
53
49
-
```azurecli-interactive
50
-
storagename="<unique-storage-name>"
51
-
queuename="eventqueue"
54
+
1. Specify a unique name for the Azure Storage account.
az storage queue create --name $queuename --account-name $storagename --account-key $key
67
+
```
56
68
57
69
## Subscribe to a custom topic
58
70
59
-
You subscribe to a custom topic to tell Event Grid which events you want to track. The following example subscribes to the custom topic you created, and passes the resource ID of the Queue storage for the endpoint. With Azure CLI, you pass the Queue storage ID as the endpoint. The endpoint is in the format:
71
+
The following example subscribes to the custom topic you created, and passes the resource ID of the Queue storage for the endpoint. With Azure CLI, you pass the Queue storage ID as the endpoint. The endpoint is in the format:
`/subscriptions/<AZURE SUBSCRIPTION ID>/resourcegroups/<RESOURCE GROUP NAME>/providers/Microsoft.Storage/storageAccounts/<STORAGE ACCOUNT NAME>/queueservices/default/queues/<QUEUE NAME>`
62
74
63
75
The following script gets the resource ID of the storage account for the queue. It constructs the ID for the queue storage, and subscribes to an event grid topic. It sets the endpoint type to `storagequeue` and uses the queue ID for the endpoint.
64
76
65
77
```azurecli-interactive
66
78
storageid=$(az storage account show --name $storagename --resource-group gridResourceGroup --query id --output tsv)
topicid=$(az eventgrid topic show --name <topic_name> -g gridResourceGroup --query id --output tsv)
80
+
topicid=$(az eventgrid topic show --name $topicname -g gridResourceGroup --query id --output tsv)
69
81
70
82
az eventgrid event-subscription create \
71
83
--source-resource-id $topicid \
72
-
--name <event_subscription_name> \
84
+
--name mystoragequeuesubscription \
73
85
--endpoint-type storagequeue \
74
86
--endpoint $queueid \
75
87
--expiration-date "<yyyy-mm-dd>"
@@ -91,14 +103,14 @@ If you use the REST API to create the subscription, you pass the ID of the stora
91
103
92
104
## Send an event to your custom topic
93
105
94
-
Let's trigger an event to see how Event Grid distributes the message to your endpoint. First, let's get the URL and key for the custom topic. Again, use your custom topic name for `<topic_name>`.
106
+
Let's trigger an event to see how Event Grid distributes the message to your endpoint. First, let's get the URL and key for the custom topic.
To simplify this article, you use sample event data to send to the custom topic. Typically, an application or Azure service would send the event data. CURL is a utility that sends HTTP requests. In this article, use CURL to send the event to the custom topic. The following example sends three events to the event grid topic:
113
+
To simplify this article, you use sample event data to send to the custom topic. Typically, an application or Azure service would send the event data. CURL is a utility that sends HTTP requests. In this article, you use CURL to send the event to the custom topic. The following example sends three events to the event grid topic:
102
114
103
115
```azurecli-interactive
104
116
for i in 1 2 3
@@ -110,7 +122,7 @@ done
110
122
111
123
Navigate to the Queue storage in the portal, and notice that Event Grid sent those three events to the queue.
:::image type="content" source="./media/custom-event-to-queue-storage/messages.png" alt-text="Screenshot showing the list of messages in the queue that are received from Event Grid.":::
114
126
115
127
> [!NOTE]
116
128
> If you use an [Azure Queue storage trigger for Azure Functions](../azure-functions/functions-bindings-storage-queue-trigger.md) for a queue that receives messages from Event Grid, you may see the following error message on the function execution: `The input is not a valid Base-64 string as it contains a non-base 64 character, more than two padding characters, or an illegal character among the padding characters.`
0 commit comments