Creating a Customer Order Tracking System using SQS, Lambda, and API Gateway

Miebi
5 min readSep 23, 2023

--

Introduction

A company wants to create a system to track customer orders. The company has decided to use the AWS Simple Queue Service (SQS), Lambda, and API Gateway services to create the foundational piece of this system.

AWS Lambda is a serverless computing service that allows one to run code without provisioning or managing servers. AWS handles the underlying infrastructure.

AWS SQS is a service that lets one send, store, and receive messages between components of an application.

Amazon API Gateway is a service that helps developers create, publish, maintain, monitor, and secure APIs. These APIs serve as the access point for applications.

For this project, API Gateway will be used to create an API that will trigger the Lambda function, which will then send a message to an SQS Queue.

To complete this project, the following items will be completed:

  1. Create a Standard SQS Queue using Python
  2. In the AWS Lambda console, create a new function and configure it to use Python 3.7 or a higher version.
  3. Update the Lambda function code to send a message to the SQS queue. Use the current time as the message content. Make sure to use the Boto3 library to interact with SQS.
  4. Create an API Gateway HTTP API type trigger: Set up an HTTP API trigger for the Lambda function in the API Gateway console. This allows you to invoke the Lambda function through an API endpoint.
  5. Test the trigger to verify the message was sent: Use a web browser to send a request to the API endpoint and verify that the Lambda function sends a message to the SQS queue.

Pre-Requisites

  • An AWS account
  • A set-up environment in AWS Cloud9 (this is optional, you can use any IDE that allows you to create the SQS Queue in your AWS account)

Solution

  • In Cloud9, create a Python file that will be used to create the SQS queue. The file should contain the code seen below:

In the code seen above, the boto3 library is imported to allow for interaction with AWS services, an SQS client is created and is used to create an SQS queue called project_queue, and the URL of the queue is printed out.

When I ran this code in my Cloud9 environment, I got the following result.

NOTE: Make sure to copy the URL printed out as that will be needed later on.

  • Go to the AWS Lambda console and create a function

Name the function, and select the latest version of Python as the runtime. Leave all other settings as the default selection.

NOTE: In order for Lambda to send messages to the SQS Queue, it needs the right permissions to do so. By default, whenever a Lambda function is created, a role is also created which has basic permissions. The right permissions will be assigned next.

  • Once the Lambda function has been created, go to the Configuration tab and select permissions. Click on the Role Name and it will open the role in the IAM console
  • Click on Add Permissions -> Attach policies -> Search for and select the AmazonSQSFullAccess policy ->Add permissions

Now, the Lambda role has the right permission to send messages to the SQS Queue.

  • Go back to the Lambda Console and edit the function code to the code seen below. This will enable the function to send a message of the current time to the SQS queue. Please read the comments to see what each command does.

NOTE: I wanted the time sent to the SQS Queue to be in my current timezone which is Central Daylight Time. This is why the cdt_timezone variable was defined as time is printed by default in the UTC time zone. If you want, you can modify this to your own time zone. You will just have to figure out the difference between UTC and your time zone and edit the hours value (in my case this was -5 as CDT is 5 hours behind UTC).

Also, make sure to replace the QueueUrl with the URL you copied earlier.

Click on Deploy once you’ve made the changes to the function code. Make sure you do this whenever you want to save changes to your code.

  • Click on Test. Name the event, leave all other settings as default values, and click Save. Click on Test again

You should now see the execution results showing the code was successful.

  • Go to the API Gateway Console and build an HTTP API
  • Add a Lambda Integration, select the Lambda function created earlier, and name the API. Leave all other settings as default and create the API
  • Go back to the Lambda function created. Go to configuration -> Triggers and open the API endpoint URL seen for the gateway created.

You will see the current time. If you keep on refreshing, you should see that time change.

  • You can also go to the SQS console into the queue you created. Click on Send and receive messages -> Poll for messages

You should see various messages come in. When you click on the ID for any of these messages, the body will be the time the Lambda function sent to the SQS queue. You will see multiple message IDs based on how many times you refreshed the API endpoint URL (i.e. triggered the Lambda function)

And that’s it for this project! Congratulations if you made it this far.

The code used for this project can also be found on my Github here: https://github.com/MiebiO/Python/tree/main/boto3/customer_tracking_orders

Happy learning!

--

--

No responses yet