|
| 1 | +<!-- |
| 2 | +title: 'Using Azure Service Queue to trigger Azure Function' |
| 3 | +description: 'This example demonstrates how to trigger an Azure function when a message arrives in Service Bus Queue' |
| 4 | +layout: Doc |
| 5 | +framework: v1 |
| 6 | +platform: AZURE |
| 7 | +language: typescript |
| 8 | +authorLink: 'https://github.com/Kurshit' |
| 9 | +authorName: 'Kurshit Kukreja' |
| 10 | +--> |
| 11 | +# Create and Deploy Azure Function using Service Bus Queue as a trigger event. |
| 12 | + |
| 13 | +This example demonstrates how to create and deploy an Azure Function which has Service Bus Queue as its trigger event using Serverless Framework. |
| 14 | + |
| 15 | +## Use-cases |
| 16 | + |
| 17 | +- This app will create and deploy an Azure Function which would be triggered when a message would arrive in a Service Bus Queue. |
| 18 | +- This app also exposes an http endpoint to send a sample message to service bus queue. |
| 19 | + |
| 20 | +## How it works |
| 21 | + |
| 22 | +The serverless.yml would define an Azure Function handler with its trigger event as Service Bus and by providing necessary details about the service bus - queue name and connection string. Whenever a message would arrive in defined service bus queue, an azure function would be invoked and sent message would be processed in its handler. |
| 23 | + |
| 24 | +To send a sample message on defined service bus queue, `serverless.yml` declares one sample http POST end point. This can be used to send a message on defined service bus queue. |
| 25 | + |
| 26 | + |
| 27 | +## Setup |
| 28 | + |
| 29 | +#### 1. Install Project Dependencies |
| 30 | +`npm install` in this directory to download the modules from `package.json`. |
| 31 | + |
| 32 | +#### 2. To run the azure function locally using `serverlesss offline --stage dev` |
| 33 | + |
| 34 | +The `serverless offline --stage dev` command will let you try and test your azure function locally. |
| 35 | + |
| 36 | +Before running this command - |
| 37 | +1. You need to create a service bus queue on Azure Portal and provide the connection string in `serverless.yml` as an environment variable and refer this environment variable name in "connection" hook. |
| 38 | + |
| 39 | +```yml |
| 40 | +# in serverless.yml |
| 41 | +provider: |
| 42 | + environment: |
| 43 | + VARIABLE_SBUS_CONNECTION_STRING: 'Endpoint=sb://.......' |
| 44 | +.... |
| 45 | +.... |
| 46 | +.... |
| 47 | + |
| 48 | +events: |
| 49 | + - serviceBus: |
| 50 | + x-azure-settings: |
| 51 | + queueName: '<YourServiceBusQueueName>' |
| 52 | + accessRights: manage |
| 53 | + connection: VARIABLE_SBUS_CONNECTION_STRING |
| 54 | +``` |
| 55 | +2. Define Service Bus details - Connection string and queuename- in `.env.dev file`. This connection string & queuename would be used in `serviceBusMessageSender.ts` file to send sample message to service bus queue. |
| 56 | + |
| 57 | +```yml |
| 58 | +# in .env.dev file |
| 59 | + |
| 60 | +SERVICEBUS_CS='Endpoint=sb://.....' |
| 61 | +SERVICEBUS_QUEUE_NAME=<YourServiceBusQueueName> |
| 62 | +``` |
| 63 | +Once this is done, run the command `serverless offline --stage dev`. |
| 64 | + |
| 65 | +The console would show the output message which should look something like this - |
| 66 | + |
| 67 | +```bash |
| 68 | +Application started. Press Ctrl+C to shut down. |
| 69 | + |
| 70 | +Functions: |
| 71 | + |
| 72 | + sendMessage: [POST] http://localhost:7071/api/v3/send |
| 73 | + |
| 74 | + sampleHandler: serviceBusTrigger |
| 75 | + |
| 76 | +For detailed output, run func with --verbose flag. |
| 77 | +[2020-10-30T07:54:23.803] Worker process started and initialized. |
| 78 | +``` |
| 79 | + |
| 80 | +#### 3. To deploy the azure function using `serverless deploy --stage dev` |
| 81 | + |
| 82 | +1. Provide the appropriate Service Bus details in `serverless.yml` & `.env.dev` file as mentioned in step #2. |
| 83 | + |
| 84 | +2. Provide the desired azure `resourceGroup`, `subscriptionId`,`region` and `stage` values in `serverless.yml` to deploy the app on Azure. |
| 85 | + |
| 86 | +Once above steps are done, run the command `serverless deploy --env dev`. |
| 87 | + |
| 88 | +The console would show the output message which should look something like this - |
| 89 | + |
| 90 | +```bash |
| 91 | +Serverless: Finished uploading blob |
| 92 | +Serverless: -> Function package uploaded successfully |
| 93 | +Serverless: Deployed serverless functions: |
| 94 | +Serverless: -> Function App not ready. Retry 0 of 30... |
| 95 | +Serverless: -> Function App not ready. Retry 1 of 30... |
| 96 | +Serverless: -> Function App not ready. Retry 2 of 30... |
| 97 | +Serverless: -> sendMessage: [POST] sls-<region>-dev-service-bus-trigger-example.azurewebsites.net/api/api/v3/send |
| 98 | +``` |
| 99 | +#### 4. To test the sample end point and invoke the function - |
| 100 | + |
| 101 | +1. Send a sample message on service bus using `../api/api/v3/send` end point: |
| 102 | + |
| 103 | +Send a POST request on `../api/api/v3/send` endpoint with following payload - |
| 104 | + |
| 105 | +```bash |
| 106 | +{ |
| 107 | + "id": 101, |
| 108 | + "name": "AnyName", |
| 109 | + "gender": "Male/Female", |
| 110 | + "age": 30 |
| 111 | +} |
| 112 | + |
| 113 | +``` |
| 114 | + |
| 115 | +When the service bus receives the above sample message, it will invoke another azure function - `sampleHandler` and it shall print the above payload on console. |
| 116 | +The console would show the output message which should look something like this - |
| 117 | + |
| 118 | +```bash |
| 119 | + |
| 120 | +[2020-10-30T08:20:07.998] Executed 'Functions.sendMessage' (Succeeded, Id=f08ed5e7-5bd2-4c7c-8054-e23cad3dbb82, Duration=317ms) |
| 121 | +[2020-10-30T08:20:11.001] Executing 'Functions.sampleHandler' (Reason='New ServiceBus message detected on 'myqueuename'.', Id=abbb78d1-a19c-4ea7-b8a7-3ae6e9c6e66d) |
| 122 | +[2020-10-30T08:20:11.005] Trigger Details: MessageId: 83840f43a8824791bc2c9624d68ea1c2, DeliveryCount: 2, EnqueuedTime: 10/30/2020 8:20:10 AM, LockedUntil: 10/30/2020 8:20:40 AM, SessionId: (null) |
| 123 | +[2020-10-30T08:20:11.023] [2020-10-30T13:50:11.022+05:30][INFO][src\controller\triggerFunctionController.ts]: Azure function has been trigged with message {"id":10,"name":"Kurshit","gender":"Male","age":27} in service bus |
| 124 | +[2020-10-30T08:20:11.026] Executed 'Functions.sampleHandler' (Succeeded, Id=abbb78d1-a19c-4ea7-b8a7-3ae6e9c6e66d, Duration=39ms) |
| 125 | + |
| 126 | +``` |
0 commit comments