Lambda and SQS: Using Lambda to send messages to an SQS queue

Joshua Hunter
4 min readJan 1, 2023

--

Amazon Simple Queue Service (SQS) is a fully managed message queuing service. Amazon SQS has many advantages:

  • Decoupled: SQS makes it easy to decouple and scale microservices, distributed systems, and serverless applications. By using SQS, you can send, store, and receive messages between various systems.
  • Flexible: SQS offers flexibility for processing messages. You can process messages in parallel or in sequence, and you can choose the level of throughput that you need.
  • Cost-Effective: You only pay for what you use, so you can start small and scale as your needs grow.

Below I will show how quick and easy it is to create an SQS queue using python and then use API gateway HTTP API to trigger a Lambda function to send messages to the queue.

Prerequisites

  • AWS Account
  • Fundamental Python Knowledge
  • Boto3 and AWS CLI installed and configured on a local machine, or virtual environment.

Creating a Standard SQS Queue using Python

I used the AWS Cloud9 integrated development environment (IDE) to create my SQS queue. Once you have Boto3 and the CLI installed you can easily use the following script to create a standard SQS Queue in your AWS account (modiying the script where you see fit.)

import boto3

sqs = boto3.client('sqs')

response = sqs.create_queue(
QueueName='my-queue',
)

print(response)

We can check our AWS account to make sure the queue was created and also grab the queue URL to use in our Python script our Lambda function will use.

Create and Modify a Lambda function to send a messages to the SQS queue

We will need to create an execution role for our Lambda functions. We could give Lambda full access to SQS but it is better to use the least privileges possible. You can reference my previous article on how to create and configure our lambda function. As well as use the custom policy I have provided below.

https://medium.com/@hunterjshaun/save-time-and-money-with-python-lambda-and-eventbridge-stopping-ec2-instances-6d2fdb88bd7

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"sqs:ReceiveMessage",
"sqs:GetQueueAttributes",
"sqs:SendMessage",
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "*"
}
]
}

Once our function is created we can import the Python code we will use to send messages to our previously created queue. Copy and paste the below script into your Lambda Function code section and click deploy to save. Notice we are using the queue URL we grabbed earlier.

Here I created a script that will output the current local time in UTC.

import boto3  # import the boto3 library to use the Amazon Web Services (AWS) Simple Queue Service (SQS)
import datetime # import the datetime module for getting the current time
import json # import the json library to convert the current time to a json object

sqs = boto3.client('sqs') # create a client object to interact with the SQS service
queue_url = 'https://sqs.us-east-1.amazonaws.com/637919856173/my-queue' # URL of the SQS queue

def lambda_handler(event, context):
current_time = str(datetime.datetime.now()) # Get the current time

response = sqs.send_message( # Send the message to the SQS queue
QueueUrl=queue_url,
MessageBody=current_time
)

return { # Return the current date and time in the message body when invoked
'statusCode': 200,
'body': json.dumps(current_time)
}

Once we have copied over our code we can test our function. We can select the test drop down menu > create new event > name the event > select apigateway-aws-proxy template > save.

We are using the apigateway template to test our function as this is how we will use an HTTP api tigger our funciont later.

Testing and Triggering our Function

We can now select the test button to invoke our Lambda function. If we then navigate to our SQS queue we should see messages in the queue.

Successful receipt of our messages.

Now we can set up our API trigger for our Lambda. In our designer dashboard we can select Add trigger.

We will then use the drop down menu to select API Gateway as our tigger > creat new API > select HTTP API > select Open Security > Add.

To invoke our Lambda function and display our message, we can go to the trigger configuration and copy the API endpoint URL. Then, we can paste it into a web browser and navigate to it

Success

To conclude, this demonstration showed how to set up a basic serverless workflow using AWS services and Python.

--

--