|
| 1 | +# AWS Lambda event filtering with DynamoDB and SQS |
| 2 | + |
| 3 | +Simple demo application illustrating AWS Lambda event source filtering with DynamoDB and SQS. For this demo, we will use AWS Serverless Application Model (SAM), and a thin LocalStack wrapper `samlocal` to create our infrastructure through SAM on LocalStack. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +* LocalStack |
| 8 | +* Docker |
| 9 | +* `make` |
| 10 | +* [`awslocal`](https://github.com/localstack/awscli-local) |
| 11 | +* [`samlocal`](https://github.com/localstack/aws-sam-cli-local) |
| 12 | +* NodeJS 14.x |
| 13 | +* [`ulid`](https://www.npmjs.com/package/ulid) |
| 14 | + |
| 15 | +## Installing |
| 16 | + |
| 17 | +Setup [Serverless Application Model (SAM)](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) and [AWS SAM CLI Local](https://github.com/localstack/aws-sam-cli-local) on your local machine. We also recommend using NodeJS 14.x alongside a [Node Version Manager](https://github.com/nvm-sh/nvm) to manage your NodeJS versions. |
| 18 | + |
| 19 | + |
| 20 | +Start LocalStack via: |
| 21 | + |
| 22 | +```sh |
| 23 | +localstack start -d |
| 24 | +``` |
| 25 | + |
| 26 | +## Deploy the application |
| 27 | + |
| 28 | +Let us first install the local dependencies: |
| 29 | + |
| 30 | +```sh |
| 31 | +npm install --save ulid |
| 32 | +``` |
| 33 | + |
| 34 | +To setup the infrastructure on LocalStack, run: |
| 35 | + |
| 36 | +```sh |
| 37 | +samlocal deploy -g |
| 38 | +``` |
| 39 | + |
| 40 | +You will be prompted to enter a name for the stack. Use the default options for the prompts and fill `Y` (`Yes`) for the confirmation prompt. The stack will be created and the output will be printed to the console. |
| 41 | + |
| 42 | +If you have made any changes to the application, you can update the stack by running: |
| 43 | + |
| 44 | +```sh |
| 45 | +samlocal deploy |
| 46 | +``` |
| 47 | + |
| 48 | +After deploying you can send a SQS message to the queue and see the Lambda function being triggered: |
| 49 | + |
| 50 | +```sh |
| 51 | +awslocal sqs send-message --queue-url http://localhost:4566/000000000000/MyQueue --message-body "{ "data" : "A" }" --delay-seconds 10 |
| 52 | +``` |
| 53 | + |
| 54 | +You will see a JSON output similar to the following: |
| 55 | + |
| 56 | +```json |
| 57 | +{ |
| 58 | + "MD5OfMessageBody": "64dfee8647a8264b25b01b7f22d72d3a", |
| 59 | + "MessageId": "22fbddd2-5add-4a03-a850-152780d786c1" |
| 60 | +} |
| 61 | +``` |
| 62 | + |
| 63 | +In the `template.yaml` we have defined the DynamoDB table and the Stream function with a filtering criteria. We instruct the Stream function to trigger the Lambda function only when the filtering criteria is satisfied. |
| 64 | + |
| 65 | +Using the SQS, we send a message body to the DynamoDB stream to match the specific filtering criteria. After the message is sent, we can see the Lambda function being triggered and you can check the logs to verify it. |
| 66 | + |
| 67 | +## Destroy the application |
| 68 | + |
| 69 | +To destroy the infrastructure on LocalStack, run: |
| 70 | + |
| 71 | +```sh |
| 72 | +samlocal delete |
| 73 | +``` |
0 commit comments