Search code examples
amazon-web-servicesamazon-sqsserverless-framework

Serverless Framework how to create an AWS SQS DeadLetter queue?


I am trying to create an AWS SQS Dead Letter Queue, using the serverless framework
The idea is to have a SQS to trigger a Lambda function,
and have another SQS as a DeadLetterQueue, ie. to pick up the message in case the Lambda fails or timesout

I did the following to create a test project -

mkdir dlq
cd dlq/
serverless create --template aws-nodejs

Following is my serverless.yaml -

service: dlq

provider:
  name: aws
  runtime: nodejs12.x
  region: ap-southeast-1
  role: arn:aws:iam::xxxx:role/dlqLambdaRole

plugins:
  - serverless-plugin-lambda-dead-letter

functions:
  dlq:
    handler: handler.hello
    events:
      - sqs:
          arn:
            Fn::GetAtt:
              - MainQueue
              - Arn

    deadLetter:
      targetArn:
        GetResourceArn: DeadLetterQueue

resources:
    Resources:  
        MainQueue:
            Type: AWS::SQS::Queue
            Properties:
                QueueName: main
        DeadLetterQueue:
            Type: AWS::SQS::Queue
            Properties:
                QueueName: dlq

I also tried the following -

service: dlq

provider:
  name: aws
  runtime: nodejs12.x
  region: ap-southeast-1
  role: arn:aws:iam::xxxx:role/dlqLambdaRole

plugins:
  - serverless-plugin-lambda-dead-letter

functions:
  dlq:
    handler: handler.hello
    events:
      - sqs:
          arn:
            Fn::GetAtt:
              - MainQueue
              - Arn

    deadLetter:
      sqs: dlq

resources:
    Resources:
      MainQueue:
        Type: AWS::SQS::Queue
        Properties:
          QueueName: main

But in both these cases, the framework is just creating a normal SQS

I am following this document -
https://www.serverless.com/plugins/serverless-plugin-lambda-dead-letter


Solution

  • To give you some background, Dead Letter Queue is just that, a normal SQS queue. it's the configuration at AWS Lambda that informs it to push message to this Queue whenever there is any error while processing the message.

    You can verify this from the management console by referring to the "Dead-letter queue service" under "Asynchronous invocation"

    enter image description here