Creating a Standard SQS Queue and a Lambda Function with Python: A Step-by-Step Guide

Fatima Darbo
6 min readJan 20, 2023

--

In this tutorial, we will use Python 3.7 to create a standard SQS queue on AWS. We will also create a Lambda function in the AWS console to send a test message to the SQS queue. Lastly, we will create an API gateway and select an HTTP API type trigger for the Lambda function. We will then test sending a message to the SQS queue to verify its successful implementation.

Prerequisite:

  • AWS Console Account
  • MFA Code
  • Cloud9 / IDE of your choosing

Objective:

  1. Create a Standard SQS Queue using Python.
  2. Create a Lambda function in the console with a Python 3.7 or higher runtime
  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.
  4. Create an API gateway HTTP API type trigger.
  5. Test the trigger to verify the message was sent.

Step 1: Creating the SQS Queue

We will begin by opening our preferred Integrated Development Environment (IDE) and creating a new Python template. This template will serve as the foundation for our code and will allow us to easily copy and paste the provided Python code.

Once the template is set up, we will copy and paste the necessary code into the template and run the script. This script will be responsible for creating our Simple Queue Service (SQS) queue. After the queue is successfully created, the script will return the URL of the queue, which can then be used for further actions.

Step 2: Verifying the Queue
In order to use the boto library, it is necessary to include the boto3 package. The code above will create a Simple Queue Service (SQS) with the name ‘week15queue’. The print statement below confirms that the SQS has been created successfully. To confirm this, you can also check for the SQS in the AWS Console.

Step 3: Creating the Lambda Function

The third step is the creation of the Lambda function. We’ll go to the AWS console and select “Create Function”. Then we’ll choose “Author from Scratch” and assign a name to our function. We’ll select Python 3.7 from runtime. Note that all the other settings should be kept default. Finally, we’ll click the create function.

To create a new Lambda function from scratch, select the “Author from scratch” option. Give the function a name of your choice, in this example it is named “myLUITFunction”. Ensure that you have selected a runtime of Python 3.7 or higher. Scroll down to the bottom of the page, and you will find a “Create function” button on the right side, click on it to begin creating the function.

The next step is to make modifications to the permission of the execution role. Go to configurations and then click permission to find the auto-generated role name.

The next step in the process is to edit the policy name. Once you’ve created the function, you will be directed to the function’s page. On this page, you will find the option to edit the function’s policies. By clicking on the edit button, you will be able to edit the policy name, which is used to grant permissions to the function.

When we access the IAM service, we will notice that there is already an AWSLambdaBasic Execution Role policy attached. Following the principle of least privilege, we will add the necessary permissions to allow the Lambda function to send messages to the SQS queue by making changes to the existing policy.

Now we’ll go to the JSON tab, where we’ll define our role to get access for sending messages to the SQS queue. Make sure that you review the policy and save the changes.

Step 4: Changing the lambda function

To proceed to the next step, it’s essential to retrieve the SQS URL that was generated previously. This URL will be necessary when setting up the Lambda function.

To edit the function, go to the Lambda page on the AWS Console, select the function you want to modify, and click on the Code tab. In this step, we will modify the code to allow the Lambda function to send messages with timestamps to our SQS queue. To do this, delete the default “Hello World” code and substitute it with the provided code.

Next, we will move on to the testing lab. In this step, we will make a small modification to the “sqs-receive-message” template. The change will be specific to the testing lab and it will not affect the overall functionality of the SQS queue or the Lambda function. Once the template has been updated to “apigateway-aws-proxy”, we can proceed with the testing process to ensure that our SQS queue and Lambda function are functioning correctly.

The Lambda function page has a button labeled “Deploy” which serves the same purpose as saving a file. The first time you deploy code, you will be prompted to name the deployed version. Subsequent deployments will automatically save without redirecting to the naming page.

Then, we’ll hit the test button to ensure the successful execution of results.The script has completed its execution successfully, indicated by the return of a 200 status code.

To confirm, let’s head over to SQS in the Console and see if we got the message from Lambda:

Step 6: Create API Gateway Trigger

The next step is to trigger the Lambda function. We just need to click the “Add Trigger” button for that.On the Lambda function dashboard on the left there will be an “Add Trigger” button.

Click “Add Trigger” then go ahead and add the correct trigger.

As seen in the screenshot, we’ll make the following settings changes, scroll down to the bottom and click Add.

Part 7: Finally Test The Trigger

To conclude, we will perform a test on the trigger we have created. To do this, navigate to the Lambda function page and select the Configuration tab. On the left side of the page, locate the Triggers section. Here, you will find the API Gateway trigger and its corresponding API endpoint URL. This URL can be used to test and verify the trigger’s functionality

Upon completion of this step, a new browser window should automatically open, displaying “ Congrats! Msg sent to SQS! ”

Finally, to confirm that the message was sent successfully, we can return to the SQS queue that was created at the start of this tutorial. The message we sent should now be visible in the queue.

Congratulations for making it to the end consider following and sharing thanks!

--

--