Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Cosnumer for AWS SQS queues
var AWS = require('aws-sdk');
//TODO : change this region based in your SQS service region
AWS.config.update({region: 'ap-south-1'});
var sqs = new AWS.SQS({apiVersion: '2012-11-05'});
//TODO : change this URL based in your queue URL
var queueURL = "https://sqs.ap-south-1.amazonaws.com/499222264523/my-queue";
var params = {
AttributeNames: [
"SentTimestamp"
],
MaxNumberOfMessages: 1,
MessageAttributeNames: [
"All"
],
QueueUrl: queueURL,
VisibilityTimeout: 0,
WaitTimeSeconds: 0
};
exports.handler = (event, context, callback) => {
// TODO implement
sqs.receiveMessage(params, function(err, data) {
console.log("data : " + JSON.stringify(data));
if (err) {
console.log("Receive Error", err);
callback(err, 'Error fetching messages from SQS');
} else if (data.Messages) {
console.log("Number of messages received : " + data.Messages.length);
console.log("Received message : " + JSON.stringify(data.Messages[0]));
console.log("Message body : " + data.Messages[0].Body);
var deleteParams = {
QueueUrl: queueURL,
ReceiptHandle: data.Messages[0].ReceiptHandle
};
sqs.deleteMessage(deleteParams, function(err, data) {
if (err) {
console.log("Delete Error", err);
} else {
console.log("Message Deleted", data);
}
});
}
else {
console.log("No messages received");
}
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.