0
0
Azurecloud~5 mins

Event Grid for event-driven architecture in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Event Grid helps different parts of your cloud apps talk to each other by sending messages when things happen. It solves the problem of making apps react quickly and automatically without checking all the time.
When you want to send a message automatically after a file is uploaded to storage.
When you want to trigger a function right after a database record changes.
When you want to connect different apps so one can react to events from another.
When you want to reduce waiting time by reacting instantly to changes.
When you want to build apps that work together without tight connections.
Config File - eventgrid-subscription.json
eventgrid-subscription.json
{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "resources": [
    {
      "type": "Microsoft.EventGrid/eventSubscriptions",
      "apiVersion": "2021-06-01-preview",
      "name": "exampleStorageAccount/blobCreatedSubscription",
      "properties": {
        "destination": {
          "endpointType": "WebHook",
          "properties": {
            "endpointUrl": "https://myapp.azurewebsites.net/api/eventhandler"
          }
        },
        "filter": {
          "includedEventTypes": ["Microsoft.Storage.BlobCreated"]
        }
      }
    }
  ]
}

This JSON file creates an Event Grid subscription that listens for new files created in a storage account. The destination is a webhook URL where the event message will be sent. The filter ensures only 'BlobCreated' events trigger the webhook.

Commands
This command creates an Event Grid subscription named 'blobCreatedSubscription' that listens to new blob creation events in the specified storage account and sends them to the given webhook URL.
Terminal
az eventgrid event-subscription create --name blobCreatedSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/exampleResourceGroup/providers/Microsoft.Storage/storageAccounts/exampleStorageAccount --endpoint https://myapp.azurewebsites.net/api/eventhandler --included-event-types Microsoft.Storage.BlobCreated
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/exampleResourceGroup/providers/Microsoft.Storage/storageAccounts/exampleStorageAccount/providers/Microsoft.EventGrid/eventSubscriptions/blobCreatedSubscription", "name": "blobCreatedSubscription", "type": "Microsoft.EventGrid/eventSubscriptions", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.azurewebsites.net/api/eventhandler" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ] } }
--name - Sets the name of the event subscription
--source-resource-id - Specifies the resource to listen for events
--endpoint - Defines where to send the event messages
--included-event-types - Filters which events trigger the subscription
This command lists all event subscriptions for the specified storage account to verify the subscription was created.
Terminal
az eventgrid event-subscription list --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/exampleResourceGroup/providers/Microsoft.Storage/storageAccounts/exampleStorageAccount
Expected OutputExpected
[ { "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/exampleResourceGroup/providers/Microsoft.Storage/storageAccounts/exampleStorageAccount/providers/Microsoft.EventGrid/eventSubscriptions/blobCreatedSubscription", "name": "blobCreatedSubscription", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.azurewebsites.net/api/eventhandler" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ] } } ]
--source-resource-id - Specifies the resource to list event subscriptions for
This command deletes the event subscription when it is no longer needed to stop receiving events.
Terminal
az eventgrid event-subscription delete --name blobCreatedSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/exampleResourceGroup/providers/Microsoft.Storage/storageAccounts/exampleStorageAccount
Expected OutputExpected
No output (command runs silently)
--name - Specifies the name of the subscription to delete
--source-resource-id - Specifies the resource the subscription belongs to
Key Concept

If you remember nothing else from this pattern, remember: Event Grid sends messages automatically when something important happens, so your apps can react instantly without checking all the time.

Common Mistakes
Using the wrong resource ID for the source when creating the subscription
The subscription won't listen to the correct events and will not trigger.
Always copy the full resource ID of the exact resource you want to listen to.
Not specifying the correct event types in the filter
You may get no events or too many unwanted events, causing confusion or extra work.
Use the --included-event-types flag to list only the events you want.
Using an incorrect or unreachable endpoint URL for the webhook
Event Grid cannot deliver events, so your app won't react as expected.
Make sure the endpoint URL is correct, publicly reachable, and ready to receive events.
Summary
Create an Event Grid subscription to listen for specific events from a resource.
Verify the subscription exists by listing subscriptions for the resource.
Delete the subscription when it is no longer needed to stop event delivery.