Unveiling Azure Event Grid: An Exhaustive Guide from Basics to the Nitty-Gritty

Unveiling Azure Event Grid: An Exhaustive Guide from Basics to the Nitty-Gritty

1. Introduction

Azure Event Grid is an event-routing service that operates on the push model. It allows you to select the destination where you'd like the events delivered. This makes it easy to build applications with event-based architectures.

2. Fundamental Components

a) Event Sources

Think of these as the origin of an event. Azure Blob Storage generating an event when a blob is added is an example.

b) Event Handlers

These are the destinations that process the event. This could be an Azure Function or Logic App.

c) Topics

A topic is essentially a channel where events are sent.

d) Event Subscriptions

Rules that define how events are routed from topics to handlers.

3. Event Grid Topics: Beyond Basics

View Azure Event Grid metrics and set alerts - Azure Event Grid | Microsoft  Learn

a) System Topics: Managed by Azure, e.g., changes in Azure Subscriptions.

b) Custom Topics: Defined by users for their applications.

Example: Creating a topic using Azure SDK:

var eventGridManagementClient = new EventGridManagementClient(new TokenCredentials("YOUR-AZURE-AUTH-TOKEN"));
await eventGridManagementClient.Topics.CreateOrUpdateAsync("resourceGroupName", "YOUR-TOPIC-NAME", new Topic { Location = "westus2" });

4. Sending Events to a Topic

var topicCredentials = new TopicCredentials("YOUR-TOPIC-KEY");
var eventGridClient = new EventGridClient(topicCredentials);
var events = new List<EventGridEvent>
{
    new EventGridEvent
    {
        Id = Guid.NewGuid().ToString(),
        EventType = "recordInserted",
        Data = JsonConvert.SerializeObject(new { Field1 = "Value1", Field2 = "Value2" }),
        EventTime = DateTime.Now,
        Subject = "Database/Records",
        DataVersion = "2.0"
    }
};
eventGridClient.PublishEvents("YOUR-TOPIC-NAME.westus2-1.eventgrid.azure.net", events);

5. Event Subscriptions

Azure Event Grid - Subscribe to partner events - Azure Event Grid |  Microsoft Learn

Allows you to filter which events your handler receives.

Example: Subscribing a Logic App to a custom topic:

var eventSubscription = new EventSubscription
{
    Destination = new WebHookEventSubscriptionDestination
    {
        EndpointUrl = new Uri("YOUR-LOGIC-APP-URL")
    }
};
await eventGridManagementClient.EventSubscriptions.CreateOrUpdateAsync("resourceGroupName", "YOUR-SUBSCRIPTION-NAME", eventSubscription);

6. Advanced Features

How to Configure Azure Event Grid Domain Monitoring

a) Dead-lettering

This is crucial when event delivery fails. Dead-lettering stores the undelivered events for inspection.

var eventSubscription = new EventSubscription
{
    DeadLetterDestination = new StorageBlobDeadLetterDestination
    {
        BlobContainerName = "deadlettercontainer",
        ResourceId = "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Microsoft.Storage/storageAccounts/{storageAccountName}"
    }
};

b) Advanced Filtering

Instead of subscribing to all events, advanced filtering allows you to specify criteria to fine-tune your event handling.

var eventSubscription = new EventSubscription
{
    Filter = new EventSubscriptionFilter
    {
        AdvancedFilters = new[]
        {
            new StringContainsAdvancedFilter
            {
                Key = "Data.Field1",
                Values = new[] { "Value1" }
            }
        }
    }
};

c) Securing Events

Security in Azure Event Grid primarily uses Shared Access Signatures (SAS) or keys. A handshake mechanism ensures that only valid subscribers receive events.

For validation, Azure sends a validation token to your endpoint, which must be returned to complete the handshake. Here's an example using ASP.NET Core:

[HttpPost]
public IActionResult EventGridHandler([FromBody] EventGridEvent[] events)
{
    foreach (var eventGridEvent in events)
    {
        if (eventGridEvent.EventType == EventTypes.EventGridSubscriptionValidationEvent)
        {
            var validationCode = ((JObject)eventGridEvent.Data)["validationCode"].ToString();
            return Ok(new { validationResponse = validationCode });
        }
    }
    // Handle other events here.
    return Ok();
}

d) Monitoring and Diagnostics

To ensure optimal performance and reliability, regularly monitor your Event Grid. Azure provides integrated tools, like Azure Monitor, to help keep track of event delivery and system health.

e) Retry Policies

Azure Event Grid provides a retry policy to ensure event delivery. If a destination doesn't acknowledge an event, Event Grid retries sending the event based on the policy.

Configuring Retry Policy:

var eventSubscription = new EventSubscription
{
    Destination = new WebHookEventSubscriptionDestination
    {
        EndpointUrl = new Uri("YOUR-WEBHOOK-URL")
    },
    RetryPolicy = new RetryPolicy
    {
        MaxDeliveryAttempts = 5,
        EventTimeToLiveInMinutes = 30
    }
};

This policy will attempt to redeliver an event up to 5 times within 30 minutes.

f) Event Delivery Schemes

There are two main delivery schemes:

i) Event Grid Schema

It's the default schema. Events are wrapped into a standard Event Grid event schema.

ii) CloudEvent Schema v1.0

This is a standardized schema from the Cloud Native Computing Foundation.

When publishing, you can choose which schema to use. Mostly used is the Event Grid schema for Azure-based applications and CloudEvent when working in multi-cloud scenarios.

Publishing Example for CloudEvent Schema:

var cloudEvent = new CloudEvent
{
    Type = "recordInserted",
    Source = new Uri("/database/records"),
    Data = new { Field1 = "Value1" }
};
var client = new EventGridPublisherClient(new Uri("YOUR-EVENTGRID-ENDPOINT"), new AzureKeyCredential("YOUR-KEY"));
client.SendEvents(new List<CloudEvent> { cloudEvent });

g) Integration with Other Azure Services

Azure Event Grid integrates smoothly with several Azure Services. Some common integrations include:

i) Azure Functions:

Automatically trigger a function on a specific event.

[FunctionName("HandleGridEvent")]
public static void Run(
    [EventGridTrigger] EventGridEvent eventGridEvent,
    ILogger log)
{
    log.LogInformation(eventGridEvent.Data.ToString());
}

ii) Azure Logic Apps:

You can design workflows that are triggered by specific events. This typically involves using the Logic App Designer and selecting Event Grid as a trigger.

iii) Azure Blob Storage:

Generate events when blobs are added, deleted, or modified.

var storageAccount = CloudStorageAccount.Parse("YOUR-STORAGE-CONNECTION-STRING");
var blobClient = storageAccount.CreateCloudBlobClient();
var container = blobClient.GetContainerReference("mycontainer");
await container.CreateIfNotExistsAsync();
await container.SetPermissionsAsync(new BlobContainerPermissions
{
    PublicAccess = BlobContainerPublicAccessType.Off,
    SharedAccessPolicies = new SharedAccessPolicies<SharedAccessBlobPolicy>
    {
        {
            "eventgridpolicy",
            new SharedAccessBlobPolicy
            {
                SharedAccessExpiryTime = DateTime.UtcNow.AddHours(1),
                Permissions = SharedAccessBlobPermissions.Read
            }
        }
    }
});

After this, you can configure Event Grid to monitor the blob container for changes.

Conclusion

Azure Event Grid is undeniably at the heart of reactive programming in Azure. Its extensible nature, combined with the robust scalability of Azure, positions it as a keystone service for myriad applications. As developers strive to create more responsive and dynamic applications, understanding and mastering Azure Event Grid becomes paramount.

With these integrations, Azure Event Grid not only establishes itself as a central hub for reactive and event-driven programming in the Azure ecosystem but also as an enabler for seamless interactions across diverse Azure services. The convergence of these tools under one cloud platform is a testament to Azure's commitment to providing highly responsive, scalable, and efficient application solutions.


Thank you for reading this Blog. Hope you learned something new today! If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.

If you have some suggestions I am happy to learn with you.

I would love to connect with you on LinkedIn

Meet you in the next blog....till then Stay Safe ➕ Stay Healthy

#HappyLearning #devops #AzureEventGrid #EventDrivenArchitecture #AzureServices #CloudComputing #ReactiveProgramming #AzureFunctions #AzureLogicApps #CloudEvents #AzureBlobStorage #EventGridTopics #AzureIntegration #EventSubscriptions #DeadLettering #EventMonitoring #AzureSDK #AzureCloud #EventPublishing #AzureDeveloper #AzureTips #CloudNative