By using AWS re:Post, you agree to the Terms of Use

AWS SDK SQS get number of messages in a dead letter queue


Hello community,

I somehow can't find the right information. I have following simple task to solve: create a lambda that checks if a dead letter queue has messages and if it has, read how many.

Before I did that I had an alarm set on an SQS metric. I chose the 'ApproximateNumberOfMessagesVisible' metric since 'NumberOfMessagesSent' (which was my first choice) does not work for DLQueues. I have read this article:

The NumberOfMessagesSent and NumberOfMessagesReceived for a dead-letter queue don't match

If you send a message to a dead-letter queue manually, it is captured by the NumberOfMessagesSent metric. However, if a message is sent to a dead-letter queue as a result of a failed processing attempt, it isn't captured by this metric. Thus, it is possible for the values of NumberOfMessagesSent and NumberOfMessagesReceived to be different.

That is nice to know, but I was missing the information: which metric shall I use if NumberOfMessagesSent won't work? I was being pragmatic here so I created an error, a message was sent to the DLQ as a result of a failed processing attempt. Now I looked at the queue in the AWS console under the monitoring-tab and I checked which metric spiked. It was ApproximateNumberOfMessagesVisible, which sounded suitable, so I used it.

Now I wanted to get alerted more often so I chose to build a lambda function that checks how many messages are in the DLQueue. I use Javascript / Typescript so I found this:
Code looked something like this:

const params = {
        QueueUrl: url,
        AttributeNames: ['ApproximateNumberOfMessagesVisible']

const resp = SQS.getQueueAttributes(params).promise()

It was kind of a bummer that the attribute I wanted was not in there, or better: it was not valid.

Valid Values: All | Policy | VisibilityTimeout | MaximumMessageSize | MessageRetentionPeriod | ApproximateNumberOfMessages | ApproximateNumberOfMessagesNotVisible | CreatedTimestamp | LastModifiedTimestamp | QueueArn | ApproximateNumberOfMessagesDelayed | DelaySeconds | ReceiveMessageWaitTimeSeconds | RedrivePolicy | FifoQueue | ContentBasedDeduplication | ...

My first attempt was to use CloudWatch metrics. So I tried this:

var params = {
  Dimensions: [
      Name: 'LogGroupName', /* required */
  MetricName: 'IncomingLogEvents',
  Namespace: 'AWS/Logs'

cw.listMetrics(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Metrics", JSON.stringify(data.Metrics));

but I could not get this working since I did not know what to add to Dimensions / Name to make this working.

Please note that I am not working very long with AWS (only 6 months). Maybe I am on a total wrong track.
Summarized: I want to achieve that my lambda gets the number of messages in a DLQ.

I hope someone can help me Cheers

1 Answer

You can use ApproximateNumberOfMessages to find out how many messages are in the queue.

profile picture
answered 6 months ago
  • Thank you for your answer I will try that, and post my reply if it works as expected.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions