使用 Azure CLI 订阅自定义主题的事件Subscribe to events for a custom topic with Azure CLI

此脚本创建自定义主题事件的事件网格订阅。This script creates an Event Grid subscription to the events for a custom topic.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

预览示例脚本需要事件网格扩展。The preview sample script requires the Event Grid extension. 若要安装,请运行 az extension add --name eventgridTo install, run az extension add --name eventgrid.

示例脚本 - 稳定版Sample script - stable

#!/bin/bash

# Provide the name of the topic you are subscribing to
myTopic=demoContosoTopic

# Provide the name of the resource group containing the custom topic
resourceGroup=demoResourceGroup

# Provide an endpoint for handling the events.
myEndpoint="<endpoint URL>"

# Select the Azure subscription that contains the custom topic.
az account set --subscription "<name or ID of the subscription>"

# Get the resource ID of the custom topic
topicID=$(az eventgrid topic show --name $myTopic -g $resourceGroup --query id --output tsv)

# Subscribe to the custom event. Include the resource group that contains the custom topic.
az eventgrid event-subscription create \
  --source-resource-id $topicID \
  --name demoSubscription \
  --endpoint $myEndpoint

示例脚本 - 预览扩展Sample script - preview extension

#!/bin/bash

# You must have the latest version of the Event Grid preview extension.
# If you have not installed previously:
# az extension add -n eventgrid
# If you have installed previously:
# az extension update -n eventgrid

# Provide the name of the topic you are subscribing to
myTopic=demoContosoTopic

# Provide the name of the resource group containing the custom topic
resourceGroup=demoResourceGroup

# Provide an endpoint for handling the events.
myEndpoint="<endpoint URL>"

# Select the Azure subscription that contains the custom topic.
az account set --subscription "<name or ID of the subscription>"

# Get the resource ID of the custom topic
topicID=$(az eventgrid topic show --name $myTopic -g $resourceGroup --query id --output tsv)

# Subscribe to the custom event. Include the resource group that contains the custom topic.
az eventgrid event-subscription create \
  --source-resource-id $topicID \
  --name demoSubscription \
  --endpoint $myEndpoint

脚本说明Script explanation

此脚本使用以下命令创建事件订阅。This script uses the following command to create the event subscription. 表中的每条命令链接到特定于命令的文档。Each command in the table links to command-specific documentation.

CommandCommand 说明Notes
az eventgrid event-subscription createaz eventgrid event-subscription create 创建事件网格订阅。Create an Event Grid subscription.
az eventgrid event-subscription create - 扩展版本az eventgrid event-subscription create - extension version 创建事件网格订阅。Create an Event Grid subscription.

后续步骤Next steps