Search code examples
aws-lambdaamazon-sqs

AWS Lambda - different dead letter queue based on Lambda alias


I've two SQS queues one for test stage and one for prod stage and a single Lambda function with test and prod aliases, each alias is triggered by a SQS event of the corresponding queue.

I was wondering if there's a way to specifcy a DLQ for each alias (I'm using a SAM template to define the Lambda function) of the Lambda or I would like to understand which is the best practice to handle such requirement.


Solution

  • It's straightforward to add your DLQs - and the test-prod alias setup you have does not complicate things at all.

    Add a RedrivePolicy to your two Queue definitions. Here's the prod half the code:

      # template.yaml
    
      # new: define a new queue to be the prod DLQ
      MyProdDLQ:
        Type: AWS::SQS::Queue
        Properties:
          QueueName: this-queue-captures-errors-sent-from-the-prod-queue
    
      MyProdQueue:
        Type: AWS::SQS::Queue
        Properties:
          QueueName: this-queue-feeds-the-prod-alias-lambda
          VisibilityTimeout: 60  
        # new: assign the DLQ
        RedrivePolicy:
            deadLetterTargetArn: !GetAtt MyProdDLQ.Arn
            maxReceiveCount: 5
    

    Note: You might be tempted to define the DLQ on the lambda function itself. Confusingly, the DLQ on the lambda function is for asynchronous (event-driven) invocations, not SQS sources. The docs warn about this, but it is easy to miss.