SQS Between AWS Lambda Functions

Girish V P
ADIBI Technologies, Basavanagudi, Blore.
2 min readFeb 21, 2024

Let us create a SQS Queue between AWS Lambda functions. First Lambda function sends message to SQS Queue and second Lambda function receive the SQS message present in the Queue.

Steps

1 — Create a Standard SQS Queue. Let me name it as Temp

2 — Create a Producer Lambda function which is used for submitting the message to SQS. Create appropriate IAM role which has Read/Write access to SQS and Cloudwatch in order to attach to the Lambda function. Add the code like below. I selected Python 3.9 as run time

import json
import boto3

def lambda_handler(event, context):
mytext = json.dumps ([ “Hello” , “This is my First SQS — Lambda Experiment.”
])
sqs = boto3.client(‘sqs’)
sqs.send_message(
QueueUrl=”https://sqs.us-east-1.amazonaws.com/Your-AWS-AccountID/Temp",
MessageBody=mytext
)

return {
‘statusCode’: 200,
‘body’: json.dumps(“I have completed my First Experiment”)
}

3 — Create a Consumer Lambda function which receives the SQS message. Create appropriate IAM role which has Read/Write access to SQS and Cloudwatch in order to attach to the Lambda function. Create a Lambda trigger, specify SQS service and SQS Queue name. Add the code like below. I selected Python 3.9 as run time.

import json

def lambda_handler(event, context):
print (event)
lines = event[‘Records’]

for line in lines :
body = line[‘body’]
print(body)

Result

Conclusion: We have learned how to send the message from Lambda to SQS and SQS to another Lambda function.

Note: Requested to test properly to implement in production environment.

--

--