Invoking Lambda function using SQS
In this article I’ll walk you through how we can create the SQS queue for sending a message and consume it by creating a lambda function. I’ll be dividing the implementation of this into two parts as follows:
1. Create a SQS queue and add message to queue you want to send.
2. Create the Lambda function to consume your SQS service.
So let’s get started.
1. Create a SQS queue and store the message you want to send.
- Assuming that you already have an AWS account created. Just sign-in to your AWS account and navigate to Services →Simple Queue Service. Click on create new Queue which will allow you to create the SQS Queue. Enter the suitable queue name and click on configure queue.
- Next screen will show you the default configuration of queue. I am creating the Queue with its default configuration, you can change the configurations as per your requirement. Finally click on create Queue.
- Queue is now created. Note the URL of Queue from details section as we will be requiring it later on.
- Now add the message to queue which will be later consumed by our Lambda function. For adding message click on Queue actions send message. Add some content to message and click on send message.
- Now message is added to poll and you can see in below screen that 1 message is available in queue.
2. Create the Lambda function to consume your SQS service.
- Now we have successfully created the Queue. In the next step we will create the Lambda function to consume our queue messages. So Navigate to Services →AWS Lambda →Click on Create function.
- Enter the suitable function name and other details in the above screen and click on create function. It will navigate you to next screen where we will write a function to consume our SQS queue.
Below is my Lambda function which is written in node.js.
var AWS = require(‘aws-sdk’);
AWS.config.update({region: ‘YOUR_SQS_REGION’});
var sqsqueue = new AWS.SQS({apiVersion: ‘2012–11–05’});
var queueURL = “ YOUR_SQS QUEUE_URL";
var params = {
AttributeNames: [
“SentTimestamp”
],
MessageAttributeNames: [
“All”
],
QueueUrl: queueURL,
VisibilityTimeout: 0,
WaitTimeSeconds: 0
};
exports.handler = (event, context, callback) => {
sqsqueue.receiveMessage(params, function(err, data){
console.log(“data : “+ JSON.stringify(data));
if (err){
console.log(“Print Error: “, err);
callback(err, ‘Error Fetching message’);
} else if(data.Messages){
console.log(“Message Body: “+data.Messages[0].body);
var deleteParams = {
QueueUrl: queueURL,
ReceiptHandle: data.Messages[0].ReceiptHandle
};
sqsqueue.deleteMessage(deleteParams, function(err, data){
if(err){
console.log(“Error in deleting the msg”, err)
}else{
console.log(“Message Deleted”, data)
}
});
}
else{
console.log(“No Messages Recieved”);
}
});
};
I have provided the URL and region of SQS Queue which we had noted in the previous step. By providing this URL we are connecting our lambda function to our SQS Queue. We are calling receiveMessage() function which will print the message which is available in poll. Once message is processed it will be deleted from the poll as we are calling deleteMessage() function after printing the message content. No we will test our lambda function to see if it is functioning correctly.
- Click on Test to test our lambda function and you can see in the below screen, it is showing the message “Hello World!!” in output. Also it is showing that message is deleted from the queue as it is processed.
That’s it folks. Please get in touch in case of any queries.
Thanks.