0
0
Azurecloud~5 mins

Event Grid for event routing in Azure - Commands & Configuration

Choose your learning style9 modes available
Introduction
Event Grid helps send messages from one place to another automatically when something happens. It solves the problem of connecting different parts of your cloud system without writing complex code.
When you want to notify a function or app immediately after a file is uploaded to storage.
When you need to trigger workflows after a database record changes.
When you want to connect different cloud services without polling or manual checks.
When you want to build reactive apps that respond to events in real time.
When you want to route events from multiple sources to different destinations easily.
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": "storageBlobCreatedSubscription",
      "properties": {
        "destination": {
          "endpointType": "WebHook",
          "properties": {
            "endpointUrl": "https://myapp.azurewebsites.net/api/eventhandler"
          }
        },
        "filter": {
          "includedEventTypes": ["Microsoft.Storage.BlobCreated"]
        },
        "eventDeliverySchema": "EventGridSchema"
      }
    }
  ]
}

This JSON file defines an Event Grid subscription.

type: Specifies this is an Event Grid subscription resource.

name: The name of the subscription.

destination: Where events are sent, here a webhook URL.

filter: Limits events to only blob creation events.

eventDeliverySchema: Format of the event data.

Commands
This command creates an Event Grid subscription that listens to blob creation events on the specified storage account and sends them to the webhook endpoint.
Terminal
az eventgrid event-subscription create --name storageBlobCreatedSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/exampleaccount --endpoint https://myapp.azurewebsites.net/api/eventhandler --included-event-types Microsoft.Storage.BlobCreated
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/exampleaccount/providers/Microsoft.EventGrid/eventSubscriptions/storageBlobCreatedSubscription", "name": "storageBlobCreatedSubscription", "type": "Microsoft.EventGrid/eventSubscriptions", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.azurewebsites.net/api/eventhandler" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ] }, "eventDeliverySchema": "EventGridSchema" }
--name - Sets the name of the event subscription
--source-resource-id - Specifies the resource to listen for events
--endpoint - Defines where to send the events
This command checks the details of the created Event Grid subscription to confirm it is set up correctly.
Terminal
az eventgrid event-subscription show --name storageBlobCreatedSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/exampleaccount
Expected OutputExpected
{ "id": "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/exampleaccount/providers/Microsoft.EventGrid/eventSubscriptions/storageBlobCreatedSubscription", "name": "storageBlobCreatedSubscription", "type": "Microsoft.EventGrid/eventSubscriptions", "destination": { "endpointType": "WebHook", "properties": { "endpointUrl": "https://myapp.azurewebsites.net/api/eventhandler" } }, "filter": { "includedEventTypes": [ "Microsoft.Storage.BlobCreated" ] }, "eventDeliverySchema": "EventGridSchema" }
--name - Specifies the subscription name to show
--source-resource-id - Specifies the source resource of the subscription
This command deletes the Event Grid subscription when it is no longer needed to avoid unnecessary event traffic and charges.
Terminal
az eventgrid event-subscription delete --name storageBlobCreatedSubscription --source-resource-id /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example-rg/providers/Microsoft.Storage/storageAccounts/exampleaccount
Expected OutputExpected
No output (command runs silently)
--name - Specifies the subscription name to delete
--source-resource-id - Specifies the source resource of the subscription
Key Concept

If you remember nothing else from this pattern, remember: Event Grid connects event sources to event handlers automatically, so your apps react instantly without polling.

Common Mistakes
Using the wrong resource ID for the source when creating the subscription
The subscription will not receive events because it listens to the wrong source.
Always copy the exact resource ID of the event source from Azure portal or CLI.
Not specifying the correct event types in the filter
You may receive unwanted events or miss important ones.
Specify only the event types your app needs to handle, like Microsoft.Storage.BlobCreated.
Using an incorrect or unreachable endpoint URL
Events will fail to deliver and may be retried or lost.
Use a valid, publicly accessible HTTPS endpoint that can accept Event Grid events.
Summary
Create an Event Grid subscription to route specific events from a source to a destination endpoint.
Verify the subscription details to ensure it listens to the correct events and sends them properly.
Delete the subscription when it is no longer needed to keep your environment clean.