Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Using Dead Letter Queues in Amazon SQS

Focus mode
Using Dead Letter Queues in Amazon SQS - AWS SDK for C++

Amazon SQS provides support for dead letter queues. A dead letter queue is a queue that other 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.

To create a dead letter queue, you must first create a redrive policy, and then set the policy in the queue’s attributes.

Important

A dead letter queue must be the same type of queue (FIFO or standard) that the source queue is. It must also be created using the same AWS account and AWS Region as the source queue.

Prerequisites

Before you begin, we recommend you read Getting started using the AWS SDK for C++.

Download the example code and build the solution as described in Getting started on code examples.

To run the examples, the user profile your code uses to make the requests must have proper permissions in AWS (for the service and the action). For more information, see Providing AWS credentials.

Create a Redrive Policy

A redrive policy is specified in JSON. To create it, you can use the JSON utility class provided with the AWS SDK for C++.

Here is an example function that creates a redrive policy by providing it with the ARN of your dead letter queue and the maximum number of times the message can be received and not processed before it’s sent to the dead letter queue.

Includes

#include <aws/core/Aws.h> #include <aws/core/utils/json/JsonSerializer.h>

Code

Aws::String MakeRedrivePolicy(const Aws::String &queueArn, int maxReceiveCount) { Aws::Utils::Json::JsonValue redrive_arn_entry; redrive_arn_entry.AsString(queueArn); Aws::Utils::Json::JsonValue max_msg_entry; max_msg_entry.AsInteger(maxReceiveCount); Aws::Utils::Json::JsonValue policy_map; policy_map.WithObject("deadLetterTargetArn", redrive_arn_entry); policy_map.WithObject("maxReceiveCount", max_msg_entry); return policy_map.View().WriteReadable(); }

See the complete example.

Set the Redrive Policy on your Source Queue

To finish setting up your dead letter queue, call the SQSClient class’ SetQueueAttributes member function with a SetQueueAttributesRequest object for which you’ve set the RedrivePolicy attribute with your JSON redrive policy.

Includes

#include <aws/sqs/SQSClient.h> #include <aws/sqs/model/SetQueueAttributesRequest.h> #include <iostream>

Code

Aws::SQS::Model::SetQueueAttributesRequest request; request.SetQueueUrl(srcQueueUrl); request.AddAttributes( Aws::SQS::Model::QueueAttributeName::RedrivePolicy, redrivePolicy); const Aws::SQS::Model::SetQueueAttributesOutcome outcome = sqsClient.SetQueueAttributes(request); if (outcome.IsSuccess()) { std::cout << "Successfully set dead letter queue for queue " << srcQueueUrl << " to " << deadLetterQueueARN << std::endl; } else { std::cerr << "Error setting dead letter queue for queue " << srcQueueUrl << ": " << outcome.GetError().GetMessage() << std::endl; }

See the complete example.

More Info

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.