diff --git a/README.md b/README.md index 14b845c..458a1f8 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,7 @@ This is a collection of Python function samples on Azure Functions 2.X. For a co | [http-trigger-blob-sas-token](v2functions/http-trigger-blob-sas-token) | Azure Function HTTP Trigger Python Sample that returns a SAS token for Azure Storage for the specified container and blob name | HTTP | NONE | HTTP | | [http-trigger-dump-request](v2functions/http-trigger-dump-request) | Azure Function HTTP Trigger Python Sample that returns request dump info with JSON format | HTTP | NONE | HTTP | | [blob-trigger-watermark-blob-out-binding](v2functions/blob-trigger-watermark-blob-out-binding) | Azure Function Python Sample that watermarks an image. This function triggers on an input blob (image) and adds a watermark by calling into the Pillow library. The resulting composite image is then written back to blob storage using a blob output binding. | Blob Storage | Blob Storage | Blob Storage | +| [sbqueue-trigger-sbqueue-out-binding](v2functions/sbqueue-trigger-sbqueue-out-binding) | Azure Functions Service Bus Queue Trigger Python Sample. The function demonstrates reading from a Service Bus queue and placing a message into an output Service Bus queue. | Service Bus Queue | None | Service Bus Queue | ### Documents * [Quickstart V2 Python Functions with Azure Functions Core Tools](docs/quickstart-v2-python-functions.md) diff --git a/v2functions/sbqueue-trigger-sbqueue-out-binding/__init.py__ b/v2functions/sbqueue-trigger-sbqueue-out-binding/__init.py__ new file mode 100644 index 0000000..eb0d26d --- /dev/null +++ b/v2functions/sbqueue-trigger-sbqueue-out-binding/__init.py__ @@ -0,0 +1,7 @@ +import logging +import azure.functions as func + +def main(msgIn: func.ServiceBusMessage, msgOut: func.Out[str]): + body = msgIn.get_body().decode('utf-8') + logging.info(f'Processed Service Bus Queue message: {body}') + msgOut.set(msgbody) \ No newline at end of file diff --git a/v2functions/sbqueue-trigger-sbqueue-out-binding/function.json b/v2functions/sbqueue-trigger-sbqueue-out-binding/function.json new file mode 100644 index 0000000..6cbaf55 --- /dev/null +++ b/v2functions/sbqueue-trigger-sbqueue-out-binding/function.json @@ -0,0 +1,19 @@ +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "name": "msgIn", + "type": "serviceBusTrigger", + "direction": "in", + "queueName": "inqueue", + "connection": "ServiceBusNamespaceConnectionString" + }, + { + "name": "msgOut", + "type": "serviceBus", + "direction": "out", + "connection": "ServiceBusNamespaceConnectionString", + "queueName": "outqueue" + } + ] + } \ No newline at end of file diff --git a/v2functions/sbqueue-trigger-sbqueue-out-binding/readme.md b/v2functions/sbqueue-trigger-sbqueue-out-binding/readme.md new file mode 100644 index 0000000..32ee076 --- /dev/null +++ b/v2functions/sbqueue-trigger-sbqueue-out-binding/readme.md @@ -0,0 +1,71 @@ +# sbqueue-trigger-sbqueue-out-binding (Python) + +| Sample | Description | Trigger | In Bindings | Out Bindings +| ------------- | ------------- | ------------- | ----------- | ----------- | +| `sbqueue-trigger-sbqueue-out-binding` | Azure Functions Service Bus Queue Trigger Python Sample. The function demonstrates reading from a Service Bus queue and placing a new message into a Service Bus queue. | Service Bus Queue | None | Service Bus Queue | + +## Configurations + +`function.json`: + +```json +{ + "scriptFile": "__init__.py", + "bindings": [ + { + "name": "msgIn", + "type": "serviceBusTrigger", + "direction": "in", + "queueName": "inqueue", + "connection": "ServiceBusNamespaceConnectionString" + }, + { + "name": "msgOut", + "type": "serviceBus", + "direction": "out", + "connection": "ServiceBusNamespaceConnectionString", + "queueName": "outqueue" + } + ] +} +``` + +### Create the Service Bus Queues + +Create two Service Bus Queues in the namespace used in `ServiceBusNamespaceConnectionString`, `inqueue` and `outqueue`: + +```sh +az servicebus queue create --name inqueue \ + --resource-group $RESOURCE_GROUP \ + --namespace-name $SERVICEBUS_NAMESPACE + +az servicebus queue create --name outqueue \ + --resource-group $RESOURCE_GROUP \ + --namespace-name $SERVICEBUS_NAMESPACE +``` + +## How to develop and publish the functions + +### Local development + +```sh +func host start +``` + +### Publish the function to the cloud + +Publish the function to the cloud +```sh +FUNCTION_APP_NAME="MyFunctionApp" +func azure functionapp publish $FUNCTION_APP_NAME --build-native-deps --no-bundler +``` + +Add Functions App Settings +```sh +FUNCTION_STORAGE_CONNECTION="*************" +az webapp config appsettings set \ + -n $FUNCTION_APP_NAME \ + -g $RESOURCE_GROUP \ + --settings \ + MyStorageConnectionString=$FUNCTION_STORAGE_CONNECTION +``` \ No newline at end of file