User Tools

Site Tools


cloud:azure:functions

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
cloud:azure:functions [2024/04/27 17:49] skipidarcloud:azure:functions [2024/05/19 14:36] (current) skipidar
Line 5: Line 5:
   * Set up IDE https://learn.microsoft.com/de-de/azure/azure-functions/functions-develop-vs-code?tabs=node-v4%2Cpython-v2%2Cisolated-process&pivots=programming-language-python   * Set up IDE https://learn.microsoft.com/de-de/azure/azure-functions/functions-develop-vs-code?tabs=node-v4%2Cpython-v2%2Cisolated-process&pivots=programming-language-python
   * Core Tools installieren https://learn.microsoft.com/de-de/azure/azure-functions/functions-run-local?tabs=windows%2Cisolated-process%2Cnode-v4%2Cpython-v2%2Chttp-trigger%2Ccontainer-apps&pivots=programming-language-python#install-the-azure-functions-core-tools   * Core Tools installieren https://learn.microsoft.com/de-de/azure/azure-functions/functions-run-local?tabs=windows%2Cisolated-process%2Cnode-v4%2Cpython-v2%2Chttp-trigger%2Ccontainer-apps&pivots=programming-language-python#install-the-azure-functions-core-tools
 +
 +Use to reload the env variables after installing
 +<sxh python>
 +$env:Path = `
 +[System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + `
 +[System.Environment]::GetEnvironmentVariable("Path","User")
 +</sxh>
  
  
Line 19: Line 26:
 {{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/tzHBRiMKL5.png}} {{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/tzHBRiMKL5.png}}
  
-Use to reload the env variables after installing +
-<sxh python> +
-$env:Path = ` +
-[System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + ` +
-[System.Environment]::GetEnvironmentVariable("Path","User"+
-</sxh>+
  
 Then even under windows one can run "func start" to test linux functions Then even under windows one can run "func start" to test linux functions
Line 33: Line 35:
  
 {{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/4HLTFBqBlR.png}} {{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/4HLTFBqBlR.png}}
 +
 +
 +=== Debugging Azure functions ===
 +
 +
 +== Validate Triggers==
 +
 +Often, if the "Azure Function" code has invalid imports,
 +then the triggers are missing after the deployment.
 +
 +You can validate, **if the triggers were recognized successfully**, by starting the Azure Function locally.
 +
 +The recognized triggers will be listed as following:
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/4HLTFBqBlR.png}}
 +
 +
 +
 +== Validate Files ==
 +
 +Often the files of Azure Function - are not deployed or lost after the terraform-update.
 +
 +Check if the files are available and up to date.
 +
 +Maybe teh reason why the trigger isnt there - is because you have not deplyoed the app?
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/41O4sn3oKW.png}}
 +
 +
 +== Test execute and see output ==
 +
 +You can execute the Azure functions from the portal.
 +
 +And you can validate the behavior via "Portal > Azure Function > Log Stream"
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/MbDC47HSC7.png}}
 +
 +
 +
 +== Validate Imports ==
 +
 +Just do, to see if the imports are understood correctly:
 +
 +<sxh python>
 +pip install -r .\requirements.txt
 +</sxh>
 +
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/7ygwRpbbkd.png}}
 +
 +
 +
 +
 +
  
 === Azure functions in Azure Portal === === Azure functions in Azure Portal ===
Line 100: Line 156:
 IN general read about developing locally: IN general read about developing locally:
 https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local https://learn.microsoft.com/en-us/azure/azure-functions/functions-develop-local
 +
 +
 +
 +=== Azure Service Bus Pub/Sub ===
 +
 +Publishing function
 +<sxh python>
 +import logging
 +import azure.functions as func
 +
 +app = func.FunctionApp()
 +
 +# vs output into queue for python
 +# https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-language-python
 +
 +@app.route(route="http_trigger_queue", auth_level=func.AuthLevel.ANONYMOUS)
 +@app.service_bus_queue_output(arg_name="message",
 +                              connection="ServiceBusConnection",
 +                              queue_name="alfdevapi5servicebusqueue")
 +def http_trigger_queue(req: func.HttpRequest, message: func.Out[str]) -> func.HttpResponse:
 +    logging.info('Python HTTP trigger function processed a request.')
 +
 +    logging.info('Python HTTP trigger function processed a request.')
 +    myMessage = "Hi alf this is my message via the queue to you."
 +    logging.info(myMessage)
 +    
 +    input_msg = req.params.get('message')
 +    message.set(f"{myMessage} {input_msg}")
 +
 +    return func.HttpResponse(
 +        "This function should process queue messages.",
 +        status_code=200
 +    )
 +</sxh>
 +
 +
 +Subscribing function
 +<sxh python>
 +import logging
 +import azure.functions as func
 +
 +app = func.FunctionApp()
 +
 +
 +@app.service_bus_queue_trigger(arg_name="azservicebus", queue_name="alfdevapi5servicebusqueue",
 +                               connection="ServiceBusConnection"
 +def servicebus_trigger(azservicebus: func.ServiceBusMessage):
 +    logging.warn('Python ServiceBus Queue trigger processed a message: %s',
 +                azservicebus.get_body().decode('utf-8'))
 +</sxh>
 +
 +Here you can see, that when the publishing function is executed, via Test
 +
 +Then the consuming function is receiving the message from the queue.
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/MbDC47HSC7.png}}
 +
 +
 +=== Azure Functions Java ===
 +
 +https://stackoverflow.com/questions/77360325/cannot-create-java-function-in-azure-function-app
 +
 +<sxh java>
 +mvn clean install
 +mvn azure-functions:run
 +mvn azure-functions:deploy
 +</sxh>
 +
 +Java SDK content
 +  * https://azure.github.io/azure-sdk-for-java/servicebus.html
 +  * https://learn.microsoft.com/en-us/java/api/com.azure.messaging.servicebus.servicebusclientbuilder?view=azure-java-stable
 +  * 
 +
 +
 +=== Azure Service Topic Pub with session ===
 +
 +requirements.txt
 +<sxh python>
 +# DO NOT include azure-functions-worker in this file
 +# The Python Worker is managed by Azure Functions platform
 +# Manually managing azure-functions-worker may cause unexpected issues
 +
 +azure-functions
 +datetime
 +azure-servicebus
 +
 +</sxh>
 +
 +local.settings.json
 +<sxh python>
 +
 +{
 +  "IsEncrypted": false,
 +  "Values": {
 +    "AzureWebJobsStorage": "",
 +    "FUNCTIONS_WORKER_RUNTIME": "python",
 +    "AzureWebJobsFeatureFlags": "EnableWorkerIndexing",
 +    "ServiceBusConnection": "Endpoint=sb://alfdevapi6sb.servicebus.windows.net/;SharedAccessKeyName=alfdevapi6servicebus_auth_rule;SharedAccessKey=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx="
 +  }
 +}
 +
 +</sxh>
 +
 +
 +Python V2 app, which uses the Python SDK, to send messages with SessionId
 +
 +  * [[https://learn.microsoft.com/en-us/azure/developer/python/sdk/azure-sdk-overview?view=azure-python|Use the Azure libraries (SDK) for Python]]
 +  *  [[https://learn.microsoft.com/en-us/python/api/azure-servicebus/azure.servicebus.servicebusclient?view=azure-python|ServiceBusClient Class]]
 +
 +<sxh python>
 +import logging
 +import azure.functions as func
 +from datetime import datetime
 +import json
 +from azure.servicebus import ServiceBusClient, ServiceBusMessage
 +import os
 +
 +app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS)
 +
 +# vs output into queue for python
 +# https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-output?tabs=python-v2%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-language-python
 +
 +
 +TOPIC_NAME_A = "alfdevapi6topic" 
 +CONN_STRING = os.environ['ServiceBusConnection']
 +SESSION_ID = "008"
 +
 +
 +@app.route(route="http_trigger")
 +def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
 +    logging.info('Python HTTP trigger function processed a request.')
 +    myMessage = "Hi alf this is my message via the queue to you."
 +
 +    jsn_message_envelope =  generateMessage(myMessage, SESSION_ID)
 +    logging.info(f"Will send envelope: {jsn_message_envelope}")
 +
 +    servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONN_STRING)
 +    rith_sen = servicebus_client.get_topic_sender(TOPIC_NAME_A)
 +    rith_msg = ServiceBusMessage(jsn_message_envelope)
 +    rith_msg.session_id = SESSION_ID
 +    with rith_sen:
 +        rith_sen.send_messages(rith_msg)
 +    servicebus_client.close()
 +
 +
 +    return func.HttpResponse(
 +            "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
 +            status_code=200
 +    )
 +
 +
 +def generateMessage(myMessage, mySessionId):
 +    now = datetime.now()
 +    print("now =", now)
 +    logging.info(f"Time stamp: {now}")
 +
 +    dt_string = now.strftime("%d/%m/%Y %H:%M:%S")
 +
 +    my_json_string = f"""
 +    {{
 +        "body": "{myMessage}",
 +        "customProperties": {{
 +            "timePublish": "{dt_string}",
 +        }},
 +        "brokerProperties": {{
 +            "SessionId": "{mySessionId}"
 +        }}
 +    }}
 +    """
 +
 +    return my_json_string
 +
 +</sxh>
 +
 +== Publish a test message ==
 +
 +Publishing a message
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/tVGGBpwi0d.png}}
 +
 +And see, which session was used during publishing, here ''session1''
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/AegrzHZAfS.png}}
 +
 +== Consuming a test message ==
 +
 +Now a consumer can consume the message
 +
 +{{https://s3.eu-central-1.amazonaws.com/alf-digital-wiki-pics/sharex/at6eSPYnFS.png}}
 +
 +
cloud/azure/functions.1714240189.txt.gz · Last modified: by skipidar