Subscribe to events for a resource group with Azure CLI
This script creates an Event Grid subscription to the events for a resource group.
To run this sample, install the latest version of the Azure CLI. To start, run az login to create a connection with Azure.
Samples for the Azure CLI are written for the bash shell. To run this sample in Windows PowerShell or Command Prompt, you may need to change
elements of the script.
If you don't have an Azure subscription, create an Azure free account before you begin.
The preview sample script requires the Event Grid extension. To install, run az extension add --name eventgrid.
Sample script - stable
#!/bin/bash
# Provide an endpoint for handling the events.
myEndpoint="<endpoint URL>"
# Provide the name of the resource group to subscribe to.
myResourceGroup="<resource group name>"
# Select the Azure subscription that contains the resource group.
az account set --subscription "<name or ID of the subscription>"
# Subscribe to the resource group. Provide the name of the resource group you want to subscribe to.
az eventgrid event-subscription create \
--name demoSubscriptionToResourceGroup \
--resource-group $myResourceGroup \
--endpoint $myEndpoint
Sample script - preview extension
#!/bin/bash
# Provide an endpoint for handling the events.
myEndpoint="<endpoint URL>"
# Provide the name of the resource group to subscribe to.
myResourceGroup="<resource group name>"
# 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)
# 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
Script explanation
This script uses the following command to create the event subscription. Each command in the table links to command-specific documentation.
| Command | Notes |
|---|---|
| az eventgrid event-subscription create | Create an Event Grid subscription. |
| az eventgrid event-subscription create - extension version | Create an Event Grid subscription. |
Next steps
- For information about querying subscriptions, see Query Event Grid subscriptions.
- For more information on the Azure CLI, see Azure CLI documentation.