Skip to content
25K
Console

Subscribe to queues with dead-letter queue

Messages not processed successfully by the primary subscriber function will be sent to the dead-letter queue after the retry limit is reached.

sst.config.ts
// create dead letter queue
const dlq = new sst.aws.Queue("DeadLetterQueue");
dlq.subscribe("subscriber.dlq");
// create main queue
const queue = new sst.aws.Queue("MyQueue", {
dlq: dlq.arn,
});
queue.subscribe("subscriber.main");
const app = new sst.aws.Function("MyApp", {
handler: "publisher.handler",
link: [queue],
url: true,
});
subscriber.ts
import type { SQSEvent, SQSHandler } from "aws-lambda";
export const main = async (event) => {
console.log(event);
throw new Error("Manual error");
};
export const dlq: SQSHandler = async (event: SQSEvent) => {
console.log(event);
return;
};
publisher.ts
import { Resource } from "sst";
import { SQSClient, SendMessageCommand } from "@aws-sdk/client-sqs";
const client = new SQSClient();
export const handler = async (event) => {
// send a message
await client.send(
new SendMessageCommand({
QueueUrl: Resource.MyQueue.url,
MessageBody: "Hello from the subscriber",
})
);
return {
statusCode: 200,
body: JSON.stringify({ status: "sent" }, null, 2),
};
};

View the full example.