livesdmo.com

Creating an AWS Lambda Function to Send Messages to SQS

Written on

Chapter 1: Introduction

In this guide, we'll walk through the process of creating an AWS Lambda function that sends messages to an SQS Queue. We'll utilize Python and the Boto3 library to achieve our objectives.

Objectives:

  • Set up a Standard SQS Queue with Python
  • Develop a Lambda function
  • Adjust the Lambda function to transmit a message containing the current time to the SQS Queue
  • Implement an API Gateway HTTP API trigger
  • Test the trigger to confirm the message was sent successfully

A Word of Advice: If you're undertaking a similar project for educational purposes or a cohort, document your process concurrently. Delaying this task can result in the need to redo the project for effective writing. I encountered this issue after completing my project six weeks ago.

SQS Queue Creation Process

After executing the code, it will display a URL which we’ll revisit later. Now, log into your AWS console to verify the creation of the SQS Queue.

Successful SQS Queue Creation

Fantastic! The queue has been created successfully, and it currently holds no messages.

Next, let's create our Lambda function in the AWS console. Navigate to Lambda and click on “Create function”.

Lambda Function Creation

Choose the option to author from scratch, assign a name to your function, and select a runtime version. For this project, I opted for Python 3.7 or higher. After that, click on create function. We will create a new role using the default execution role, so no modifications are necessary here.

Lambda Function Configuration

Now, navigate to IAM to set up our role. Begin typing the function name, and it should appear in the list. Select it.

IAM Role Selection

Next, choose the corresponding policy name displayed.

Policy Name Selection

You should now see the policy settings. Click on “edit policy” to add the necessary action and resource so that Lambda can send messages to SQS.

Editing IAM Policy

Once you click on edit policy, navigate to the JSON tab.

JSON Policy Editing

Before modifications, this is what you should see.

After adding the necessary action, adjust the resource ARN at the bottom, and then select review policy.

Reviewing IAM Policy

We added the SendMessage action on line 14 and specified our SQS ARN as the resource on line 17. Now, save your changes.

Saving IAM Policy Changes

Afterward, return to your Lambda function and open it. Scroll down to find the code section. We need to modify this code so that Lambda can send a message to SQS.

Lambda Function Code Editing

Before modifications, this is the initial code.

The following code will enable Lambda to send the current time to SQS. Remember to replace the QueueUrl in this code with the URL generated in the first step.

After updating the code, click on deploy to apply the changes. Then create a test event.

Creating Test Event

Ensure you change the QueueUrl to the URL printed earlier. Create a new event, name it, and select the apigateway-aws-proxy template. Finally, scroll down and hit save.

Saving Test Event

Let’s run the test.

Testing the Lambda Function

In the Response section, you should see that the statusCode indicates success and the body displays the time.

Now, we will create an API Gateway and set up an HTTP API trigger. Scroll back to the top of the page and click on “Add trigger”.

Adding Trigger to Lambda Function

Select API Gateway, create a new API, choose HTTP API, and set security to open. Then, scroll down and click add.

API Gateway Configuration

Now, we need to verify if the message was sent. Copy the API endpoint and paste it into your browser.

API Endpoint Verification

Here’s the API endpoint.

API Endpoint Display

Keep in mind that the time displayed in the browser is in UTC, so it may differ from your local time zone.

Lastly, let’s check SQS to confirm that a message has been received.

Checking SQS for Messages

Success! A message has been received!

That's it! You have successfully created a Lambda function to send messages to an SQS Queue with the current time.

Thank you for reading! I hope this guide was helpful.

Jason Wood

@jwood9799

Chapter 2: Video Resources

Explore how to set up Amazon SQS and trigger Lambda functions using .NET on AWS in this informative video.

Learn the steps to trigger AWS Lambda from SQS in just two minutes with this concise tutorial.

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Understanding Burnout: Prevention Strategies for a Healthier Life

Explore effective strategies to prevent burnout and maintain well-being in both work and personal life.

# Optimizing Your Thinking and Planning Workflow with Amplenote and Obsidian

Discover how to enhance your productivity with Amplenote and Obsidian for effective thinking and planning.

The Myers-Briggs Type Indicator: Validity and Critiques Explored

This article delves into the criticisms and scientific evaluations surrounding the Myers-Briggs Type Indicator and offers alternatives like the Big Five model.