|
| 1 | +# SQS to Lambda and DynamoDB (Message Aggregator pattern) |
| 2 | + |
| 3 | +This pattern create two SQS queue, one for receiving the splitted messages and one for the final aggregation and a lambda function and a DynamoDB Table. The pattern is an implementation of the Integration pattern: "Message Aggregator" available here: [https://www.enterpriseintegrationpatterns.com/patterns/messaging/Aggregator.html](https://www.enterpriseintegrationpatterns.com/patterns/messaging/Aggregator.html) |
| 4 | + |
| 5 | +Learn more about this pattern at Serverless Land Patterns: [https://serverlessland.com/patterns/sqs-lambda-dynamodb-message-aggregator-cdk](https://serverlessland.com/patterns/sqs-lambda-dynamodb-message-aggregator-cdk) |
| 6 | + |
| 7 | +Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the [AWS Pricing page](https://aws.amazon.com/pricing/) for details. You are responsible for any AWS costs incurred. No warranty is implied in this example. |
| 8 | + |
| 9 | +## Requirements |
| 10 | + |
| 11 | +* [Create an AWS account](https://portal.aws.amazon.com/gp/aws/developer/registration/index.html) if you do not already have one and log in. The IAM user that you use must have sufficient permissions to make necessary AWS service calls and manage AWS resources. |
| 12 | +* [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/install-cliv2.html) installed and configured |
| 13 | +* [Git Installed](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) |
| 14 | +* [AWS CDK](https://docs.aws.amazon.com/cdk/v2/guide/getting_started.html) (AWS SAMCDK) installed |
| 15 | + |
| 16 | +## Deployment Instructions |
| 17 | + |
| 18 | +1. Create a new directory, navigate to that directory in a terminal and clone the GitHub repository: |
| 19 | + ```bash |
| 20 | + $: git clone https://github.com/aws-samples/serverless-patterns |
| 21 | + ``` |
| 22 | +1. Change directory to the pattern directory: |
| 23 | + ```bash |
| 24 | + $: cd sqs-lambda-dynamodb-message-aggregator-cdk/src |
| 25 | + ``` |
| 26 | +1. From the command line, use AWS SAM to deploy the AWS resources for the pattern as specified in the template.yml file: |
| 27 | + ```bash |
| 28 | + $: cdk deploy |
| 29 | + ``` |
| 30 | +1. Note the outputs from the CDK deployment process. These contain the resource names and/or ARNs which are used for testing. |
| 31 | + |
| 32 | +## How it works |
| 33 | + |
| 34 | +The pattern is composed by an SQS Queue which is capable to receive portion of messages. Messages can be aggregated together by providing, for each message, two distinct attributes. `CorrelationId` will inform the AWS Lambda function how to aggregate multiple messages and `Total` will inform the AWS Lambda about the total amount of messages that should be expected for one single aggregation. |
| 35 | + |
| 36 | +Messages are tracked inside a DynamoDB table. When all messages with the same `CorrelationId` are received, the message is aggregated, forwarded to a destination SQS Queue and the record is deleted from DynamoDB, to avoid unecessary costs. |
| 37 | + |
| 38 | +## Testing |
| 39 | + |
| 40 | +Inside the Folder `msg` there are 3 distinct messages and the relative attribute file. The script `test.sh` will send all three messages to the source SqS queue. |
| 41 | + |
| 42 | +```bash |
| 43 | +$: ./msg/test.sh |
| 44 | +Provide the name of the SQS queue to send messages to |
| 45 | +https://sqs.us-east-1.amazonaws.com/[your account]/[your account]-src-queue |
| 46 | +{ |
| 47 | + "MD5OfMessageBody": "8be1b3b225c0f5113fa4c37f421667d2", |
| 48 | + "MD5OfMessageAttributes": "a512f8c4c91e1bd27eaedaa9916243fe", |
| 49 | + "MessageId": "35527c0c-3dcc-4ec7-8afa-b997019d0549" |
| 50 | +} |
| 51 | +{ |
| 52 | + "MD5OfMessageBody": "4018bae8eed190d3c907720c3f3cdf1c", |
| 53 | + "MD5OfMessageAttributes": "a512f8c4c91e1bd27eaedaa9916243fe", |
| 54 | + "MessageId": "dda99dbf-2f97-4e88-ab28-68011b707a9a" |
| 55 | +} |
| 56 | +{ |
| 57 | + "MD5OfMessageBody": "a9d29a076761cb5a4a1f955814338fcb", |
| 58 | + "MD5OfMessageAttributes": "a512f8c4c91e1bd27eaedaa9916243fe", |
| 59 | + "MessageId": "33e07f8d-5c37-4ffa-aaef-b8f90b9b7646" |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +The script will wait 5 seconds then try to pull the destination queue: |
| 64 | + |
| 65 | +```bash |
| 66 | +Provide the name of the SQS queue to receive the aggregated message |
| 67 | +https://sqs.us-east-1.amazonaws.com/[your account]/[your account]-dest-queue |
| 68 | +{ |
| 69 | + "Messages": [ |
| 70 | + { |
| 71 | + "MessageId": "52505cb6-7013-4f0f-a6f7-4d4f2f5e223d", |
| 72 | + "ReceiptHandle": "AQEB8E0k8R6Qn4BdU8fxWM93NTavPuy6+0lbtg7avweEoMWgETQar1puOAROTB5XZuiqSyaEi7pRH6qaBAgfEuzBLX9aC4gGxRPBXrYyu3byntEk2k6T68q2sLjrhlHpa5dbUuuJGk5ahluQkbMaLqtq3jMRHfVp8QLrzAGdBqZzIGLTJAI9kfD1H38anwnlWF1fSMsY/jC21spEeq8zM72w4Lc83qMynrsPJMCysrzoyG7/PVXYnf8OWHj+am9FPzGPSq0YE+1nW1ATJKEcug2DXJgVdbWpXe+uJ5osOHfEk+e/pTf0uHd2ZL1trr1ICwU4F4Af81tZH8GYP4S7aJ8EMLZycyZDxXBuoHIj2wh86E0gy4Ke0vygbskyYkLAZXSqmiwP87oCIYUcxuITgiA3Hg==", |
| 73 | + "MD5OfBody": "0871a008c15d4c8ccede318f8b942ee5", |
| 74 | + "Body": "{\"VAT\":\"7.7\",\"Street\":\"Louisiana Road 11\",\"Bank\":\"New York State Bank\",\"TotalAmount\":\"123.00 USD\",\"OrderId\":\"123ABC\",\"State\":\"NY\",\"Transaction\":\"123ABC\",\"Date\":\"2023-01-01\",\"Time\":\"08:00:00\"}" |
| 75 | + } |
| 76 | + ] |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Cleanup |
| 81 | + |
| 82 | +1. Delete the stack |
| 83 | + ```bash |
| 84 | + $: cdk destroy |
| 85 | + ``` |
| 86 | +---- |
| 87 | +Copyright 2023 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 88 | + |
| 89 | +SPDX-License-Identifier: MIT-0 |
0 commit comments