Skip to main content

Triggering Lambda from SQS

Hands-On Lab

 

Photo of Mark Richman

Mark Richman

AWS Training Architect II in Content

Length

01:00:00

Difficulty

Intermediate

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.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Triggering Lambda from SQS

Introduction

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.

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

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

  1. 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

  1. Open your terminal application, and connect to the public EC2 instance using the credentials provided on the lab instructions page.
    ssh cloud_user@<PUBLIC_IP_ADDRESS>
    • Type yes at the prompt.
    • Enter your password at the prompt.
    • List the contents of the root directory.
      ll
    • Open the send_message.py script and familiarize yourself with the contents.
      vim send_message.py
    • Press Ctrl + C to exit the vim editor.
    • Send a message to the Messages queue 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.

Conclusion

Congratulations, you've successfully completed this hands-on lab!