Skip to content

Process Azure Blob events with eventgrid using function and persist in Cosmos & Blob using bindings

Notifications You must be signed in to change notification settings

miztiik/azure-blob-trigger-cosmos-binding-function

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Azure Blob processing with Event Grid Triggers & Azure Functions Persisted to CosmosDB

The developers at Mystique Unicorn process files as soon as they arrive. They are using a client library to upload files to Cosmos DB. They have heard there are easier way to do this. Can you help them implement this event processing at Mystique Unicorn?

Miztiik Automation - Azure Blob processing with Event Grid Triggers & Azure Functions Persisted to CosmosDB

🎯 Solution

Our solution leverages Azure Function bindings to Cosmos. lets you write a new document to an Azure Cosmos DB database using the SQL API without having to manage clients. he binding also manages opening and closing connections as necessary. We can implement event processing on Azure Blob Storage through the use of Azure Functions and Event Grid Triggers using user managed identity. The Azure blob storage events for events like blob creation and deletion can be used to trigger the function. A sample event from event grid is shown below,

{
  "id": "538fcf9f-3..-1024-801417067d3a",
  "data": {
    "api": "PutBlob",
    "clientRequestId": "c0c0f290-ec..0bc9ef3b",
    "requestId": "538fcf9f-3..01417000000",
    "eTag": "0x8DB4E3BA4F8E488",
    "contentType": "application/json",
    "contentLength": 40,
    "blobType": "BlockBlob",
    "url": "https://warehouse6p5crf002.blob.core.windows.net/store-events-blob-002/source/7031_2023-05-06_event.json",
    "sequencer": "0000000000000000000000.000005276ba",
    "storageDiagnostics": { "batchId": "2901e730-b..-80d271000000" }
  },
  "topic": null,
  "subject": "/blobServices/default/containers/store-events-blob-002/blobs/source/7031_2023-05-06_event.json",
  "event_type": null
}

We can use this event as a trigger, retrieve the corresponding blob mentioned in data.url using the input binding and persist the processed event back to Blob Storage using the output binding and cosmos DB.

By leveraging the power of Bicep, all necessary resources can be easily provisioned and managed with minimal effort. Our solution uses Python for efficient event processing, allowing for quick and easy deployment of sophisticated event processing pipelines.

  1. 🧰 Prerequisites

    This demo, instructions, scripts and bicep template is designed to be run in northeurope. With few or no modifications you can try it out in other regions as well(Not covered here).

  2. ⚙️ Setting up the environment

    • Get the application code

      https://github.com/miztiik/azure-blob-trigger-cosmos-binding-function
      cd azure-blob-trigger-cosmos-binding-function
  3. 🚀 Prepare the environment

    Let check you have Azure Cli working with

      # You should have azure cli preinstalled
      az account show

    You should see an output like this,

     {
       "environmentName": "AzureCloud",
       "homeTenantId": "16b30820b6d3",
       "id": "1ac6fdbff37cd9e3",
       "isDefault": true,
       "managedByTenants": [],
       "name": "YOUR-SUBS-NAME",
       "state": "Enabled",
       "tenantId": "16b30820b6d3",
       "user": {
         "name": "miztiik@",
         "type": "user"
       }
     }
  4. 🚀 Deploying the application

    • Stack: Main Bicep This will create the following resoureces

      • General purpose Storage Account
        • This will be used by Azure functions to store the function code
      • Storage Account with blob container
        • This will be used to store the events
      • Event Grid Topic
        • This will be used to trigger the Azure Function.
        • Create a subscription to the topic, that filters for Microsoft.Storage.BlobCreated events specific to the blob container.
      • Python Azure Function
        • Input, Trigger, Output Binding to the blob container for events
      • Cosmos DB
        • This will be used to store the processed events
        • Output binding from function to cosmos db
      # make deploy
      sh deployment_scripts/deploy.sh

      After successfully deploying the stack, Check the Resource Groups/Deployments section for the resources.

  5. 🔬 Testing the solution

    • Upload file(s) to blob

      Get the storage account and container name from the output of the deployment. Upload a file to the container and check the logs of the function app to see the event processing in action.

      Sample bash script to upload files to blob container. You can also upload manually from the portal,

      # Set variables
      RESOURCE_GROUP="Miztiik_Enterprises_azure_blob_eventgrid_trigger_function_002"
      LOCATION="northeurope"
      SA_NAME="warehouse6p5crf002"
      CONTAINER_NAME="store-events-blob-002"
      
      for i in {1..2}
      do
      FILE_NAME_PREFIX=$(openssl rand -hex 4)
      FILE_NAME="${RANDOM}_$(date +'%Y-%m-%d')_event.json"
      echo -n "{\"message\": \"hello world on $(date +'%Y-%m-%d')\" , \"timestamp\": \"$(date -u +"%Y-%m-%dT%H:%M:%SZ")\"}" > ${FILE_NAME} 
      az storage blob upload \
        --account-name ${SA_NAME} \
        --container-name ${CONTAINER_NAME} \
        --name "source/${FILE_NAME}" \
        --file ${FILE_NAME} \
        --auth-mode login
      
      sleep 2
      echo -e "\n\n---> ${FILE_NAME} uploaded to ${CONTAINER_NAME} in ${SA_NAME} storage account\n\n"
      done

      You should see an output like this,

      ---> 27999_2023-05-06_event.json uploaded to store-events-blob-002 in warehouse6p5crf002 storage account

      Miztiik Automation - Azure Blob processing with Event Grid Triggers & Azure Functions Persisted to CosmosDB

      Miztiik Automation - Azure Blob processing with Event Grid Triggers & Azure Functions Persisted to CosmosDB

      Miztiik Automation - Azure Blob processing with Event Grid Triggers & Azure Functions Persisted to CosmosDB

  6. 📒 Conclusion

    Here we have demonstrated trigger Azure functions with event grid trigger and process blob files. You can extend the solution and configure the function to send the events to other services like Event Hub, Service Bus, etc.

  7. 🧹 CleanUp

If you want to destroy all the resources created by the stack, Execute the below command to delete the stack, or you can delete the stack from console as well

# Delete from resource group
az group delete --name Miztiik_Enterprises_xxx --yes
# Follow any on-screen prompt

This is not an exhaustive list, please carry out other necessary steps as maybe applicable to your needs.

📌 Who is using this

This repository aims to show how to Bicep to new developers, Solution Architects & Ops Engineers in Azure.

💡 Help/Suggestions or 🐛 Bugs

Thank you for your interest in contributing to our project. Whether it is a bug report, new feature, correction, or additional documentation or solutions, we greatly value feedback and contributions from our community. Start here

👋 Buy me a coffee

ko-fi Buy me a coffee ☕.

📚 References

  1. Azure Event Grid trigger for Azure Functions
  2. Blob Storage events
  3. Azure Blob Storage Input Binding
  4. Azure Blob Storage Ouput Binding
  5. Azure Event Grid Filters
  6. Miztiik Blog - Blob Storage Event Processing with Python Azure Functions
  7. Miztiik Blog - Blob Storage Processing with Python Azure Functions with HTTP Triggers
  8. Azure CosmosDB Output Binding for Azure Functions

🏷️ Metadata

miztiik-success-green

Level: 200

About

Process Azure Blob events with eventgrid using function and persist in Cosmos & Blob using bindings

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Sponsor this project

Packages

No packages published

Languages