Skip to content

Trigger function with HTTP events and store in cosmosdb and blob storage

Notifications You must be signed in to change notification settings

miztiik/azure-http-trigger-function-to-cosmos

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HTTP Trigger Event for Azure Functions & store in CosmosDB

The developers at Mystique Unicorn [process files][6] as soon [as they arrive][7].They want to switch to an event-driven architecture. They were looking for a custom trigger, whether it be payload-based or time-based, to process files efficiently.

They heard about Azure's capabilities for user managed identities. Can you help them implement this event processing at Mystique Unicorn?

Miztiik Automation - HTTP Trigger Event for Azure Functions & store in CosmosDB

🎯 Solution

Our solution enables seamless event processing using Azure Functions and HTTP triggers. With send a event payload to perform some action. For example, the payload could contain the name of a blob to be processed. The function can then retrieve the blob and process it accordingly. Our solution also includes an output binding to persist the processed event back to Blob Storage & also to 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-http-trigger-function-to-cosmos
      cd azure-http-trigger-function-to-cosmos
  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 processed events
      • Python Azure Function
        • Input, Trigger, Output Binding to the blob container & Cosmos DB
      • Cosmos DB
        • This will be used to store the processed events
      # make deploy
      sh deployment_scripts/deploy.sh

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

      Miztiik Automation - HTTP Trigger Event for Azure Functions & store in CosmosDB

  5. 🔬 Testing the solution

    • Upload file(s) to blob

      Currently the function is designed to generate `1`` event as there a limitaion on python bindings to blob & cosmos db. If you do not know how many output blobs or cosmos items will be create by your function, then you will have to use the SDK instead of bindings.

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

      # Set variables
      URL="https://store-backend-fnapp-006.azurewebsites.net/api/store-events-consumer-fn?count=1"
      $ curl $URL

      You should see an output like this,

      Processed Event: {"miztiik_event_processed": true, "msg": "", "count": 1, "resp": {"status": true, "tot_msgs": 1, "bad_msgs": 0, "sale_evnts": 0, "inventory_evnts": 1, "tot_sales": 68.17}, "last_processed_on": "2023-05-14T10:55:54.964561"}
      

      Miztiik Automation - HTTP Trigger Event for Azure Functions & store in CosmosDB

      Miztiik Automation - HTTP Trigger Event for Azure Functions & store in CosmosDB

  6. 📒 Conclusion

    Here we have demonstrated how to use Azure functions triggered by HTTP events and persist data to blob and cosmosDB.

  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 Functions HTTP Trigger
  2. Azure Blob Storage Ouput Binding
  3. Azure Cosmos Ouput Binding
  4. Azure Binding Limitations Issue
  5. [][5]
  6. [Miztiik Blog - Blob Storage Event Processing with Python Azure Functions][6]
  7. [Miztiik Blog - Blob Storage Processing with Python Azure Functions with HTTP Triggers][7]

[5]: [6]: https://github.com/miztiik/azure-blob-trigger-python-function [7]: https://github.com/miztiik/azure-blob-input-binding-to-function

🏷️ Metadata

miztiik-success-green

Level: 200