In this hands-on AWS lab, you will learn how to trigger a Lambda function using SQS. This Lambda function will process messages from the SQS queue and insert the message data as records into a DynamoDB table.
– Download the Lambda execution role IAM policy [here ( https://github.com/julielkinsfembotit/SQSLambdaTriggers/blob/master/lambda_execution_role.json ).
– Download the Lambda function source code [here] ( https://github.com/julielkinsfembotit/SQSLambdaTriggers/blob/master/lambda_function.py ).
Learning Objectives
Successfully complete this lab by achieving the following learning objectives:
- Create the Lambda Function
- In the AWS Management Console, navigate to Lambda.
- Select Create a function.
- Give the function any name you like (e.g., "SQSDynamoDB").
- Under Runtime, choose Python 3.7.
- Create a new role with basic Lambda permissions. You will have to update this manually using the IAM policy here.
- Click Create function.
- Select SQS as the trigger, and choose the queue named Messages.
- Click Add.
- Click the function name at the top of the page to enable the function editor.
- Paste in the function body using the source code here.
- Set an environment variable called
QUEUE_NAME
and set its value to Messages (the name of the SQS queue). - Set an environment variable called
MAX_QUEUE_MESSAGES
and set its value to 10 (the maximum message batch size to receive from SQS). - Set an environment variable called
DYNAMODB_TABLE
and set its value to Message. - Save the Lambda function.
- Send Messages to SQS
- Connect to the public EC2 instance using the provided credentials.
- Run the
send_message.py
script to send messages to SQS:- Example: To send a message containing random text to the
Messages
queue every 0.1 seconds (10 messages per second):
./send_message.py -q Messages -i 0.1
Type Ctrl + C to quit.
Full source code is available here.
- Example: To send a message containing random text to the
- Using CloudWatch Logs, verify that the Lambda function has been triggered.
- Verify that a record has been written to DynamoDB.