Skip to content

Commit 556ef19

Browse files
committed
Review & Update for freshness and to fix a UUF issue
1 parent c796718 commit 556ef19

File tree

4 files changed

+81
-5
lines changed

4 files changed

+81
-5
lines changed
98.5 KB
Loading
38 KB
Loading
70.9 KB
Loading

articles/event-grid/post-to-custom-topic.md

Lines changed: 81 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
---
22
title: Post event to custom Azure Event Grid topic
3-
description: This article describes how to post an event to a custom topic. It shows the format of the post and event data.
3+
description: This article describes how to post an event to a custom topic using Azure PowerShell or Azure CLI. It shows the format of the post and event data.
44
ms.topic: conceptual
5-
ms.date: 01/18/2024
5+
ms.date: 06/18/2024
66
---
77

8-
# Publish events to Azure Event Grid custom topics using access keys
8+
# Publish events to Azure Event Grid custom topics using access keys using PowerShell and CLI
99

1010
This article describes how to post an event to a custom topic using an access key. It shows the format of the post and event data. The [Service Level Agreement (SLA)](https://azure.microsoft.com/support/legal/sla/event-grid/v1_0/) only applies to posts that match the expected format.
1111

@@ -17,30 +17,48 @@ This article describes how to post an event to a custom topic using an access ke
1717

1818
When sending the HTTP POST to a custom topic, use the URI format: `https://<topic-endpoint>?api-version=2018-01-01`. For example, a valid URI is: `https://exampletopic.westus2-1.eventgrid.azure.net/api/events?api-version=2018-01-01`. To get the endpoint for a custom topic using Azure CLI, use:
1919

20+
# [Azure portal](#tab/azure-portal)
21+
You can find the topic's endpoint on the **Overview** tab of the **Event Grid Topic** page in the Azure portal.
22+
23+
:::image type="content" source="./media/post-to-custom-topic/topic-endpoint.png" alt-text="Screenshot that shows the Event Grid Topic page on the Azure portal with the topic endpoint highlighted." lightbox="./media/post-to-custom-topic/topic-endpoint.png":::
24+
25+
# [Azure CLI](#tab/azure-cli)
26+
2027
```azurecli-interactive
2128
az eventgrid topic show --name <topic-name> -g <topic-resource-group> --query "endpoint"
2229
```
2330

24-
To get the endpoint for a custom topic using Azure PowerShell, use:
31+
# [Azure PowerShell](#tab/azure-powershell)
2532

2633
```powershell
2734
(Get-AzEventGridTopic -ResourceGroupName <topic-resource-group> -Name <topic-name>).Endpoint
2835
```
2936

37+
---
38+
3039
## Header
3140

3241
In the request, include a header value named `aeg-sas-key` that contains a key for authentication. For example, a valid header value is `aeg-sas-key: xxxxxxxxxxxxxxxxxxxxxxx`. To get the key for a custom topic using Azure CLI, use:
3342

43+
# [Azure portal](#tab/azure-portal)
44+
To get the access key for the custom topic, select **Access keys** tab on the **Event Grid Topic** page in the Azure portal.
45+
46+
:::image type="content" source="./media/post-to-custom-topic/custom-topic-access-keys.png" alt-text="Screenshot that shows the Access Keys tab of the Event Grid Topic page on the Azure portal." lightbox="./media/post-to-custom-topic/custom-topic-access-keys.png":::
47+
48+
# [Azure CLI](#tab/azure-cli)
49+
3450
```azurecli
3551
az eventgrid topic key list --name <topic-name> -g <topic-resource-group> --query "key1"
3652
```
3753

38-
To get the key for a custom topic using PowerShell, use:
54+
# [Azure PowerShell](#tab/azure-powershell)
3955

4056
```powershell
4157
(Get-AzEventGridTopicKey -ResourceGroupName <topic-resource-group> -Name <topic-name>).Key1
4258
```
4359

60+
---
61+
4462
## Event data
4563

4664
For custom topics, the top-level data contains the same fields as standard resource-defined events. One of those properties is a `data` property that contains properties unique to the custom topic. As an event publisher, you determine properties for that data object. Here's the schema:
@@ -78,6 +96,64 @@ For example, a valid event data schema is:
7896
}]
7997
```
8098

99+
## Send the sample event
100+
101+
# [Azure portal](#tab/azure-portal)
102+
103+
1. In the [Azure portal](https://portal.azure.com), launch Cloud Shell.
104+
1. In the Cloud Shell, run the commands from the Azure PowerShell or Azure CLI in the **Bash** or **PowerShell** session.
105+
106+
:::image type="content" source="./media/post-to-custom-topic/cloud-shell.png" alt-text="Screenshot that shows the Cloud Shell in the Azure portal." lightbox="./media/post-to-custom-topic/cloud-shell.png":::
107+
108+
109+
# [Azure CLI](#tab/azure-cli)
110+
111+
```azurecli
112+
endpoint=$(az eventgrid topic show --name <topic name> -g <resource group name> --query "endpoint" --output tsv)
113+
114+
key=$(az eventgrid topic key list --name <topic name> -g <resource group name> --query "key1" --output tsv)
115+
116+
event='[ {"id": "'"$RANDOM"'", "eventType": "recordInserted", "subject": "myapp/vehicles/motorcycles", "eventTime": "'`date +%Y-%m-%dT%H:%M:%S%z`'", "data":{ "make": "Ducati", "model": "Monster"},"dataVersion": "1.0"} ]'
117+
118+
curl -X POST -H "aeg-sas-key: $key" -d "$event" $endpoint
119+
```
120+
121+
# [Azure PowerShell](#tab/azure-powershell)
122+
123+
```azurepowershell
124+
$resourceGroupName = "<resource group name>"
125+
$topicName = "<topic name>"
126+
127+
$endpoint = (Get-AzEventGridTopic -ResourceGroupName $resourceGroupName -Name $topicName).Endpoint
128+
129+
$keys = Get-AzEventGridTopicKey -ResourceGroupName $resourceGroupName -Name $topicName
130+
131+
$eventID = Get-Random 99999
132+
#Date format should be SortableDateTimePattern (ISO 8601)
133+
$eventDate = Get-Date -Format s
134+
135+
#Construct body using Hashtable
136+
$htbody = @{
137+
id= $eventID
138+
eventType="recordInserted"
139+
subject="myapp/vehicles/motorcycles"
140+
eventTime= $eventDate
141+
data= @{
142+
make="Ducati"
143+
model="Monster"
144+
}
145+
dataVersion="1.0"
146+
}
147+
148+
#Use ConvertTo-Json to convert event body from Hashtable to JSON Object
149+
#Append square brackets to the converted JSON payload since they are expected in the event's JSON payload syntax
150+
$body = "["+(ConvertTo-Json $htbody)+"]"
151+
152+
Invoke-WebRequest -Uri $endpoint -Method POST -Body $body -Headers @{"aeg-sas-key" = $keys.Key1}
153+
```
154+
155+
---
156+
81157
## Response
82158

83159
After posting to the topic endpoint, you receive a response. The response is a standard HTTP response code. Some common responses are:

0 commit comments

Comments
 (0)