Prenumerera på händelser för ett anpassat ämne med Azure CLI
Det här skriptet skapar en Event Grid-prenumeration på händelser för ett anpassat ämne.
Om du vill köra det här exemplet installerar du den senaste versionen av Azure CLI. Börja genom att köra az login för att upprätta en anslutning med Azure.
Exempel för Azure CLI skrivs för bash gränssnittet. Om du vill köra det här exemplet i Windows PowerShell eller kommando tolken kan du behöva ändra element i skriptet.
Om du inte har en Azure-prenumerationkan du skapa ett kostnads fritt konto innan du börjar.
Förhandsversionen av exempelskriptet kräver Event Grid-tillägget. Installera genom att köra az extension add --name eventgrid.
Exempelskript – stabilt
#!/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>"
# Subscribe to the custom event. Include the resource group that contains the custom topic.
az eventgrid event-subscription create \
--resource-group $resourceGroup \
--topic-name $myTopic \
--name demoSubscription \
--endpoint $myEndpoint
Exempelskript – tillägg för förhandsversion
#!/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
Förklaring av skript
Det här skriptet använder följande kommandon för att skapa händelseprenumerationen. Varje kommando i tabellen länkar till kommandospecifik dokumentation.
| Kommando | Kommentarer |
|---|---|
| az eventgrid event-subscription create | Skapa en Event Grid-prenumeration. |
| az eventgrid event-subscription create – version av tillägg | Skapa en Event Grid-prenumeration. |
Nästa steg
- Information om att köra frågor mot prenumerationer finns i Query Event Grid subscriptions (Köra frågor mot Event Grid-prenumerationer).
- Mer information om Azure CLI finns i Azure CLI-dokumentationen.