Dev1ce
Dev1ce

Reputation: 5944

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

Upvotes: 6

Views: 10539

Answers (2)

kumar harshit
kumar harshit

Reputation: 672

Better late than never. Hope this helps you or someone searching for this problem.

When you configure SQS to trigger Lambda, the DLQ is supposed to be configured on the SQS(Since it would not be an Asynchronous invocation). Notice the 'Note' section in the link Source

Hence your serverless.yaml needs to declare the ReddrivePolicy in the main queue to refer to the DLQ. (Below)

service: dlq

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


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
                RedrivePolicy: 
                  deadLetterTargetArn: 
                    Fn::GetAtt: 
                      - "DeadLetterQueue"
                      - "Arn"
                  maxReceiveCount: 5
        DeadLetterQueue:
            Type: AWS::SQS::Queue
            Properties:
                QueueName: dlq

maxReceiveCount is set to 5 as per AWS Documentation.

Upvotes: 15

Mayank Raj
Mayank Raj

Reputation: 1624

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

Upvotes: -5

Related Questions