0
0
Azurecloud~7 mins

Event Grid subscriptions and filters in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Event Grid subscriptions let you receive notifications when something happens in your cloud resources. Filters help you choose which events you want to get, so you only get the important ones.
When you want to get notified only about new files added to a storage container, not all changes.
When you want to trigger a function only if a virtual machine changes state to running.
When you want to send alerts only for error events from your app, ignoring info messages.
When you want to connect your app to events from multiple sources but only care about specific event types.
When you want to reduce the amount of data your app processes by filtering events early.
Config File - eventgrid-subscription.json
eventgrid-subscription.json
{
  "type": "Microsoft.EventGrid/eventSubscriptions",
  "apiVersion": "2023-06-01",
  "name": "myEventSubscription",
  "properties": {
    "destination": {
      "endpointType": "WebHook",
      "properties": {
        "endpointUrl": "https://myapp.example.com/api/events"
      }
    },
    "filter": {
      "subjectBeginsWith": "/blobServices/default/containers/images/",
      "subjectEndsWith": ".jpg",
      "isSubjectCaseSensitive": false,
      "includedEventTypes": [
        "Microsoft.Storage.BlobCreated"
      ]
    }
  }
}

This JSON creates an Event Grid subscription named myEventSubscription.

The destination is a webhook URL where events will be sent.

The filter section limits events to those where the subject starts with /blobServices/default/containers/images/ and ends with .jpg, meaning only new JPEG images in the images container.

It also only includes the event type Microsoft.Storage.BlobCreated, so only new blobs trigger the subscription.

The filter is case insensitive for the subject.

Commands
This command creates an Event Grid subscription named myEventSubscription. It listens to blob created events from the storage account mystorageaccount. It filters events to only those starting with the images container path and ending with .jpg files. Events are sent to the specified webhook endpoint.
Terminal
az eventgrid event-subscription create --name myEventSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount --endpoint https://myapp.example.com/api/events --included-event-types Microsoft.Storage.BlobCreated --subject-begins-with /blobServices/default/containers/images/ --subject-ends-with .jpg
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount/providers/Microsoft.EventGrid/eventSubscriptions/myEventSubscription", "name": "myEventSubscription", "type": "Microsoft.EventGrid/eventSubscriptions", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.example.com/api/events" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ], "subjectBeginsWith": "/blobServices/default/containers/images/", "subjectEndsWith": ".jpg", "isSubjectCaseSensitive": false }, "provisioningState": "Succeeded" }
--included-event-types - Specifies which event types to receive
--subject-begins-with - Filters events by subject prefix
--subject-ends-with - Filters events by subject suffix
This command shows the details of the event subscription named myEventSubscription to verify it was created with the correct filters and destination.
Terminal
az eventgrid event-subscription show --name myEventSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount/providers/Microsoft.EventGrid/eventSubscriptions/myEventSubscription", "name": "myEventSubscription", "type": "Microsoft.EventGrid/eventSubscriptions", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.example.com/api/events" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ], "subjectBeginsWith": "/blobServices/default/containers/images/", "subjectEndsWith": ".jpg", "isSubjectCaseSensitive": false }, "provisioningState": "Succeeded" }
This command lists all event subscriptions on the storage account to see all active subscriptions and their filters.
Terminal
az eventgrid event-subscription list --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount
Expected OutputExpected
[ { "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount/providers/Microsoft.EventGrid/eventSubscriptions/myEventSubscription", "name": "myEventSubscription", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.example.com/api/events" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ], "subjectBeginsWith": "/blobServices/default/containers/images/", "subjectEndsWith": ".jpg", "isSubjectCaseSensitive": false }, "provisioningState": "Succeeded" } ]
This command deletes the event subscription named myEventSubscription when it is no longer needed to stop receiving events.
Terminal
az eventgrid event-subscription delete --name myEventSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/myResourceGroup/providers/Microsoft.Storage/storageAccounts/mystorageaccount
Expected OutputExpected
No output (command runs silently)
Key Concept

If you remember nothing else from this pattern, remember: filters let you receive only the events you care about, saving time and resources.

Common Mistakes
Not specifying included event types when creating the subscription
You might receive all event types, causing unnecessary processing and noise.
Always use the --included-event-types flag to limit events to only those you want.
Using incorrect subject filters that do not match event subjects
No events will be delivered because the filter excludes all events.
Check the exact event subject format and use correct prefixes and suffixes in filters.
Not verifying the subscription after creation
You might miss misconfigurations that prevent events from arriving.
Use the show or list commands to confirm the subscription settings.
Summary
Create an Event Grid subscription with filters to receive only specific event types and subjects.
Verify the subscription details to ensure filters and destination are correct.
Delete the subscription when it is no longer needed to stop event delivery.