5
votes

SQS messages get processed successfully by lambda. Here is the code that processes and then executes the callback:

 exports.handler = function(event, context, callback) {
   handleSQSMessages(context,event, function () {
    const response = {
        statusCode: 200,
        body: JSON.stringify({
            message: 'SQS event processed.',
            input: event,
        }),
    };
    console.log ("OK DONE");
    callback(null, response);

function handleSQSMessages(context, messages, callback) {
 messages = messages.Records;
 if (messages && messages.length > 0) {
   messages.forEach(function(message) {
       console.log(message);
       //...

I see that cloudwatch prints the "OK DONE" message. However, SQS puts messages 'in flight' (and it stays in flight forever). My understanding is that once a successful response is sent, the message will be automatically deleted. My visibility timer = 10 min

2
Have you deleted the message? SQS doesn't automatically delete the message for you. - stdunbar
Lambda is supposed to auto delete after a successful response is my understanding - Siddharth Ram
Lambda does what your code tells it to do. I've never heard of it doing anything like that. - stdunbar
docs.aws.amazon.com/lambda/latest/dg/with-sqs.html . Lambda polls the queue and invokes your function synchronously with an event that contains queue messages. Lambda reads messages in batches and invokes your function once for each batch. When your function successfully processes a batch, Lambda deletes its messages from the queue. - Siddharth Ram
I see a forEach in the AWS examples. Your link says that if you don't respond to the batch (i.e. all the messages that your are given) then it does not delete the messages. Are you reading everything given? - stdunbar

2 Answers

0
votes

You seem to be missing some closing parenthesis/curly braces. Is the handler callback being called in the handleSQSMessages callback? It's hard to tell with the missing braces - but if not, that's probably the reason it's failing.

0
votes

You could set context.callbackWaitsForEmptyEventLoop to false to send the callback response immediately, instead of waiting for all event loop tasks to get completed.