SQS ReceiveMessage succeeds but gets a null message

Viewed 2274

I have the following code in lambda to receive SQS messages: When I inject a message into SQS, the lambda triggers, but says data.Messages is null.

function receiveMessages(callback)
{
    var params = {
        QueueUrl: TASK_QUEUE_URL,
        MaxNumberOfMessages: 2,
        WaitTimeSeconds: 1,
        AttributeNames: ["All"]
    };

    SQS.receiveMessage(params, function(err, data)
    {
        if (err)
        {
            console.error(err, err.stack);
            callback(err);
        }
        else if (data.Messages == null)
        {
            console.log("null message", data);
            callback(null,null);
        }
        else
        {
            callback(null, data.Messages);
        }
    });
}

It is not obvious what I might be doing wrong. I tried both a fifo and a non-fifo queue

1 Answers

When using an SQS Queue as a Lambda event source, a component of the Lambda service actually polls the queue and passes the message payload to the function invocation, in an array event.Records, which will contain one or more messages from the queue. The messages are temporarily invisible in the queue (they are "in flight").

You don't need to interact directly with SQS in this application.

You process the messages and exit the Lambda function successfully and all the messages just given to you are automatically deleted from the queue by the Lambda poller.

If an exception is thrown, all the messages you were just handed are set back to being visible in the queue.

Related