The Notification that Python Built

A Beginner’s Guide to Using Python to Create a Standard SQS Queue to Receive Messages with a Lambda Function Triggered by API Gateway

Melissa (Mel) Foster
Women in Technology
7 min readMay 30, 2023

--

Edited Adobe Free Stock Image

Hello again! Today, I wanted to provide a walk-through creating a system that can track customer orders as well as send status notifications. I mean, who doesn’t love that email letting you know the status of your order? Our objective will be to build a system utilizing AWS services with Python. Are you up for the challenge? If so, let’s go…

A little background //

SQS (Amazon Simple Queue Service) is a fully managed AWS Service. SQS allows you the ability to send, store, and receive messages between software components at any volume, without losing messages or requiring other services to be available.

Objectives //

  • Create a Standard SQS Queue using Python
  • Create a Lambda function in the console with a Python 3.7 or higher runtime
  • Modify the Lambda to send a message to the SQS queue
  • Create an API Gateway HTTP API type trigger
  • Test the trigger to verify the message was sent

To follow along with this project you will need //

  • Access to AWS
  • An Configured AWS Cloud9 with AWS CLI & Boto3
  • An Optional GitHub Account
  • Attention to Details

Creating an SQS (Simple Queue Service) //

Our script we will be creating a Standard queue vs a FIFO (First-In-First-Out). If you need a FIFO queue you will need to adjust the code to specifiy FifoQueue attribute.

To create our SQS we will start in AWS Cloud9 on a New Branch:

  • Create a New Python File from Template
  • Import the Boto3 SDK
    (Boto3 will allow us to interact with AWS Services through Python code.)
  • Obtain the service resource
  • Create the Queue
  • print the response
#This code will create a standard SQS
import boto3

#Create a low-level SQS Client
sqs = boto3.client('sqs', region_name = 'us-east-1')

#Creating the Queue
response = sqs.create_queue(
QueueName= 'fosterwk15')
print(response)
  • Save as Create_SQS.py and then Run
Successful Run!

We can verify on AWS console by searching for Amazon SQS🠆Queues.

Way to go!!

Create a Lambda Function //

With our successful queue created, we can now create our Lambda function which will actually send the message to our queue. Navigate to Lambda from the AWS console.

  • Select Create function
  • Select Author from scratch
  • Add Function Name
  • Select Runtime Python 3.8
  • Click Create a function

With our successful Lambda function created, we need to ensure our Lambda Execution role has appropriate permissions. Navigate to the Configuration Tab on your success screen.

  • Select the highlighted role name, which will open up IAM in a new window
  • Select Add permissions
  • Search for SQSFullAccess
    (Note: granting full access for demo only)
Success!

Back on our Lambda success screen, we need to add our SQS as a destination.

  • Select Add destination
  • Change Destination Type to SQS queue
  • Change Destination to the SQS queue we created
  • Click Save

Next, we will continue to our third objective. You are doing great!!

Modify the Lambda to send a message to the SQS queue //

Remaining on our Lambda screen

  • Select Code Tab to Edit
  • Add the code below
    (Update the QueueURL to your specific URL which you can find on your SQS Queue Detail)
  • Click Deploy to save all changes
import boto3 
import json

from datetime import datetime

def lambda_handler(event, context):
# creating sqs client
sqs = boto3.client('sqs')
# using the now() function to get a datetime object containing current date and time.
now = datetime.now()
# using datetime.strftime() function to create a string representing
# the current time
time_date = now.strftime("%H:%M:%S %m/%d/%Y")


# sending the message to the sqs queue
sqs.send_message(
# sqs queue url: found in SQS Details
QueueUrl = 'https://sqs.us-east-1.amazonaws.com/796344786736/fosterwk15',
MessageBody = time_date)
return {
'statusCode': '200',
'body': json.dumps('Order_Processed')
}
  • Click Test to configure a test event
  • Name Event
  • Keep on Private
  • Select Template: apigateway-aws-proxy
  • Click Save
  • Select Test
Execution Results

Navigate to SQS Dashboard for another way to confirm that our Lambda function can successfully send a message to the SQS queue.

  • Select SQS Queue we created
  • Select Send and receive messages
  • Select Poll Messages
    This will take a few minutes to populate, but if we are successful we should see a message appear at the bottom of the screen.
  • Select Blue Highlighted Message
  • Select Body
Successful Test!! The Body is showing the Time in UTC. To formulate to your current time zone, you can utilize a convertor.

Let’s stretch before we complete the last two objectives. Remember when coding, case sensitivity can cause errors. Make sure to check your puctuation as well. Believe it or not a little space can cause a bit of a headache.

Adobe Free Stock Image

When you are ready, let’s wrap up this walk-through and finish strong!

Create an API Gateway HTTP API type trigger //

Let’s add an API Gateway HTTP API type trigger to our Lambda functon. Navigate back to the Lambda function we carefully created. Just like we added the destination to our function, we will now choose Add trigger.

  • Choose API Gateway from dropdown
  • Select Create a new API
  • Select HTTP API
  • Configure Security to Open
  • Select Add
We now have a successful API Gateway as a Trigger and Amazon SQS as a destination

Our Lambda function is now connected to the API Gateway and we can use it to send messages to our queue. Take a deep breath, we are moments away from completing this walk-through.

Test the API Gateway HTTP API type trigger //

To test our trigger, we don’t even have to leave our Lambda function. At the bottom of the screen you should be able to select Triggers from the left-hand menu and then go to the Configuration tab.

  • Select the highlighted blue https address

You should see a new internet window open with our message:

Whoo-Hoo!! SUCCESS!!

Additionally, we can verify by polling our SQS queue one final time.

  • Select SQS Queue
  • Select Send and receive messages
  • Select Poll for messages
Success! (Remember the time is in UTC )
Adobe Free Stock Image

High Fives all around! Together, we created the foundations to be able to send an “Order Processed” message. Just think of all the possiblities we could build upon. I encourage you to continue to push yourself with new challenges. I hope you join me on my next one!

Tips //

  • Commit any new or updated codes to your GitHub
    (Merge and delete your branch)
  • Delete your SQS
  • Delete Lambda function

Helpful Resources //

Join me on https://www.linkedin.com/in/melissafoster08/ or follow me at https://github.com/mel-foster

--

--

Melissa (Mel) Foster
Women in Technology

𝔻𝕖𝕧𝕆𝕡𝕤 𝗘𝗻𝗴𝗶𝗻𝗲𝗲𝗿 |𝒲𝑜𝓂𝑒𝓃 𝐼𝓃 𝒯𝑒𝒸𝒽 𝒜𝒹𝓋𝑜𝒸𝒶𝓉𝑒 | 𝚂𝚘𝚌𝚒𝚊𝚕 𝙼𝚎𝚍𝚒𝚊 𝙲𝚛𝚎𝚊𝚝𝚘𝚛 | Photographer