cloud:azure:functions
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
cloud:azure:functions [2024/04/27 17:50] – skipidar | cloud:azure:functions [2024/05/19 14:36] (current) – skipidar | ||
---|---|---|---|
Line 35: | Line 35: | ||
{{https:// | {{https:// | ||
+ | |||
+ | |||
+ | === Debugging Azure functions === | ||
+ | |||
+ | |||
+ | == Validate Triggers== | ||
+ | |||
+ | Often, if the "Azure Function" | ||
+ | then the triggers are missing after the deployment. | ||
+ | |||
+ | You can validate, **if the triggers were recognized successfully**, | ||
+ | |||
+ | The recognized triggers will be listed as following: | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | |||
+ | |||
+ | == 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:// | ||
+ | |||
+ | |||
+ | == Test execute and see output == | ||
+ | |||
+ | You can execute the Azure functions from the portal. | ||
+ | |||
+ | And you can validate the behavior via " | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | |||
+ | |||
+ | == Validate Imports == | ||
+ | |||
+ | Just do, to see if the imports are understood correctly: | ||
+ | |||
+ | <sxh python> | ||
+ | pip install -r .\requirements.txt | ||
+ | </ | ||
+ | |||
+ | |||
+ | {{https:// | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
=== Azure functions in Azure Portal === | === Azure functions in Azure Portal === | ||
Line 102: | Line 156: | ||
IN general read about developing locally: | IN general read about developing locally: | ||
https:// | https:// | ||
+ | |||
+ | |||
+ | |||
+ | === 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:// | ||
+ | |||
+ | @app.route(route=" | ||
+ | @app.service_bus_queue_output(arg_name=" | ||
+ | connection=" | ||
+ | queue_name=" | ||
+ | def http_trigger_queue(req: | ||
+ | logging.info(' | ||
+ | |||
+ | logging.info(' | ||
+ | myMessage = "Hi alf this is my message via the queue to you." | ||
+ | logging.info(myMessage) | ||
+ | | ||
+ | input_msg = req.params.get(' | ||
+ | message.set(f" | ||
+ | |||
+ | return func.HttpResponse( | ||
+ | "This function should process queue messages.", | ||
+ | status_code=200 | ||
+ | ) | ||
+ | </ | ||
+ | |||
+ | |||
+ | Subscribing function | ||
+ | <sxh python> | ||
+ | import logging | ||
+ | import azure.functions as func | ||
+ | |||
+ | app = func.FunctionApp() | ||
+ | |||
+ | |||
+ | @app.service_bus_queue_trigger(arg_name=" | ||
+ | | ||
+ | def servicebus_trigger(azservicebus: | ||
+ | logging.warn(' | ||
+ | azservicebus.get_body().decode(' | ||
+ | </ | ||
+ | |||
+ | 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:// | ||
+ | |||
+ | |||
+ | === Azure Functions Java === | ||
+ | |||
+ | https:// | ||
+ | |||
+ | <sxh java> | ||
+ | mvn clean install | ||
+ | mvn azure-functions: | ||
+ | mvn azure-functions: | ||
+ | </ | ||
+ | |||
+ | Java SDK content | ||
+ | * https:// | ||
+ | * https:// | ||
+ | * | ||
+ | |||
+ | |||
+ | === 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 | ||
+ | |||
+ | </ | ||
+ | |||
+ | local.settings.json | ||
+ | <sxh python> | ||
+ | |||
+ | { | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | } | ||
+ | } | ||
+ | |||
+ | </ | ||
+ | |||
+ | |||
+ | Python V2 app, which uses the Python SDK, to send messages with SessionId | ||
+ | |||
+ | * [[https:// | ||
+ | * [[https:// | ||
+ | |||
+ | <sxh python> | ||
+ | import logging | ||
+ | import azure.functions as func | ||
+ | from datetime import datetime | ||
+ | import json | ||
+ | from azure.servicebus import ServiceBusClient, | ||
+ | import os | ||
+ | |||
+ | app = func.FunctionApp(http_auth_level=func.AuthLevel.ANONYMOUS) | ||
+ | |||
+ | # vs output into queue for python | ||
+ | # https:// | ||
+ | |||
+ | |||
+ | TOPIC_NAME_A = " | ||
+ | CONN_STRING = os.environ[' | ||
+ | SESSION_ID = " | ||
+ | |||
+ | |||
+ | @app.route(route=" | ||
+ | def http_trigger(req: | ||
+ | logging.info(' | ||
+ | myMessage = "Hi alf this is my message via the queue to you." | ||
+ | |||
+ | jsn_message_envelope = generateMessage(myMessage, | ||
+ | logging.info(f" | ||
+ | |||
+ | 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, | ||
+ | now = datetime.now() | ||
+ | print(" | ||
+ | logging.info(f" | ||
+ | |||
+ | dt_string = now.strftime(" | ||
+ | |||
+ | my_json_string = f""" | ||
+ | {{ | ||
+ | " | ||
+ | " | ||
+ | " | ||
+ | }}, | ||
+ | " | ||
+ | " | ||
+ | }} | ||
+ | }} | ||
+ | """ | ||
+ | |||
+ | return my_json_string | ||
+ | |||
+ | </ | ||
+ | |||
+ | == Publish a test message == | ||
+ | |||
+ | Publishing a message | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | And see, which session was used during publishing, here '' | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ | == Consuming a test message == | ||
+ | |||
+ | Now a consumer can consume the message | ||
+ | |||
+ | {{https:// | ||
+ | |||
+ |
cloud/azure/functions.1714240230.txt.gz · Last modified: by skipidar