Skip to content

Orchestrating Events with Azure Service Bus and Azure Functions using Managed Identity

Notifications You must be signed in to change notification settings

miztiik/azure-function-producer-to-service-bus

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Orchestrating Events with Azure Service Bus and Azure Functions using Managed Identity

Mystique Unicorn's developers are seeking assistance in implementing event processing for orchestrating events across multiple applications and services using Azure's native capabilities. Can you provide guidance on how to accomplish this?

🎯 Solution

We can leverage Azure Service Bus and the Python SDK for orchestrating events. Azure Service Bus provides a reliable messaging platform for event orchestration. In this demo, an Azure Function with a managed identity will produce events and send them to a queue within a specific service bus namespace. The managed identity is exclusively scoped to this service bus namespace. By leveraging the power of Bicep, all necessary resources can be easily provisioned and managed with minimal effort.

Note: Azure functions need this AZURE_CLIENT_ID environment variable for getting the auth token from AAD, Thanks to this github issue and this one here for the solution.

Our function will generate the following event with custom properties and also set the time to live to 1 day.

{
  "id": "743da362-69df-4e63-a95f-a1d93e29825e",
  "request_id": "743da362-69df-4e63-a95f-a1d93e29825e",
  "store_id": 5,
  "store_fqdn": "localhost",
  "store_ip": "127.0.0.1",
  "cust_id": 549,
  "category": "Notebooks",
  "sku": 169008,
  "price": 45.85,
  "qty": 34,
  "discount": 10.3,
  "gift_wrap": true,
  "variant": "red",
  "priority_shipping": false,
  "ts": "2023-05-19T14:36:09.985501",
  "contact_me": "github.com/miztiik",
  "is_return": true
}

Message custom properties,

{
   "event_type":"sale_event",
   "priority_shipping":false,
}

Miztiik Automation - Orchestrating Events with Azure Service Bus and Azure Functions using Managed Identity

  1. 🧰 Prerequisites

    This demo, instructions, scripts and bicep template is designed to be run in westeurope. 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-function-producer-to-service-bus
      cd azure-function-producer-to-service-bus
  3. 🚀 Prepare the environment

    Ensure you have Azure Cli and bicep working

    # You should have azure cli preinstalled
    bicep --version
    az account show
  4. 🚀 Deploying the application

    • Stack: Main Bicep We will create the following resoureces

      • General purpose Storage Account
        • This will be used by Azure functions to store the function code
      • Servie Bus Namespace
        • Service Bus Queue
      • Managed Identity
        • This will be used by the Azure Function to interact with the service bus
      • Python Azure Function
        • HTTP Trigger
      # 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

    • Trigger the function

      FUNC_URL="https://fn-producer-**to**-svc-bus-store-backend-fn-app-008.azurewebsites.net/api/store-events-producer-fn"
      curl ${FUNC_URL}?count=5

      You should see an output like this,

      {
         "miztiik_event_processed": true,
         "msg": "Generated 5 messages",
         "resp": {
            "status": true,
            "tot_msgs": 5,
            "bad_msgs": 0,
            "sale_evnts": 3,
            "inventory_evnts": 2,
            "tot_sales": 152.66
         },
         "count": 5,
         "last_processed_on": "2023-05-19T17:17:46.440429"
      }

      Miztiik Automation - Orchestrating Events with Azure Service Bus and Azure Functions using Managed Identity

  6. 📒 Conclusion

    Here we have demonstrated how to use Azure functions to process send messages to Azure Service Bus.

  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 Docs - Managed Identity
  2. Azure Docs - Managed Identity Caching
  3. Gitub Issue - Default Credential Troubleshooting
  4. Gitub Issue - Default Credential Troubleshooting

🏷️ Metadata

miztiik-success-green

Level: 200