Sitemap

Create a Standard SQS Queue using Python

5 min readMar 3, 2023
Press enter or click to view image in full size

In this article we are continuing our Python series, this time creating a Amazon Simple Queue Services (SQS). SQS is a fully managed message queuing service that enables you to decouple and scale microservices, distributed systems, and serverless applications. Some of the key are simple, reliable, and highly scalable that can be used to transfer messages across multiple availability zones for high durability.

Standard SQS queues support a variety of features to include; sending/receiving messages in batch, visibility timeout configuration, and dead-letter queues to store messages that can’t be processed. Overall, SQS queue is simple and cost-effective way to decouple components of distributed applications, while providing high reliability and scalability.

For this medium, we will complete the following tasks:

  • Create SQS Queue using Python
  • Create a Lambda function in the AWS Management Console with Python 3.7 or higher runtime
  • Modify the Lambda to send a message to SQS queue
  • Create an API gateway HTTP API type trigger
  • Test the trigger to verify the message was sent.

Prerequisites:

  • AWS Free Tier
  • Basic Python and Boto3 Knowledge
  • IDE of Choice (Cloud9/Visual Studio)
  • GitHub/GitLab (Recommended)

Step 1: Import Boto3 Library and Create an SQS Client
Below is the code snippet I used to create an SQS client. We will need a new standard queue by calling the create_queue method. See documentation from SQS AWS documentation.

Press enter or click to view image in full size
SUCCESS!

You should see the ‘Queue url’ in the command line below.

Press enter or click to view image in full size
“QueueURL: https://sqs.us-west-2.amazonaws.com/…”

Switch over to your AWS management console, type in SQS in the search, and it should be shown.

Press enter or click to view image in full size
“luitw15-queue”

Step 2: Create a Lambda Function with Python 3.7 or Higher Runtime
AWS Lambda is a serverless computing service that allows developers to run their code in the cloud without having to manage servers or infrastructure. AWS Lambda is often used to build serverless applications with event-driven data processing pipelines. We will use Lambda Function to send messages to our SQS queue.

Get Charles Black II’s stories in your inbox

Join Medium for free to get updates from this writer.

Head over to AWS Management console, navigate to Lambda service, and click the orange “Create function” button.

Press enter or click to view image in full size

Choose “Author from scratch”, create a function name, and select Python 3.7 or higher for Runtime. I chose Python 3.9. Click “Create function button.

Press enter or click to view image in full size

Next, you should have a “green success” banner. Also, json code, which we will be modifying.

Press enter or click to view image in full size
Success!

Step 3: Modify the Lambda to send a message to the SQS queue
Now that our function is created, we can modify and add code that sends a message to our SQS queue. The following code snippet shows how to send a message to the SQS queue using Python Boto3 library.

Now lets configure a Test event to check our code.

Press enter or click to view image in full size

Once your template is created and saved, click the “Test” button.

Press enter or click to view image in full size
Success!

Step 4: Create an API Gateway HTTP API Type Trigger
Application Programming Interface (API) Gateway is a fully managed serviced by AWS that allows developers to create, deploy, and manage APIs. API gateway is a powerful tool, we will use to create an HTTP API type trigger for our Lambda function.

Switch back over the the AWS Management console, navigate to the API Gateway service, and click “Build.” We will be creating an “HTTP API.”

Press enter or click to view image in full size

On the next screen, hit the “Add Integration” drop-down menu, and select your Lambda function created earlier. Create an API Name and click “Next.”

Press enter or click to view image in full size

Step 3 “Define Stages” you can just press next. We will keep the default path.

Press enter or click to view image in full size
“Click Create”

You see the “green banner” page. HTTP API successfully created!

Press enter or click to view image in full size
Success!

Step 5: Testing the Trigger to Verify the Message Was Sent
Now that HTTP API has been created, we can test it by making an HTTP request. Head back over to the Lambda console and set the permissions needed to communicate with API Gateway. Navigate over to Lambda, select “Configuration”, “triggers”, and. “API Gateway Endpoint.” A new tab will open with confirmation it works.

Press enter or click to view image in full size

Final step is invoking the URL within the API Gateway details. Click the HTTP web browser that displays your Lambda function.

Press enter or click to view image in full size
Click the URL
Press enter or click to view image in full size

This concludes this tutorial, please continue to follow along my DevOps journey in AWS, Python, and future CI/CD Pipeline projects.

Charles Black II
Charles Black II

Written by Charles Black II

Founder and CEO of Made It Digital. Business | Technology | Personal Growth