Using SQS, Python, Lambda & API Gateway Triggers To Send Messages

Sharon Johnson
4 min readApr 22, 2023

--

Photo by Lucas van Oort on Unsplash

So lets get started with the “What is”

What is SQS? Amazon Simple Queue Service (SQS) lets you send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available.

What is Python? Python is a computer programming language often used to build websites and software, automate tasks, and conduct data analysis. Python is a general-purpose language, meaning it can be used to create a variety of different programs and isn’t specialized for any specific problems.

What is Lambda? Lambda is a serverless compute service that runs your code in response to events and automatically manages the underlying compute resources for you.

What is API Gateway & Triggers? API is use to create an HTTP endpoint that clients can interact with and your Lambda function can respond to, while trigger is a resource you configure to allow another AWS service to invoke your function when certain events or conditions occur.

Scenario:

A company wants to create a system that can track customer orders and send notifications when orders have been shipped. They want to use AWS services to build the system. They have decided to use SQS, Lambda, and Python for the project.

I will be using AWS Console and the Cloud9 (IDE) to perform the task.

Step 1: Create a Standard SQS Queue using Python.

In Cloud9 we will need to open a new file and name it(don’t forget to save it as a .py file) now we can open a new terminal so we can run the script. As you can see below it provided me a url after hitting run.

Let navigate to the AWS Console and search for SQS to verify that it was created successfully.

Step 2: Create a Lambda function in the console with a Python 3.7 or higher runtime.

Now lets search for “Lambda” and create a Lambda function, click on create function and follow the entries below

Here you will need to add your permissions, click on Create Function

Permission Policies Attached

Successfully created the function

Step 3: Modify the Lambda to send a message to the SQS queue. Your message should contain either the current time or a random number. You can use the built-in test function for testing.

In the Lambda function I created I entered in a Python script to create a message which will print out the date and time the message was sent.

From here you will need to “Deploy” and “Test” the code to get the execution results, hopefully you received a successful result here.

Navigate back to SQS in the console, click on your SQS Queue, then hit the Send and Receive message in the right corner of the screen, poll for the message and click your message ID number (mine not shown below missed the screenshot)

After clicking on your ID you should this message

Step 4: Create an API gateway HTTP API type trigger.

Navigate back to your Lambda function and click on add trigger, select API Gateway, create a new API, use HTTP API and under security select open now add.

Once you click “add” you will see the API Gateway, click on the API endpoint (https)

You should see the following results

Step 5: Test the trigger to verify the message was sent.

Navigate to the SQS to verify the message was sent. (this is what your verification should look like my time should not be the same as the above verification, I deleted the wrong screenshot but wanted to show you what to look for)

Project complete!

--

--

No responses yet