AWS SDK for JavaScript
Developer Guide for SDK version 2.122.0

Using Dead Letter Queues in Amazon SQS

                                        Relationship between JavaScript environments, the SDK, and Lambda

This Node.js example shows you how to use a queue to receive and hold messages from other queues that the queues can't process.

The Scenario

A dead letter queue is one that other (source) queues can target for messages that can't be processed successfully. You can set aside and isolate these messages in the dead letter queue to determine why their processing did not succeed. You must individually configure each source queue that sends messages to a dead letter queue. Multiple queues can target a single dead letter queue.

In this example, a Node.js module is used to route messages to a dead letter queue. The Node.js module uses the SDK for JavaScript to use dead letter queues using this method of the AWS.SQS client class:

For more information about Amazon SQS dead letter queues, see Using Amazon SQS Dead Letter Queues in the Amazon Simple Queue Service Developer Guide.

Prerequisite Tasks

To set up and run this example, you must first complete these tasks:

Configuring the SDK

Configure the SDK for JavaScript by creating a global configuration object, setting the region, and providing credentials for your code. In this example, the credentials are provided using the JSON file you created.

// Load the SDK for JavaScript var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json');

Configuring Source Queues

After you create a queue to act as a dead letter queue, you must configure the other queues that route unprocessed messages to the dead letter queue. To do this, specify a redrive policy that identifies the queue to use as a dead letter queue and the maximum number of receives by individual messages before they are routed to the dead letter queue.

Create a Node.js module with the file name sqs_deadletterqueue.js. Be sure to configure the SDK as previously shown. To access Amazon SQS, create an AWS.SQS service object. Create a JSON object containing the parameters needed to update queue attributes, including the RedrivePolicy parameter that specifies both the ARN of the dead letter queue, as well as the value of maxReceiveCount. Also specify the URL source queue you want to configure. Call the setQueueAttributes method.

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Load credentials and set region from JSON file AWS.config.loadFromPath('./config.json'); // Create the SQS service object var sqs = new AWS.SQS({apiVersion: '2012-11-05'}); var params = { Attributes: { "RedrivePolicy": "{\"deadLetterTargetArn\":\"DEAD_LETTER_QUEUE_ARN\",\"maxReceiveCount\":\"10\"}", }, QueueUrl: "SOURCE_QUEUE_URL" }; sqs.setQueueAttributes(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

To run the example, type the following at the command line.

node sqs_deadletterqueue.js

This sample code can be found here on GitHub.