Amazon SQS Queues, Lambda Functions and HTTP API Triggers

Avyana Baker
Nerd For Tech
Published in
6 min readFeb 23, 2023

If you’re looking for a way to get started with Amazon Simple Queue Service (SQS), Lambda, AND Python, you have come to the right place. I will walk through a project that calls for the creation of both a Standard Amazon SQS Queue and a Lambda function with an API gateway HTTP API trigger.

Project Outline:

  1. Use Python to create a Standard SQS Queue.

2. Use the AWS console to create a Lambda function with a runtime of Python 3.7 or higher.

3. Modify the Lambda function to send a message to the SQS Queue. You could send a simple message such as the current time.

4. Create a trigger: API gateway HTTP API type.

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

Prerequisites:

  • An AWS admin account
  • AWS Cloud9 or an IDE of you’re familiar with
  • A desktop sticky note or notepad

Part A:

  • Login to your AWS admin user account and connect to your Cloud9 environment. We’ll start by creating a Standard SQS Queue.
  • Click file > New From Template > Python File > Save As… name your file. Make sure you KEEP the extension .py.
  • You could use the following script to create your SQS Queue:
  • Click the green Run button to the top right to run the script.
  • Take note of your queue’s URL and save it on your desktop sticky or notepad. We will use it later for the permissions configuration. Below is an example of the output you should receive:
  • For confirmation that you have successfully created the queue, you could go to your AWS management console > search SQS > click Queues > and you should see your standard queue listed. Great job so far!
  • Make sure you click on your queue and copy the ARN. Paste the ARN into the same sticky note where you are holding your queue’s URL.
Here is confirmation, from the AWS console, that my queue has been created.

Part B:

  • In the search bar of your AWS console, type in Lambda > selection functions > create function.
  • To create the Lambda function: select Author from scratch > name your function > select Python 3.7 for the Runtime > keep x86_64 for Architecture > Create a new execution role with basic Lambda permissions > Create Function.
  • You could also use the screenshot below as a guide:
  • Once you’ve successfully created your function, we will configure the permissions so the function is fully equipped for the remaining tasks.
  • Click configuration > Permissions > under execution role click the role name that was created for your function.
  • You will be taken to your function’s role under IAM. Below policy name, you should click on the policy so we could edit the permissions. Your policy may look similar to mine: AWSLambdaBasicExecutionRole-xxxxx
  • Notice we are missing the action for the function to send a message to our SQS queue? This is where the queue ARN that we copied earlier comes in to play!
  • Select Edit policy > JSON > and add the following code to your existing policy using your unique ARN in the Resource line:
{
"Effect": "Allow",
"Action": "sqs:SendMessage",
"Resource": "arn:aws:sqs:region_name:xxxx:queue_name"
}
  • Here’s what the final policy should look like:
  • Once you’ve made sure the brackets, curly brackets, commas, and parentheses, indents, and quotation marks are all in the right places… click review policy > save changes.

*You will be prompted to fix any errors as you’re editing the policy.*

  • You should be taken back to your function where you will click code. You’ll see an orange Test button and a drop down arrow next to it. Click the arrow and select configure test event.
  • To configure test event: Create new event > name your event > could leave event sharing as private > keep the hello-world template and event JSON > save and let’s move on.
  • In the code source section for your function, you could copy all of the code and replace it with mine shared below. Feel free to use this as a guide or create your own:
  • To save the file, you will need to click Deploy. Once you’ve done that, click Test. You should receive a similar execution result with a status code of 200. Success!
  • We could now go to our queue, which was empty when we created it, and check for the message.
  • Search SQS > Queues > click your queue > select send and receive messages.
  • As you could see we have messages. You may not have a list of messages because I had a lot of trial and error, before providing you with a solution! Click on the message and check it out.
  • As expected, I received a message with the current date and time. Please note: the time will be in UTC (Coordinated Universal Time). This is the message I had in my queue:

Part C:

  • Head back over to Lambda and we’ll get add a trigger to our function!
  • Once you click Add trigger, select the following to configure your trigger: API Gateway > Create new API > HTTP API for API type > Security — Open > Name your API > add!
  • See below for a reference:
  • Nice! You just added a trigger to your function and now it’s time to test it. Your screen should look similar to mine below. You will see a link next to your trigger labeled API endpoint. Copy the link and open it in a new tab. (Drum roll please…)
  • This is the result of successfully testing my trigger:

Wow! Look at all that we’ve completed together. Thank you so much for reading and following along with my tutorial. I hope you learned something new or feel more encouraged to get hands-on with Python and AWS. Stay tuned for my upcoming projects!

--

--