@aws-lambda-powertools/idempotency

中文文档
2.36.0发布于 2026/10/06MIT-0TypeScriptESM + CJS构建来源已验证近 30 天下载 48.3万

The idempotency package for the Powertools for AWS Lambda (TypeScript) library. It provides options to make your Lambda functions idempotent and safe to retry.

原始使用文档
文档目录14 节
  1. Intro
  2. Usage
  3. Function wrapper
  4. Decorator
  5. Middy middleware
  6. DynamoDB persistence layer
  7. Contribute
  8. Roadmap
  9. Connect
  10. How to support Powertools for AWS Lambda (TypeScript)?
  11. Becoming a reference customer
  12. Sharing your work
  13. Using Lambda Layer
  14. License

Powertools for AWS Lambda (TypeScript) - Idempotency Utility

Powertools for AWS Lambda (TypeScript) is a developer toolkit to implement Serverless best practices and increase developer velocity.

You can use the package in both TypeScript and JavaScript code bases.

Intro

This package provides a utility to implement idempotency in your Lambda functions. You can either use it to wrap a function, decorate a method, or as Middy middleware to make your AWS Lambda handler idempotent.

The current implementation provides a persistence layer for Amazon DynamoDB, which offers a variety of configuration options. You can also bring your own persistence layer by extending the BasePersistenceLayer class.

Usage

To get started, install the library by running:

npm i @aws-lambda-powertools/idempotency @aws-sdk/client-dynamodb @aws-sdk/lib-dynamodb

Next, review the IAM permissions attached to your AWS Lambda function and make sure you allow the actions detailed in the documentation of the utility.

Function wrapper

You can make any function idempotent, and safe to retry, by wrapping it using the makeIdempotent higher-order function.

The makeIdempotent function takes a reference to the function to be made idempotent as first argument, and an object with options as second argument.

When you wrap your Lambda handler function, the utility uses the content of the event parameter to handle the idempotency logic.

import {makeIdempotent} from '@aws-lambda-powertools/idempotency';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context, APIGatewayProxyEvent} from 'aws-lambda';

``
const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

const myHandler = async (
  event: APIGatewayProxyEvent,
  _context: Context
): Promise<void> => {
  // your code goes here here
};

export const handler = makeIdempotent(myHandler, {
  persistenceStore,
});  

You can also use the makeIdempotent function to wrap any other arbitrary function, not just Lambda handlers.

import {makeIdempotent} from '@aws-lambda-powertools/idempotency';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context, SQSEvent, SQSRecord} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

const processingFunction = async (payload: SQSRecord): Promise<void> => {
  // your code goes here here
};

const processIdempotently = makeIdempotent(processingFunction, {
  persistenceStore,
});

export const handler = async (
  event: SQSEvent,
  _context: Context
): Promise<void> => {
  for (const record of event.Records) {
    await processIdempotently(record);
  }
};

If your function has multiple arguments, you can use the dataIndexArgument option to specify which argument should be used as the idempotency key.

import {makeIdempotent} from '@aws-lambda-powertools/idempotency';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context, SQSEvent, SQSRecord} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

const processingFunction = async (payload: SQSRecord, customerId: string): Promise<void> => {
  // your code goes here here
};

const processIdempotently = makeIdempotent(processingFunction, {
  persistenceStore,
  // this tells the utility to use the second argument (`customerId`) as the idempotency key
  dataIndexArgument: 1,
});

export const handler = async (
  event: SQSEvent,
  _context: Context
): Promise<void> => {
  for (const record of event.Records) {
    await processIdempotently(record, 'customer-123');
  }
};

Note that you can also specify a JMESPath expression in the Idempotency config object to select a subset of the event payload as the idempotency key. This is useful when dealing with payloads that contain timestamps or request ids.

import {makeIdempotent, IdempotencyConfig} from '@aws-lambda-powertools/idempotency';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context, APIGatewayProxyEvent} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
    tableName: 'idempotencyTableName',
});

const myHandler = async (
  event: APIGatewayProxyEvent,
  _context: Context
): Promise<void> => {
  // your code goes here here
};

export const handler = makeIdempotent(myHandler, {
  persistenceStore,
  config: new IdempotencyConfig({
    eventKeyJmespath: 'requestContext.identity.user',
  }),
});

Additionally, you can also use one of the JMESPath built-in functions like powertools_json() to decode keys and use parts of the payload as the idempotency key.

import {makeIdempotent, IdempotencyConfig} from '@aws-lambda-powertools/idempotency';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context, APIGatewayProxyEvent} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

const myHandler = async (
  event: APIGatewayProxyEvent,
  _context: Context
): Promise<void> => {
  // your code goes here here
};

export const handler = makeIdempotent(myHandler, {
  persistenceStore,
  config: new IdempotencyConfig({
    eventKeyJmespath: 'powertools_json(body).["user", "productId"]',
  }),
});

Check the docs for more examples.

Decorator

You can make any function idempotent, and safe to retry, by decorating it using the @idempotent decorator.

import {idempotent} from '@aws-lambda-powertools/idempotency';
import {LambdaInterface} from '@aws-lambda-powertools/commons';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context, APIGatewayProxyEvent} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

class MyHandler extends LambdaInterface {
  @idempotent({persistenceStore: dynamoDBPersistenceLayer})
  public async handler(
    event: APIGatewayProxyEvent,
    context: Context
  ): Promise<void> {
    // your code goes here here
  }
}

const handlerClass = new MyHandler();
export const handler = handlerClass.handler.bind(handlerClass);

Using the same decorator, you can also make any other arbitrary method idempotent.

import {idempotent} from '@aws-lambda-powertools/idempotency';
import {LambdaInterface} from '@aws-lambda-powertools/commons';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import type {Context} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});

class MyHandler extends LambdaInterface {

  public async handler(
    event: unknown,
    context: Context
  ): Promise<void> {
    for (const record of event.Records) {
      await this.processIdempotently(record);
    }
  }

  @idempotent({persistenceStore: dynamoDBPersistenceLayer})
  private async process(record: unknown): Promise<void> {
    // process each code idempotently
  }
}

const handlerClass = new MyHandler();
export const handler = handlerClass.handler.bind(handlerClass);

The decorator configuration options are identical with the ones of the makeIdempotent function. Check the docs for more examples.

Middy middleware

If instead you use Middy, you can use the makeHandlerIdempotent middleware. When using the middleware your Lambda handler becomes idempotent.

By default, the Idempotency utility will use the full event payload to create an hash and determine if a request is idempotent, and therefore it should not be retried. When dealing with a more elaborate payload, where parts of the payload always change you should use the IdempotencyConfig object to instruct the utility to only use a portion of your payload. This is useful when dealing with payloads that contain timestamps or request ids.

import {IdempotencyConfig} from '@aws-lambda-powertools/idempotency';
import {makeHandlerIdempotent} from '@aws-lambda-powertools/idempotency/middleware';
import {DynamoDBPersistenceLayer} from '@aws-lambda-powertools/idempotency/dynamodb';
import middy from '@middy/core';
import type {Context, APIGatewayProxyEvent} from 'aws-lambda';

const persistenceStore = new DynamoDBPersistenceLayer({
  tableName: 'idempotencyTableName',
});
const config = new IdempotencyConfig({
  hashFunction: 'md5',
  useLocalCache: false,
  expiresAfterSeconds: 3600,
  throwOnNoIdempotencyKey: false,
  eventKeyJmesPath: 'headers.idempotency-key',
});

export const handler = middy(
  async (_event: APIGatewayProxyEvent, _context: Context): Promise<void> => {
    // your code goes here here
  }
).use(
  makeHandlerIdempotent({
    config,
    persistenceStore,
  })
);

Check the docs for more examples.

DynamoDB persistence layer

You can use a DynamoDB Table to store the idempotency information. This enables you to keep track of the hash key, payload, status for progress, expiration, and much more.

You can customize most of the configuration options of the table, i.e the names of the attributes. See the API documentation for more details.

Contribute

If you are interested in contributing to this project, please refer to our Contributing Guidelines.

Roadmap

The roadmap of Powertools for AWS Lambda (TypeScript) is driven by customers’ demand.
Help us prioritize upcoming functionalities or utilities by upvoting existing RFCs and feature requests, or creating new ones, in this GitHub repository.

Connect

How to support Powertools for AWS Lambda (TypeScript)?

Becoming a reference customer

Knowing which companies are using this library is important to help prioritize the project internally. If your company is using Powertools for AWS Lambda (TypeScript), you can request to have your name and logo added to the README file by raising a Support Powertools for AWS Lambda (TypeScript) (become a reference) issue.

The following companies, among others, use Powertools:

Sharing your work

Share what you did with Powertools for AWS Lambda (TypeScript) 💞💞. Blog post, workshops, presentation, sample apps and others. Check out what the community has already shared about Powertools for AWS Lambda (TypeScript).

Using Lambda Layer

This helps us understand who uses Powertools for AWS Lambda (TypeScript) in a non-intrusive way, and helps us gain future investments for other Powertools for AWS Lambda languages. When using Layers, you can add Powertools as a dev dependency to not impact the development process.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

版本与发布记录

@aws-lambda-powertools/idempotency 共发布 69 个版本,首次发布于 2023/07/11;过去 12 个月发布了 11 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
2.36.0latest2026/10/061 个月前
2.35.02026/08/182 个月前
2.34.02026/07/103 个月前
2.33.12026/05/255 个月前
2.33.02026/04/146 个月前
2.32.02026/03/197 个月前

其他发布通道:next → 2.0.0-alpha.2。

关于 @aws-lambda-powertools/idempotency 的常见问题

@aws-lambda-powertools/idempotency 是什么?

The idempotency package for the Powertools for AWS Lambda (TypeScript) library. It provides options to make your Lambda functions idempotent and safe to retry.

如何安装 @aws-lambda-powertools/idempotency?

在项目目录运行 npm install @aws-lambda-powertools/idempotency,或使用 pnpm add / yarn add / bun add。

@aws-lambda-powertools/idempotency 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@aws-lambda-powertools/idempotency 支持 ESM 还是 CommonJS?

同时提供 ES Module 与 CommonJS 入口,import 和 require() 都可以使用,打包工具会自动选择合适的版本。

@aws-lambda-powertools/idempotency 可以商用吗?

@aws-lambda-powertools/idempotency 使用 MIT-0 许可。声明的许可为 MIT-0,请阅读许可原文确认使用条件。以上为概括,不构成法律意见,请以许可原文为准。

@aws-lambda-powertools/idempotency 的下载量有多少?

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

@aws-lambda-powertools/idempotency 还在维护吗?

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