Bird
Raised Fist0
Azurecloud~5 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What is the main purpose of Azure Event Grid in event-driven architecture?
easy
A. To store large amounts of data for analytics
B. To connect event sources to event handlers for instant reactions
C. To create virtual machines automatically
D. To manage user identities and access

Solution

  1. Step 1: Understand Event Grid's role

    Event Grid is designed to route events from sources to handlers quickly and efficiently.
  2. Step 2: Compare options with Event Grid's function

    Only To connect event sources to event handlers for instant reactions describes connecting event sources to handlers for instant reactions, which matches Event Grid's purpose.
  3. Final Answer:

    To connect event sources to event handlers for instant reactions -> Option B
  4. Quick Check:

    Event routing = Connect sources to handlers [OK]
Hint: Event Grid routes events instantly between sources and handlers [OK]
Common Mistakes:
  • Confusing Event Grid with data storage services
  • Thinking Event Grid manages virtual machines
  • Mixing Event Grid with identity management
2. Which Azure CLI command correctly creates an Event Grid subscription named mySub for a topic myTopic?
easy
A. az eventgrid event-subscription create --name mySub --source-resource-id /subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.EventGrid/topics/myTopic
B. az eventgrid event-subscription create --name mySub --source-resource-id myTopic
C. az eventgrid topic create --name mySub --resource-group myTopic
D. az eventgrid subscription create --topic myTopic --name mySub

Solution

  1. Step 1: Identify correct CLI syntax for event subscription creation

    The command requires the full resource ID for the source topic, not just the name.
  2. Step 2: Analyze options for correct parameters

    az eventgrid event-subscription create --name mySub --source-resource-id /subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.EventGrid/topics/myTopic uses the full resource ID format, which is required. az eventgrid event-subscription create --name mySub --source-resource-id myTopic uses only the topic name, which is invalid. Options C and D use incorrect commands or parameters.
  3. Final Answer:

    az eventgrid event-subscription create --name mySub --source-resource-id /subscriptions/{subId}/resourceGroups/{rg}/providers/Microsoft.EventGrid/topics/myTopic -> Option A
  4. Quick Check:

    Full resource ID needed for source in CLI [OK]
Hint: Use full resource ID for source in event-subscription create [OK]
Common Mistakes:
  • Using only topic name instead of full resource ID
  • Confusing topic creation with subscription creation
  • Using wrong CLI commands like 'subscription create'
3. Given this Azure CLI command:
az eventgrid event-subscription create --name sub1 --source-resource-id /subscriptions/123/resourceGroups/rg1/providers/Microsoft.EventGrid/topics/topic1 --endpoint https://myapp.com/api/events

What happens after running this command?
medium
A. The endpoint URL is registered as a new event source
B. A new Event Grid topic named 'sub1' is created
C. An event subscription named 'sub1' is created to send events from 'topic1' to the specified endpoint
D. The command fails due to missing resource group parameter

Solution

  1. Step 1: Understand the command purpose

    The command creates an event subscription named 'sub1' for the existing topic 'topic1'.
  2. Step 2: Analyze the effect of parameters

    The endpoint URL is where events will be delivered. The command does not create a topic or register an endpoint as a source.
  3. Final Answer:

    An event subscription named 'sub1' is created to send events from 'topic1' to the specified endpoint -> Option C
  4. Quick Check:

    Event subscription sends events to endpoint [OK]
Hint: Event subscription links topic events to endpoint URL [OK]
Common Mistakes:
  • Thinking the command creates a new topic
  • Assuming endpoint is an event source
  • Believing resource group is mandatory in this command
4. You wrote this Azure CLI command to create an Event Grid subscription:
az eventgrid event-subscription create --name sub2 --source-resource-id myTopic --endpoint https://myapp.com/api/events

But it fails with an error. What is the most likely cause?
medium
A. The source resource ID must be a full Azure resource ID, not just the topic name
B. The endpoint URL must be an Azure service URL
C. The subscription name cannot contain numbers
D. The command requires a resource group parameter

Solution

  1. Step 1: Check the source-resource-id parameter format

    The source-resource-id must be the full Azure resource ID, not just the topic name.
  2. Step 2: Validate other parameters

    The endpoint can be any valid HTTPS URL. Subscription names can include numbers. Resource group is not required here if full resource ID is used.
  3. Final Answer:

    The source resource ID must be a full Azure resource ID, not just the topic name -> Option A
  4. Quick Check:

    Full resource ID needed for source-resource-id [OK]
Hint: Always use full resource ID for source-resource-id [OK]
Common Mistakes:
  • Using only topic name instead of full resource ID
  • Thinking endpoint must be Azure URL
  • Believing subscription names cannot have numbers
5. You want to build an app that reacts instantly when a new blob is uploaded to Azure Storage. Which Event Grid setup is best to achieve this?
hard
A. Create an Event Grid subscription on the Storage Account's blob deleted event
B. Create a Storage Queue and poll it every minute for new blobs
C. Use Azure Functions timer trigger to check blobs hourly
D. Create an Event Grid subscription on the Storage Account's blob created event, with your app's webhook as endpoint

Solution

  1. Step 1: Identify event source and event type

    The Storage Account emits blob created events when new blobs upload.
  2. Step 2: Choose subscription and endpoint

    Creating an Event Grid subscription for blob created events with your app's webhook endpoint enables instant reaction.
  3. Step 3: Evaluate other options

    Polling or timer triggers cause delays. Subscribing to blob deleted events does not detect new uploads.
  4. Final Answer:

    Create an Event Grid subscription on the Storage Account's blob created event, with your app's webhook as endpoint -> Option D
  5. Quick Check:

    Blob created event subscription = instant reaction [OK]
Hint: Subscribe to blob created events for instant upload notifications [OK]
Common Mistakes:
  • Using polling instead of event-driven subscription
  • Subscribing to wrong event type like blob deleted
  • Relying on timer triggers causing delays