Saturday, September 14, 2024

Setup Azure function (ServiceBus and HTTP based trigger ) with Docker Container Image

 To setup Azure function we first need to choose the azure hosting plans.

  • Consumption Plan
  • Premium Plan
  • Dedicated Plan (App Service Plan)
In this blog, we will see how to create a Azure function using App Service plan and configured it with Docker container image.




























































Once you have your azure function created in azure you can start configuring triggers.
  • Create a file called function_app.py using VS code.
  • Add below code to it.

import azure.functions as func
import json
import logging
import time

app = func.FunctionApp()

#Function 1 - ServiceBus Triggered
@app.function_name('ServiceBusTrigger-Function')
@app.service_bus_queue_trigger(arg_name="message",
                               queue_name="my-servicebus-queue-name",
                               connection="ServiceBusConnectionString")
def main(message: func.ServiceBusMessage):
    logging.info(json.loads(message.get_body()))
   
    logging.info("Start: The time of code execution begin is : %s", time.ctime())
    time.sleep(300)
    # your processing logic
   
    logging.info("End : %s", time.ctime())


#Function 2 - Http Triggered
@app.function_name('HttpTrigger-Function')
@app.route(route="payload", auth_level=func.AuthLevel.ANONYMOUS)
def get_metadata(httpReq: func.HttpRequest) -> httpReq.HttpResponse:
    return func.HttpResponse(json.dumps(httpReq.get_body()))





















  •  local.settings.json file to store function parameters value to run azure function locally.
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=stg;AccountKey=abcdef==;EndpointSuffix=core.windows.net",
    "ServiceBusConnectionString": "Endpoint=sb://my-sbns.servicebus.windows.net/;SharedAccessKeyName=SharedAccessKey=abcdefghi=",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "my-servicebus-queue-name": "queue-name"
  }
}


  • We can use App Service - App Settings to configure the parameters values, Also we need to set Docker image related parameters for Deployment.



























[
  {
    "name": "APPLICATIONINSIGHTS_CONNECTION_STRING",
    "value": "InstrumentationKey=abcdef-4tgh-4567-jkih-tryrtyrtyr;IngestionEndpoint=https://ai.azure.com/;LiveEndpoint=https://es2.livediagnostics.monitor.azure.com/",
    "slotSetting": false
  },
  {
    "name": "AzureWebJobsStorage",
    "value": "DefaultEndpointsProtocol=https;AccountName=acctest;AccountKey=abcde==;EndpointSuffix=core.windows.net",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_PASSWORD",
    "value": "abcdefghijklmnopqrstuvwxyz",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_URL",
    "value": "https://mydemoacrtest.azurecr.io",
    "slotSetting": false
  },
  {
    "name": "DOCKER_REGISTRY_SERVER_USERNAME",
    "value": "mydemoacrtest",
    "slotSetting": false
  },
  {
    "name": "FUNCTIONS_EXTENSION_VERSION",
    "value": "~4",
    "slotSetting": false
  },
  {
    "name": "FUNCTIONS_WORKER_RUNTIME",
    "value": "python",
    "slotSetting": false
  },
  {
    "name": "ServiceBusConnectionString",
    "value": "Endpoint=sb://my-sbns.servicebus.windows.net/;SharedAccessKeyName=;SharedAccessKey=abcde=",
    "slotSetting": false
  },
  {
    "name": "WEBSITES_ENABLE_APP_SERVICE_STORAGE",
    "value": "false",
    "slotSetting": false
  }
]

This is the folder structure.























Thanks for reading, CloudOps Signing Off! 😊



No comments:

Post a Comment