Triggering Lambda from SQS
AWS Training Architect II in Content
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. - Download the Lambda function source code here.
Triggering Lambda from SQS
In this hands-on AWS lab, we 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.
Log in to the AWS Management Console using the credentials provided on the lab instructions page. Make sure you are using the us-east-1 (N. Virginia) region.
Create the Lambda Function
- In the AWS Management Console, navigate to the Lambda service.
- Click Create a function.
- Under Author from scratch, give the function a name (e.g., "SQSDynamoDB").
- Under Runtime, choose Python 3.7.
- Under Role, choose Create a custom role.
- In the IAM role editor, click View Policy Document.
- Click Edit, then Ok.
- In a new browser tab, open the IAM policy provided on the lab instructions page.
- Copy the policy text to your clipboard, and paste it in the policy document field in the IAM Management Console.
- Click Allow.
- On the Create function screen, click Create function.
- Under Designer, scroll down through the list of triggers and click SQS.
- Scroll down the page to SQS queue, and select the Messages queue from the dropdown.
- Set the batch size to 10.
- Check the box next to Enable trigger.
- Click Add.
- Click the function name at the top of the page, and scroll down the page to the code editor.
- Paste the function source code into the function editor.
- Scroll down the page to the Environment variables header, and configure the following environment variables:
- Key: QUEUE_NAME | Value: Messages
- Key: MAX_QUEUE_MESSAGES | Value: 10
- Key: DYNAMODB_TABLE | Value: Message
- Click Save.
Send Messages to SQS
- Open your terminal application, and connect to the public EC2 instance using the credentials provided on the lab instructions page.
yesat the prompt.
- Enter your password at the prompt.
- List the contents of the root directory.
- Open the
send_message.pyscript and familiarize yourself with the contents.
- Press Ctrl + C to exit the vim editor.
- Send a message to the
Messagesqueue every 0.1 seconds (10 messages per second).
./send_message.py -q Messages -i 0.1
- Switch to your browser, and navigate to the CloudWatch service in the AWS Management Console.
- Click Logs in the left sidebar.
- Click the name of the log group to open it.
- Click one of the log stream links, and verify that the Lamda function has been triggered.
- Navigate to the DynamoDB service.
- Click Tables in the left sidebar.
- Select the Messages table.
- Click the Items tab.
- Click one of the listed items to open it.
- In the Edit item menu, verify that a record has been written to DynamoDB.
Congratulations, you've successfully completed this hands-on lab!