Working with AWS and Python to trigger Lambda to send a message to an SQS queue.

Morgan Clark
4 min readJul 13, 2022

Task at hand…

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 for the lambda.
5) Test the trigger to verify the message was sent.

Required items:

a. AWS IAM user account with admin Access (Personal or business account)
b. IAM permissions
c. Boto3 and AWS CLI to be installed on Cloud9 using the following commands:

1. pip3 install awscli
2. pip3 install boto3 {For reference please see https://boto3.amazonaws.com/v1/documentation/api/latest/guide/quickstart.html}

Here we go…

I. From within the AWS Cloud9, we will Create a Standard SQS Queue using Python:
Below is the script that I used to create my SQS in AWS.

Figure 1 AWS SQS written in Python

To execute the above python script, there are several options to select from to execute your code:
1. Enter: python script.py {the script name} and press enter
2. Click the Green circle containing the black arrow labeled Run at the top of the below screenshot
3. Click Run on the tool bar, then select a run option from there

Figure 2 Run options available in AWS Cloud9 IDE

4. You can utilize the link above or head over to your AWS account to the SQS section and confirm that your new queue was created:

Figure 3 Created SQS queue

II. Create a Lambda function in the console with a Python 3.7 or higher runtime

  1. While still in the AWS Management console, search for Lambda and select the option to Create Function.
    a. Select Author from scratch
    b. Name your function (ProjWK16)
    c. Select Runtime Python 3.7 or a higher version
    d. Click the Create function button
Figure 4 Newly created Lambda function

III. 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.

  1. Add permissions to the new role built and assigned to Lambda function
  2. Select Configuration > Permissions then click on the role name {ProjWK16-role-zj4xgt3m}
Figure 5 Lambda function’s configuration section

3. You are now in the IAM (Identity and Access Management) console
4. Click on the add permissions button

Figure 6 IAM Role for project

5. Search for SQS and select AmazonSQSFullAccess (Temporarily during this project)

Figure 7 Searching for SQS to assign role to the project
Figure 8 Selection and attaching SQS policy
Figure 9 Permissions currently assigned after completing the previous step

6. Now let’s replace the code provided in the lambda_function, shown below

Figure 10 Python code generated when the function was created
Figure 11 New Lambda function and Python code

7. Line 21 in the above Python script requires the URL from the SQS queue that was created earlier

8. Select Deploy, Test, and then create a test event

Figure 12 Copy the URL info shown below

9. Select Test and confirm that the code that you entered for the lambda function, returns a 200 for successfully executing.

Figure 13 Configuring a test event for the Lambda function
Figure 14 Result of running the test

IV. Create an API gateway HTTP API type trigger for the lambda.

While in the Lambda function…
1. Click the add button labeled + Add trigger

Figure 15 Lambada function and Add trigger

2. Configure the new API trigger by selecting: API Gateway, Create an API, API type is HTTP API, Security is Open and click the add button

Figure 16 Building the Trigger with API gateway

Just like the SQS role, we had to assign permissions for access, we will do the same here also.

3. Follow these steps: Select configuration, permissions, click on the Role name, select Add permissions…

… attach Policy, search for API and select the one named AmazonAPIGatewayAdminstrator (temporary usage) then finally click the attach policies button.

Figure 17 Assigning the required permissions to the trigger
Figure 18 Test the trigger to verify the message was sent
Figure 19 Several messages from testing

You have success!!!

--

--

Morgan Clark

Sr. Telecom Engineer, pursuing new skills and career opportunities in DevOps.