Introduction :
In part 1, we saw what is a claim check pattern, useful scenarios in which the pattern is implemented, and different approaches to implementing the pattern in Azure. In this blog post, I will implement the pattern using one of the approaches.
Prerequisites :
Please go through https://exploreazurecloud.com/2021/06/implement-claim-check-pattern-in-azure-part-1/ before you read this blog post.
If you want to try implementing this pattern practically, You need to have an Azure subscription, create an Azure Free Account using this link https://azure.microsoft.com/en-in/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio
Practical Implementation of Claim Check Pattern in Azure using Azure Storage Blob, Azure Event Grid System Topic, and Azure Logic App :
Sign in to the Azure Account and then first create a Storage Account if not created already. Use this link and createt the same https://docs.microsoft.com/en-us/azure/storage/common/storage-account-create?tabs=azure-portal
Once Created , Storage Account Overview page will look like the below .
3. Next Step is to create a Blob Container where the Original Message will be uploaded by the Client . You can create it by clicking on the blob service tab and then you will have create container button . I already created eventcontainer for the same.
4. Now that we have a Blob Container where the file gets uploaded, We need to set up the Event Handling Mechanism to poll these messages from the blob container whenever a new message is uploaded.
5. We have Azure Storage events that allow applications to react to events, such as the creation and deletion of blobs. We do not need complicated code or expensive and inefficient polling services. The best part is you only pay for what you use.
6. If you are using the Event Grids for the first time in your subscription ,we have to verify if the Event Grid Resource Provider is registered. In order to check the same , please go to Subscription ---> Settings -----> Resource Providers and find Microsoft.EventGrid . It should be in Registered Status , if not Register it.
7. Before we Subscribe to the events for the Blob Storage , We need to create a messaging endpoint for the event message. I created a Logic App with HTTP trigger as messaging endpoint to consume the event message . Please fidn the screenshot below .
8. Now that we have a messaging endpoint , Let us setup the Event Subscription Configuration by going to Events Section under Azure Storage Account where the Blob Container is Created.
9. Click on the Event Subscriptions option , On the Event Subscription Page do the following steps.
a) Enter Name for the Event Subscription .
b) Enter a name for the system topic
c) Once after you provide Event Name and System Topic Name in this page , you have Event Type Configuration Filter , Select Blob Created Event under that like mentioned below .
d) Under Endpoint Details, Select the Endpoint type as Webhook and copy the http request trigger URL from the Logic App Created in Step 6 and paste it under Subscriber Endpoint URL.
e) Now go to filters tab under Event Subscription page, Under Filters, we need to enable Subject Filtering and Add the below filter under Subject begins with Property.
/blobServices/default/containers/eventcontainer/
General Syntax is : /blobServices/default/containers/{blobcontainername}
f) This filter is setup to send blob storage creation event information to the Azure System Event Grid only when the message is uploaded on the blob container named "Event Container" . In Practical Scenarios too , We usually expose a blob container to the Client using SAS URI to dump their messages. If we do not setup this Subject Filter , Event Grid will receive the message for any blob creates happening on any container in ths storage account.
Please be noted that Subject Ends with is not configured to any value . ".jpg" is actually blurred and it is a reference for the developer to indicate that this property is for filtering specific file extensions . I didn't add any specific value for this property.
10. Once after all the configurations are finished , this is how the Event Subscription would look .
Filters Section :
- Now we are all set to test the pattern, upload a file into the blob container you created earlier. I am using a small file as this is a Proof of Concept. Ideally, this pattern is for dealing with large messages.
- The Moment you dump a file into the blob container, it is treated as an Event and this Blob Creation Metadata flows to the System Event Grid and then to the Event Subscription.
- We can see the message is successfully picked up by the Event Subscription and it is delivered to the message endpoint i.e Azure Logic app. Let us go to Azure Logic App Run history for the confirmation.
- We can see from the run that Blob Metadata is received on the Azure Logic App Successfully.
Blob Event Data Received on the Logic App :
Key Details like a topic, event subject filter, event type, and blob uri details are received from the Event Subscription to Logic App.
Now using url attribute which contains blob container details, we can easily retrieve the original message by connecting to the blob storage connector. So this step is added in the logic app.
Blob Content is retrieved using Blob Storage Connector.
- Finally a Compose Action is added to retrieve the Original Message of the Blob by Converting the application/octet-stream content to JSON using the below simple expression.
json(body('Get_blob_content_using_path'))
Conclusion :
With this, Practical implementation of a claim check pattern in Microsoft Azure is demonstrated using one of the approaches successfully.
References :
https://docs.microsoft.com/en-us/azure/storage/blobs/storage-blob-event-overview