HTTP API+Lambda+Python+SQS = Serverless Glory!

Christopher Lawshe
4 min readJan 19, 2023

--

As I continue my journey to become a Cloud DevOps engineer, I find out the more I learn, the less I know. (Serisouly!)

For my project this week, I created a serverless workflow to send a simple message generated by a Python script in AWS Lambda to an SQS queue. The function was setup to be triggered by a HTTP API Gateway as a trigger.

Prerequisites for this project:

  • AWS Free Tier Account
  • Amazon Cloud9 IDE configured on EC2 instance with boto3 and python 3.7 installed and credentials configured for my AWS profile

Objectives for this project:

  1. ) Create a standard SQS queue with Python
  2. ) Create a Lambda function with a runtime of Python 3.7 or higher
  3. ) Modify the Lambda function to to send a message to the SQS queue
  4. ) Create an API gateway with an HTTP type trigger
  5. ) Test the complete workflow for functionality by triggering the API gateway

LET’S GET STARTED!

STEP 1: Create the SQS queue with Python

First is (in my opinion) the easiest part of the project. To create the queue I fired up my AWS Cloud9 IDE and wrote a simple script with only the bare minimum requirements included to create the queue. (See code below)

The code defines ‘response’ as a Python variable to create an SQS queue named ‘Week15’.

STEP 2: Create a Lambda function with a runtime of Python 3.7 or higher

Next, I had to enter the AWS console directly to create a Lambda function.

After naming the function and designating the runtime as Python 3.7, I had to allocate an execution role to give it permission to execute and send messages to my SQS queue.

For the sake of the exercise, I gave the function a pre defined role named AmazonSQSFullAccess as it includes all privileges for SQS. Note that this was done only for the sake of simplicity. Best practice dictates that you allocate only the necessary permissions to resources in their roles. (LEAST PRIVILEGE)

STEP 3: Modify the Lambda function to to send a message to the SQS queue

After creating the function and assigning necessary permissions I had to input the Python code to my lambda function to send a message to the SQS Queue.

The code defines the current time as variable, ‘current_time’ and sends it as a message to the SQS queue.

After this was complete, I set the SQS queue as a Target of my lambda function in the console.

I set the queue as a destination upon successfully executed code.

At this phase I executed my Lambda function a couple of times to verify functionality and checked the queue to ensure the messages were received.

The result of the Lamba execution was a success and the time is displayed on the screen and sent to the SQS Queue.

Please note that it was necessary to poll the queue to recieve the messages and read them.

The queue is populated with the two messages resulting from the two Lambda function executions from earlier.

STEP 4: Create an API gateway with an HTTP type trigger

The final piece of the serverless puzzle was to create the API gateway that would trigger the Lambda function to execute and provide messagess to our SQS queue. I decided creating a simple HTTP API was the best way to accomplish this. To do so, I clicked the ‘Add trigger’ button in the Lambda console menu and created the API from scratch.

In the trigger configuration menu, I set the trigger type to ‘API Gateway’ and selected the ‘HTTP API’ API type. After giving it a name I left the other options as default and completed the creation.

STEP 5: Test the complete workflow for functionality by triggering the API gateway

For the final step in the project I just needed to verify that prompting the API would indeed trigger the Lambda function and send the message through to the SQS queue. From the Lambda console menu, I clicked on the API trigger and then clicked on the given URL that appeared in the ‘Configuration’ menu.

(Note that clicking the link opens a new web page that displays the current time in Coordinated Universal Time (UTC), the standard for AWS.)

After executing this I went over to SQS and opened the ‘Week15’ queue and ran a poll to collect messages to verify the time message came through.

With the success of this message receipt my serveless application is complete!

--

--

Christopher Lawshe
Christopher Lawshe

Written by Christopher Lawshe

IT enthusiast — looking to learn from and connect with IT professionals to help push the world forward through Cloud DevOps

No responses yet