Triggering Lambda from Amazon SQS

45 minutes
  • 2 Learning Objectives

About this Hands-on Lab

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.

Learning Objectives

Successfully complete this lab by achieving the following learning objectives:

Create the Lambda Function
  1. Navigate to Lambda.
  2. Select Create a function.
  3. Give the function the name "SQSDynamoDB".
  4. Under Runtime, choose Python 3.7.
  5. Create a new role with basic Lambda permissions. You will have to update this manually using the IAM policy here.
  6. Click Create function.
  7. Select SQS as the trigger, and choose the queue named Messages.
  8. Click Add.
  9. Click the function name at the top of the page to enable the function editor.
  10. Paste in the function body using the source code here.
  11. Set an environment variable called DYNAMODB_TABLE, and set its value to "Message".
  12. Save the Lambda function.
Send Messages to SQS

Note: Full source code is available here.

  1. Connect to the public EC2 instance using the provided credentials.
  2. Install the prerequisites for Python, as well as Python, Boto3, and the Faker library.
  3. Run the script to send a message containing random text to the Messages queue every 0.1 seconds (10 messages per second):

    ./ -q Messages -i 0.1
  4. Press Ctrl+C to quit.
  5. Verify a record has been written to DynamoDB.
  6. Using CloudWatch Logs, verify the Lambda function has been triggered.

Additional Resources

Make sure you're in the N. Virginia (us-east-1) region throughout the lab.

An SQS topic and DynamoDB table have been provided for you. The EC2 instance contains a script called you can use to quickly send a high volume of messages to SQS.

  • Download the Lambda execution role IAM policy here.
  • Download the Lambda function source code here.

Configure the Lambda function so it is triggered by the SQS queue. Make sure each message sent to the SQS queue is inserted into DynamoDB.

What are Hands-on Labs

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Sign In
Welcome Back!

Psst…this one if you’ve been moved to ACG!

Get Started
Who’s going to be learning?