|
| 1 | +<!-- |
| 2 | +title: 'Serverless Framework Node SQS Producer-Consumer on AWS' |
| 3 | +description: 'This template demonstrates how to develop and deploy a simple SQS-based producer-consumer service running on AWS Lambda using the traditional Serverless Framework.' |
| 4 | +layout: Doc |
| 5 | +framework: v2 |
| 6 | +platform: AWS |
| 7 | +language: nodeJS |
| 8 | +authorLink: 'https://github.com/serverless' |
| 9 | +authorName: 'Serverless, inc.' |
| 10 | +authorAvatar: 'https://avatars1.githubusercontent.com/u/13742415?s=200&v=4' |
| 11 | +--> |
| 12 | + |
| 13 | +# Serverless Framework Node SQS Producer-Consumer on AWS |
| 14 | + |
| 15 | +This template demonstrates how to develop and deploy a simple SQS-based producer-consumer service running on AWS Lambda using the traditional Serverless Framework. It allows to accept messages, for which computation might be time or resource intensive, and offload their processing to an asynchronous background process for a faster and more resilient system. |
| 16 | + |
| 17 | +## Anatomy of the template |
| 18 | + |
| 19 | +This template defines two functions, `producer` and `consumer`. First of them, `producer`, is triggered by `http` event type, accepts JSON payload and sends it to a corresponding SQS queue for further processing. To learn more about `http` event configuration options, please refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). Second function, `consumer`, is responsible for processing messages from SQS queue thanks to its `sqs` trigger definition. To learn more about `sqs` event configuration options, please refer to [sqs event docs](https://www.serverless.com/framework/docs/providers/aws/events/sqs/). Additionally, the template takes care of provisioning underlying SQS queue along with corresponding SQS dead-letter queue, which are defined in `resources` section. The dead-letter queue is defined in order to prevent processing invalid messages over and over. In our case, if message is delivered to the source queue more than 5 times, it will be moved to dead-letter queue. For more details, please refer to official [AWS documentation](https://docs.aws.amazon.com/lambda/latest/dg/with-sqs.html). To learn more about `resources`, please refer to [our docs](https://www.serverless.com/framework/docs/providers/aws/guide/resources/). |
| 20 | + |
| 21 | +## Usage |
| 22 | + |
| 23 | +### Deployment |
| 24 | + |
| 25 | +This example is made to work with the Serverless Framework dashboard, which includes advanced features such as CI/CD, monitoring, metrics, etc. |
| 26 | + |
| 27 | +In order to deploy with dashboard, you need to first login with: |
| 28 | + |
| 29 | +``` |
| 30 | +serverless login |
| 31 | +``` |
| 32 | + |
| 33 | +and then perform deployment with: |
| 34 | + |
| 35 | +``` |
| 36 | +serverless deploy |
| 37 | +``` |
| 38 | + |
| 39 | +After running deploy, you should see output similar to: |
| 40 | + |
| 41 | +```bash |
| 42 | +Serverless: Packaging service... |
| 43 | +Serverless: Excluding development dependencies... |
| 44 | +Serverless: Creating Stack... |
| 45 | +Serverless: Checking Stack create progress... |
| 46 | +........ |
| 47 | +Serverless: Stack create finished... |
| 48 | +Serverless: Uploading CloudFormation file to S3... |
| 49 | +Serverless: Uploading artifacts... |
| 50 | +Serverless: Uploading service aws-node-sqs-worker.zip file to S3 (1.04 KB)... |
| 51 | +Serverless: Validating template... |
| 52 | +Serverless: Updating Stack... |
| 53 | +Serverless: Checking Stack update progress... |
| 54 | +................................................ |
| 55 | +Serverless: Stack update finished... |
| 56 | +Service Information |
| 57 | +service: aws-node-sqs-worker |
| 58 | +stage: dev |
| 59 | +region: us-east-1 |
| 60 | +stack: aws-node-sqs-worker-dev |
| 61 | +resources: 17 |
| 62 | +api keys: |
| 63 | + None |
| 64 | +endpoints: |
| 65 | + POST - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/produce |
| 66 | +functions: |
| 67 | + producer: aws-node-sqs-worker-dev-producer |
| 68 | + consumer: aws-node-sqs-worker-dev-consumer |
| 69 | +layers: |
| 70 | + None |
| 71 | +``` |
| 72 | + |
| 73 | +_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/). |
| 74 | + |
| 75 | +### Invocation |
| 76 | + |
| 77 | +After successful deployment, you can now call the created API endpoint with `POST` request to invoke `producer` function: |
| 78 | + |
| 79 | +```bash |
| 80 | +curl --request POST 'https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/produce' --header 'Content-Type: application/json' --data-raw '{"name": "John"}' |
| 81 | +``` |
| 82 | + |
| 83 | +In response, you should see output similar to: |
| 84 | + |
| 85 | +```bash |
| 86 | +{"message": "Message accepted!"} |
| 87 | +``` |
0 commit comments