@digitalmaas/serverless-plugin-lambda-dead-letter

中文文档
1.0.0发布于 2023/02/24MITCommonJS近 30 天下载 2555

serverless plugin that can configure a lambda with a dead letter queue or topic

原始使用文档
文档目录9 节
  1. What is it?
  2. Installation
  3. Basic Setup
  4. How do I use it?
  5. Method-1
  6. Method-2
  7. Method-3
  8. Remove DeadLetter Resource
  9. License

Serverless Lambda Dead Letter Queue Plugin

serverless NPM version digitalmaas NPM downloads standardjs

A Serverless plugin that can assign a dead letter SQS queue to a Lambda function. Works with serverless v1.4+, v2 and v3.

What is it?

Assigns a DeadLetterConfig to a Lambda function and optionally create a new SQS queue or SNS Topic with a simple syntax.

Failed asynchronous messages for Amazon Lambda can be be sent to an SQS queue or an SNS topic by setting the DeadLetterConfig. Lambda Dead Letter Queues are documented here.

At the time this plugin was developed AWS Cloudformation (and serverless) did not support the DeadLetterConfig property of the Lambda so we have introduced a plugin that calls UpdateFunctionConfiguration on the lambda after serverless deploys the CloudFormation stack.

Installation

From your target serverless project, run:

npm install @digitalmaas/serverless-plugin-lambda-dead-letter --save-dev

Basic Setup

Add the plugin to your serverless.yml:

plugins:
  - '@digitalmaas/serverless-plugin-lambda-dead-letter'

How do I use it?

Dead letter settings are assigned via a new deadLetter property nested under a function in a serverless.yml file.

There are several methods to configure the Lambda deadLetterConfig.

Method-1

DeadLetter Queue

Use the deadLetter.sqs to create a new dead letter queue for the function.

The resulting cloudformation stack will contain an SQS Queue and it's respective QueuePolicy.

Create new dead-letter queue by name

# 'functions' in serverless.yml

functions:
  createUser: # Function name
    handler: handler.createUser # Reference to function 'createUser' in code
    deadLetter:
      sqs:  createUser-dl-queue  # New Queue with this name

Create new dead-letter queue with properties

# 'functions' in serverless.yml

functions:
  createUser: # Function name
    handler: handler.createUser # Reference to function 'createUser' in code
    deadLetter:
      sqs:      # New Queue with these properties
        queueName: createUser-dl-queue
        delaySeconds: 60
        maximumMessageSize: 2048
        messageRetentionPeriod: 200000
        receiveMessageWaitTimeSeconds: 15
        visibilityTimeout: 300

DeadLetter Topic

Use the deadLetter.sns to create a new dead letter topic for the function.

The resulting cloudformation stack will contain an SQS Topic resource.

# 'functions' in serverless.yml

functions:
  createUser: # Function name
    handler: handler.createUser # Reference to function 'createUser' in code

    deadLetter:
      sns:  createUser-dl-topic

Method-2

Use the targetArn property to specify the exact SQS queue or SNS topic to use for Lambda dead letter messages. In this case the queue\topic must already exist as must the queue\topic policy.

Reference the ARN of an existing queue createUser-dl-queue

# 'functions' in serverless.yml

functions:
  createUser: # Function name
    handler: handler.createUser # Reference to function 'createUser' in code

    deadLetter:
      targetArn: arn:aws:sqs:us-west-2:123456789012:createUser-dl-queue

Method-3

If you created a queue\topic in the resource section you can reference it using the GetResourceArn pseudo method.

This will use the arn of the resource referenced by {logicalId}

    deadLetter:
      targetArn:
        GetResourceArn: {logicalId}

Note:

  • At present this only works for SQS queues or SNS Topics.
  • If a queue\topic is created in the resources section you will still need to add a resource for the respective queue\topic policy so that that lambda has permissions to write to the dead letter queue\topic.

In this example the createUser lambda function is using the new CreateUserDeadLetterQueue SQS queue defined in the resources section.

# 'functions' in serverless.yml

functions:
  createUser: # Function name

    handler: handler.createUser # Reference to function 'createUser' in code

    # ...

    deadLetter:
      targetArn:
        GetResourceArn: CreateUserDeadLetterQueue

resources:
    Resources:
      CreateUserDeadLetterQueue:
        Type: AWS::SQS::Queue
        Properties:
          QueueName: create-user-lambda-dl-queue

      CreateUserDeadLetterQueuePolicy:
        Type: AWS::SQS::QueuePolicy
        Properties:
          Queues:
            - Ref: CreateUserDeadLetterQueue

            # Policy properties abbreviated but you need more here ...

Remove DeadLetter Resource

If you previously had a DeadLetter target and want to remove it such that there is no dead letter queue or topic you can supply the deadLetter object with an empty targetArn. Upon deploy the plugin will run the Lambda UpdateFunctionConfiguration and set an empty TargetArn.

# 'functions' in serverless.yml

functions:
  createUser: # Function name

    handler: handler.createUser # Reference to function 'createUser' in code

    # ...

    # Set an empty targetArn to erase previous DLQ settings.
    deadLetter:
      targetArn:

License

MIT License.

This project has been forked from the original serverless-plugin-lambda-dead-letter and published under a different name, as the original has been abandoned.

For the complete information, please refer to the license file.

版本与发布记录

@digitalmaas/serverless-plugin-lambda-dead-letter 共发布 1 个版本,首次发布于 2023/02/24;过去 12 个月发布了 0 个版本。

版本发布日期距今
1.0.0latest2023/02/243 年前

关于 @digitalmaas/serverless-plugin-lambda-dead-letter 的常见问题

@digitalmaas/serverless-plugin-lambda-dead-letter 是什么?

serverless plugin that can configure a lambda with a dead letter queue or topic

如何安装 @digitalmaas/serverless-plugin-lambda-dead-letter?

在项目目录运行 npm install @digitalmaas/serverless-plugin-lambda-dead-letter,或使用 pnpm add / yarn add / bun add。

@digitalmaas/serverless-plugin-lambda-dead-letter 支持 TypeScript 吗?

该包的 package.json 没有声明类型入口。需要类型提示时,可以在 npm 上查找社区维护的 @types/digitalmaas__serverless-plugin-lambda-dead-letter 是否存在。

@digitalmaas/serverless-plugin-lambda-dead-letter 支持 ESM 还是 CommonJS?

以 CommonJS 形式发布,可以直接 require();在 ES Module 项目中也可以用 import 引入(Node.js 会把 module.exports 作为默认导出)。

@digitalmaas/serverless-plugin-lambda-dead-letter 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=4.0。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

@digitalmaas/serverless-plugin-lambda-dead-letter 可以商用吗?

@digitalmaas/serverless-plugin-lambda-dead-letter 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@digitalmaas/serverless-plugin-lambda-dead-letter 的下载量有多少?

根据 npm 官方下载统计,@digitalmaas/serverless-plugin-lambda-dead-letter 在 2026/10/12 – 2026/11/10 这 30 天内被下载 2,555 次,日均约 85 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@digitalmaas/serverless-plugin-lambda-dead-letter 还在维护吗?

最新版本 1.0.0 发布于 2023/02/24(3 年前);至今共发布 1 个版本,其中过去 12 个月发布了 0 个;首次发布于 2023/02/24。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。