Last updated on April 27, 2023
Introduction
In the first part of this post, we covered the concept behind event filtering in AWS Lambda and learned how it could help you save money by invoking functions only when necessary. Now it’s time to put that knowledge into practice. In this part 2, we’ll see event filtering in action using Amazon Simple Queue Service (SQS) as the event source.
Overview
- A user sends a JSON message to the SQS queue.
- Event source mapping picks up the message from the queue and evaluates it against the specified filtering criteria.
- Event source mapping invokes the Lambda function synchronously when a filtering criterion is met.
Setting up
I will give you two options for setting up the resources needed in this tutorial. The first option is the easier route, where you simply deploy the provided CloudFormation template, and it’ll provision everything for you. The second one is for those who prefer a hands-on approach and involves setting up the resources manually in the AWS Console.
Via AWS CloudFormation
- Download the CloudFormation template here. Save it as a .yaml file on your local computer.
- Go to the CloudFormation console. Click Create stack.
3. Click on Upload a template file. Click Choose file, then select the template that you’ve just downloaded. Finally, hit Next.
4. Enter a stack name. Then, click Next.
5. Click Next until you reach the final step. Scroll down and tick the box that says I acknowledge that AWS CloudFormation might create IAM resources. Hit Submit.
6. Wait for CloudFormation to finish deploying the resources. You may click the refresh button to see updates without reloading the webpage on your browser.
7. Once finished, head over to the Testing section for testing.
Manual method
- Create a Lambda function and choose Python3.9 as the Runtime. Leave everything else as is.
2. Copy the code below and paste it into your Lambda code editor. Then, deploy the changes.
3. Under the Configuration tab, select Permissions and click the execution role that was created for your function. You will be redirected to the IAM Role page. Copy the ARN of your function’s execution role and put it in a text editor. You’ll need this later.
4. Go to the SQS Console and create a queue.
5. Scroll down to the Access policy section. In the Define who can receive messages from the queue setting, select the Only the specified AWS accounts, IAM users, and roles option. In the text box, paste the ARN of your function’s execution role.
**Note that SQS will generate three permissions for you by default. You have to include the sqs:GetQueueAttributes to successfully configure the SQS queue as an event source for your Lambda function later.
6. Go back to your Lambda function and click the Add trigger button.
7. Select SQS as the source, then choose the SQS queue you’ve created. Set the Batch size to 1.
8. Add filter criteria and paste the following pattern:
{"body":{"price":[ { "numeric": [ ">", 50 ] } ]}}
9. Click Add
Testing
- On the webpage of your SQS queue, click Send and receive messages.
2. Enter the following message and click Send message.
{"price":55}
3. Try sending multiple messages with varying prices ( < 50 or > 50).
4. Head over to your Lambda function. Under the Monitor tab, click View CloudWatch Logs.
5. You should only see prices that are greater than 50. This means that messages containing prices under 50 were discarded by the event source mapping.
Conclusion
That’s it! I’ve shown you a basic example of how you can use event filtering in your workflow. You can try exploring other filter criteria to suit your needs.
To learn more about AWS Lambda, check out this FREE AWS Lambda Foundations course. And if you’re preparing for an AWS Certification, our high-quality practice exam samplers can help you get ready for test day. Try them out for FREE to get a feel for the exam format and level of difficulty.