How do I set up SQS DLQ and CloudWatch alert for messages?
In this guide, we will set up an Amazon SQS queue with a Dead Letter Queue (DLQ) and configure a CloudWatch alarm to monitor the messages in the DLQ. This setup ensures that messages that cannot be processed are moved to the DLQ and alerts are triggered when there are messages in the DLQ.
import * as aws from "@pulumi/aws";
// Create a Dead Letter Queue (DLQ)
const dlq = new aws.sqs.Queue("dlq", {
messageRetentionSeconds: 1209600, // Retain messages for 14 days
});
// Create the main SQS queue with a redrive policy to the DLQ
const mainQueue = new aws.sqs.Queue("mainQueue", {
redrivePolicy: JSON.stringify({
deadLetterTargetArn: dlq.arn,
maxReceiveCount: 5, // After 5 failed receives, send message to DLQ
}),
});
// Create a CloudWatch alarm to monitor the DLQ
const dlqAlarm = new aws.cloudwatch.MetricAlarm("dlqAlarm", {
alarmDescription: "Alarm when there are messages in the DLQ",
metricName: "ApproximateNumberOfMessagesVisible",
namespace: "AWS/SQS",
statistic: "Sum",
period: 300,
evaluationPeriods: 1,
threshold: 1,
comparisonOperator: "GreaterThanOrEqualToThreshold",
alarmActions: [], // Add SNS topic ARN or other actions here
dimensions: {
QueueName: dlq.name,
},
});
Key Points
- Dead Letter Queue (DLQ): A special queue used to store messages that cannot be processed successfully.
- Redrive Policy: Configures the main queue to send messages to the DLQ after a specified number of failed processing attempts.
- CloudWatch Alarm: Monitors the DLQ for visible messages and triggers an alert when the threshold is met.
Summary
We created an SQS queue with a Dead Letter Queue (DLQ) and set up a CloudWatch alarm to monitor the DLQ. This configuration helps in handling message processing failures and ensures that alerts are triggered when there are messages in the DLQ.
Deploy this code
Want to deploy this code? Sign up for a free Pulumi account to deploy in a few clicks.
Sign upNew to Pulumi?
Want to deploy this code? Sign up with Pulumi to deploy in a few clicks.
Sign upThank you for your feedback!
If you have a question about how to use Pulumi, reach out in Community Slack.
Open an issue on GitHub to report a problem or suggest an improvement.