Utilizing Python to create and send messages to SQS using Lambda with an API Gateway trigger.

Abidoye Joshua mayowa
7 min readApr 21, 2023

--

Welcome to Week 15 with LUIT!, In this week’s tutorial, we will send a message from AWS SQS via Lambda Function with API gateway. The Lambda function will be written in Python coding language.

Scenario:

A company wants to create a system that tracks customer orders and sends notifications when orders have been shipped. They want to use AWS services to build the system. They have decided to use SQS, Lambda, and Python for the project.

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.

Prerequisites:

  • AWS CLI and Boto3 installed
  • AWS account with I AM user access, NOT root user
  • Basic AWS command line knowledge
  • Basic Python programming language
  • Basic knowledge of AWS Interactive Development Environment (IDE)

Definition of Basic Terms

API Gateway is a fully managed service that makes creating, publishing, maintaining, monitoring, and securing APIs at any scale easy. It acts as a front door for your application, allowing users to request/response with the API of an application.

Lambda is a serverless computing service that lets you run your code without provisioning or managing servers. Lambda takes care of scaling, availability, and fault tolerance.

SQS (Simple Queue Service) is a fully managed message queuing service that enables you to decouple and scale microservices and serverless applications. It allows you to send, store, and receive messages between software components at any scale.

So let’s dive into the project.

Step 1:Create a Standard SQS Queue Using Python.

To create our Python script, we must first import boto3.

To call the SQS service resource: we use sqs = boto3.resource(‘sqs’, region_name = ‘us-east-2’). We must adjust the region name to the region in which you want to create the SQS queue. We must define a variable that uses the SQS create_queue action to create the queue. This creates a standard queue; you can assign a queue name (required) and attributes (optional). In the code below, I have assigned the queue name “W15Project_SQS” and the attributes DelaySeconds and VisibilityTimeOut.

import boto3

sqs = boto3.resource('sqs', region_name = 'us-east-2')

queue = sqs.create_queue(
QueueName='W15Project_SQS',
Attributes={'DelaySeconds': '5', 'VisibilityTimeout': '60'})

print(queue.url)

Run the Python script in your IDEor AWS Cloud9 and copy the queue URL in your notepad for later use.

Step 2: Create a Custom IAM Role for Lambda

Our Lambda function will need permission to send messages to the SQS queue. In the IAM console, click Roles, then Create role. On the next screen, for Trusted entity type select AWS service, under Use Case select Lambda, then click Next.

We need to give our Lambda function permission to interact with the SQS queue. Search for the AWSLambdaSQSQueueExecutionRole policy. This policy has most of the permissions we need, but we will have to modify it a bit to fit our preferred use case. After finding the policy, copy the JSON code and click Create policy. This will open up a new browser tab where you can create the policy.

On the next screen, delete the existing code and paste the code from the AWSLambdaSQSQueueExecutionRole policy. Under “Action,” add “sqs: SendMessage” to the policy. This will allow Lambda to send messages.

Click the Next: Tags button, then Next: Review. Give the policy a name and review the summary of permissions, then click Create policy.

After creating the permission policy, navigate to the browser tab where you created the role. Refresh the policies and find the policy you just created. You may have to refresh it a few times before it shows up. Select the policy and click Next.

Create a new role and give the role your desired name and attach the policy name in the image above to the new role.

Step 3: Create Python Script to Send a Message with the Current Date and Time.

Lambda functions can run code in response to an event trigger. We want to create a Lambda function that will send a message to the SQS queue we just created. The message will contain the current UTC time created by the Python script we wrote above.

Firstly, Navigate to the Lambda console and click Create function. Select the Author from scratch. Under Basic information, give the function a name and select Python 3.7. Leave the Architecture as the default x86_64. Under Permissions, expand the Change default execution role arrow. Select Use an existing role, then under the Existing role drop-down, find the role you just created and select it. Click Create function.

Under the Code tab, delete the existing code and paste it into your created Python code to send the timestamp message. When your code is complete, click Deploy.

Step 4:Test Lambda Function Sending Message to SQS

Click on Test and select Create new event. Give the test event a name. Under Template, find the SQS template and click Save.

Head back to the Lambda function page, and you can now click Test, which will send a test message to the SQS queue. You should see a response with status code 200 and your message body, including the date and time.

To confirm this worked, head over to the SQS console and click the SQS queue you created previously. Click Send and receive messages.

Click Poll for messages to see your message appear. Click Stop polling.

Notice that the Sent date and time are in your current time zone. Click on the message hyperlink to see the details of the message, and here you will find the message in UTC format.

Step 5: Create an API Gateway with HTTP API Trigger

Lambda has different trigger types, including SQS. However, for this tutorial, we want an HTTP API to trigger our Lambda, which will send our date and timestamp message to the SQS. To do this, navigate to the API Gateway console and click Create API. For Choose an API type, select HTTP API, then click Build.

Under Integrations select Lambda. Select your AWS Region and the Lambda function you created earlier. Give the API a name and click Next.

For this tutorial, under Configure routes, you can delete the Resource path and click Next.

Under Define stages, you can leave it as default and click Next.

Review the API and click Create.

You should now see your API with an Invoke URL.

Step 6: Test the API Gateway Trigger

To test the API Gateway trigger, you simply click the Invoke URL, which should bring you to an HTTP webpage displaying your Lambda function message. You have completed the tutorial if you see your date and timestamp message.

Thank you so much for following along with this tutorial. If you like this tutorial, please watch for my future tutorials, where I will dive deeper into AWS and Python and start containerization soon!

To follow me via LinkedIn on social media, click the link below.

linkedin.com/in/joshua-abidoye-0ab796195

--

--

Abidoye Joshua mayowa

DevOps Engineer. I'm interested in collaborating with anyone interested in cloud engineer or cloud DevOPs.