AWS Lambda — Python, SQS and API Gateway
What is this article about?
This article is another example of Python and what its relationships is to AWS services. Knowing Python is really an integral piece for anyone that wants to automate and utilize serverless architecture in the cloud. I don't think I am saying anything groundbreaking in stating how important it is to learn scripting if you are a technology professional. It can give you the opportunity to automate mundane tasks and save your company money. So, like I stated previously just another small example of the power of scripting!
This article will explore the following:
- Creating an SQS queue in AWS using Python
- Creating a Lambda function that will use Python to send messages to the SQS queue
- Create an API gateway that has an HTTP API type trigger
- Test the trigger to verify the message was sent
A Disclaimer
Before I get started, I have gotten to the point of my articles where I am not going to go in to deep on what you need in regard to setting up your permissions and connecting tools to AWS etc. I assume you know these things as a technology professional. If you do not there are plenty of resources available on medium and YouTube that can help you with those pieces.
That being said …... let gets started!
1. Creating an SQS queue using Python.
Below is the screenshot showing the simple Python script that I used to create the SQS Queue. Some important pieces of this are importing boto3 (The SDK), using the resource (sqs), using the create_queue command and making sure you name your queue besides QueueName .
To verify that it is was created, you can login it to your AWS instance, go to SQS. You should see the queue you created. It should match the name you provided in the script and the URL should match what was printed to your screen.
2. Creating the Lambda function
In your AWS console go to the Lambda section. From there you want to choose create function. Some of the key parts of this is you must give it a name and choose Python 3.7. Then select the Create function button.
Once created you should be presented with the following screen.
There are a couple of key points to the above screen:
- The lambda_function.py — this is where we will enter your Python code to send the message to the SQS queue.
- The other is you need to ensure the Lambda function has the correct IAM role to give access to SQS to allow Lambda to send a message to the queue.
Here is a screenshot of the Python that I will be using that will send a message to my newly created queue. This needs to be pasted into the lambda_function.py section.
Here is a screenshot of the code in the Lambda function
I tried testing it and received an error. I realized I needed to change the Iam role to give the function the right permission. If you go to Configuration>Permissions you can find the role that was automatically created for you.
If you click on the role name you can then add the correct permissions.
For this example, I chose the AmazonSQSFullAccess role. I would recommend being more granular with roles/permissions in a production environment.
Get Marc Coady’s stories in your inbox
Join Medium for free to get updates from this writer.
To test this, you can create a test event and use the API Gateway AWS Proxy
If it works you should receive the following when you run the test:
Next up adding the trigger!
3. Adding an API gateway that has an HTTP API type trigger
To the left of the Lambda function choose add trigger button:
Configure the API Gateway like the following and choose the Add button.
When completed your screen should look like this:
If you click on the API endpoint you should receive the following output from a browser:
It should show you the same output from before, when we tested the function.
4. The final step is to verify our SQS queue
If you go to your SQS queue you should see that there are messages available:
Choose your queue and select the send and receive messages button
Then choose the Poll for messages button
You should see your messages:
The body of these messages should be created from the random number you generated using the Lambda function.
In conclusion and links
Well another project completed. It was great to complete this as many of these services I was aware of but, never fully vetted and tested. For my the Python scripts I used, here is my link to Github branch that has them.








