Python: Send event from AWS Lambda to AWS SQS
Upasana | January 03, 2020 | 3 min read | 2,804 views | AWS Tutorials
Here, we are going to assume that you are familiar with working of AWS lambda function.
In case, you are not then you can go through this article https://www.javacodemonk.com/creating-aws-lambda-using-python-3-6-7803cc53
For overview of AWS SQS, you can go through this article: https://www.javacodemonk.com/aws-sdk-2-sqs-object-operations-using-spring-boot-d3e157fb
We are going to learn how to send event from AWS lambda to AWS sqs queue.
Table of contents
-
Configure AWS Lambda
-
Set permissions on AWS SQS
Configure AWS Lambda
In Lambda, we will be choosing python3.6 to build the function. For working with sqs queue and sending message, we will be using boto3
.
Since, we will be working with SQS, we shall choose the lambda function also with the related Execution role. Choose the execution role as given in the image below while creating lambda function.
Same execution role can be added later on as well if you have already created lambda function by changing execution roles.
send_sqs_message
takes two necessary parameters i.e. sqs queue url & message that we want to send to sqs
def send_sqs_message(sqs_queue_url, msg_body):
"""
:param sqs_queue_url: String URL of existing SQS queue
:param msg_body: String message body
:return: Dictionary containing information about the sent message. If
error, returns None.
"""
# Send the SQS message
sqs_client = boto3.client('sqs') (1)
sqs_queue_url = sqs_client.get_queue_url(
QueueName=QueueName
)['QueueUrl'] (2)
try:
msg = sqs_client.send_message(QueueUrl=sqs_queue_url,
MessageBody=json.dumps(msg_body)) (3)
except ClientError as e:
logging.error(e) (4)
return None
return msg
1 | Getting SQS with boto3 |
2 | We are getting the queue URL with get_queue_url and extracting only QueueUrl from the returned json of the the method. |
3 | Sending message to queue after converting the dict to json with json.dumps |
4 | Here we are logging the error which we can check in AWS cloud watch to and setup the alarms if any exception received |
def lambda_handler(event, context):
"""Exercise send_sqs_message()"""
QueueName = 'sqs' (1)
# Set up logging
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s: %(asctime)s: %(message)s')
# Send some SQS messages
msg = send_sqs_message(QueueName,event)
if msg is not None:
logging.info(f'Sent SQS message ID: {msg["MessageId"]}')
return {
'statusCode': 200,
'body': json.dumps(event)
}
1 | Define the sqs queue name here. In our case, its sqs |
2 | Here, we are sending event to sqs as message with send_sqs_message |
Whole code would be something like below:
import json
import logging
import boto3
from botocore.exceptions import ClientError
def send_sqs_message(QueueName, msg_body):
"""
:param sqs_queue_url: String URL of existing SQS queue
:param msg_body: String message body
:return: Dictionary containing information about the sent message. If
error, returns None.
"""
# Send the SQS message
sqs_client = boto3.client('sqs')
sqs_queue_url = sqs_client.get_queue_url(
QueueName=QueueName
)['QueueUrl']
try:
msg = sqs_client.send_message(QueueUrl=sqs_queue_url,
MessageBody=json.dumps(msg_body))
except ClientError as e:
logging.error(e)
return None
return msg
def lambda_handler(event, context):
"""Exercise send_sqs_message()"""
QueueName = 'sqs'
# Set up logging
logging.basicConfig(level=logging.DEBUG,
format='%(levelname)s: %(asctime)s: %(message)s')
# Send some SQS messages
msg = send_sqs_message(QueueName,event)
if msg is not None:
logging.info(f'Sent SQS message ID: {msg["MessageId"]}')
return {
'statusCode': 200,
'body': json.dumps(event)
}
Permissions to Lambda
For sending messages to sqs, lambda would need permissions which can be given globally or only to AWS Lambda in Permissions section in SQS queue manager.
Now, we are good to go to use lambda functions. Thanks for reading.
AWS Tutorials:
- AWS Lambda Interview Questions for Developers
- AWS SDK 1.x - S3 file download & upload
- AWS SDK 2: SQS Object Operations using Spring Boot
- S3 File upload & download with AWS Java SDK v2
- AWS Lambda in Kotlin using Spring Cloud Function
- Creating AWS Lambda using python 3.6
- Invoke AWS Lambda from a Kotlin Client