使用 Azure CLI 订阅资源组的事件并筛选出资源Subscribe to events for a resource group and filter for a resource with Azure CLI

此脚本创建资源组事件的事件网格订阅。This script creates an Event Grid subscription to the events for a resource group. 它使用筛选器,只获取资源组中指定资源的事件。It uses a filter to get only events for a specified resource in the resource group.

若要运行此示例,请安装最新版本的 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 an endpoint for handling the events.
myEndpoint="<endpoint URL>"

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

# Get resource ID of the resource group.
resourceGroupID=$(az group show --name myResourceGroup --query id --output tsv)

# Get the resource ID to filter events
resourceId=$(az resource show --name demoSecurityGroup --resource-group myResourceGroup --resource-type Microsoft.Network/networkSecurityGroups --query id --output tsv)

# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
az eventgrid event-subscription create \
  --name demoSubscriptionToResourceGroup \
  --source-resource-id $resourceGroupID \
  --endpoint $myEndpoint \
  --subject-begins-with $resourceId

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

#!/bin/bash

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

# Provide the name of the custom topic to create
topicName=<your-topic-name>

# Provide the name of the resource group to contain the custom topic
myResourceGroup=demoResourceGroup

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

# Create the resource group
az group create -n $myResourceGroup -l chinanorth2

# Create custom topic
az eventgrid topic create --name $topicName -l chinanorth2 -g $myResourceGroup

# Get resource ID of custom topic
topicid=$(az eventgrid topic show --name $topicName -g $myResourceGroup --query id --output tsv)

# Subscribe to the custom topic. Filter based on a value in the event data.
az eventgrid event-subscription create \
  --source-resource-id $topicid \
  -n demoAdvancedFilterSub \
  --advanced-filter data.color stringin blue red green \
  --endpoint $endpointURL

脚本说明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