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.
Setup Serverless Application Model (SAM) and AWS SAM CLI Local on your local machine. We also recommend using NodeJS 14.x alongside a Node Version Manager to manage your NodeJS versions.
Start LocalStack via:
localstack start -d
Let us first install the local dependencies:
npm install --save ulid
To setup the infrastructure on LocalStack, run:
samlocal deploy -g
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.
If you have made any changes to the application, you can update the stack by running:
samlocal deploy
After deploying you can send a SQS message to the queue and see the Lambda function being triggered:
awslocal sqs send-message --queue-url http://localhost:4566/000000000000/MyQueue --message-body "{ "data" : "A" }" --delay-seconds 10
You will see a JSON output similar to the following:
{
"MD5OfMessageBody": "64dfee8647a8264b25b01b7f22d72d3a",
"MessageId": "22fbddd2-5add-4a03-a850-152780d786c1"
}
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.
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.
To destroy the infrastructure on LocalStack, run:
samlocal delete