Skip to content

Feature request: idempotent decorator for Lambda handler #1306

@saragerion

Description

@saragerion

Use Case

Users who write their Lambda functions as classes should be able to make their function handler idempotent via decorator.

Solution/User Experience

import { LambdaInterface } from '@aws-lambda-powertools/commons';
import { 
  idempotentHandler,
  DynamoDBPersistenceLayer,
  IdempotencyConfig,
} from '@aws-lambda-powertools/idempotency';

const config = new IdempotencyConfig({...});
const ddbPersistenceLayer = new DynamoDBPersistenceLayer({...});

class Lambda implements LambdaInterface {
  // Decorate your handler class method
  @idempotentHandler({
    persistenceStore: ddbPersistenceLayer,
    config
  })
  public async handler(_event: unknown, _context: unknown): Promise<void> {
    /* ...Function logic here... */
  }
}

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

The idempotentHandler decorator should be able to decorate an handler method of a LambdaInterface class, both async and sync. The decorator should accept an object with mandatory persistenceStore and an optional config one. The former should be an instance of any class that extends BasePersistenceLayer, while the latter should be an instance of the class IdempotencyConfig.

Following the Powertools for Python implementation, the decorator should:

  • return early if the POWERTOOLS_IDEMPOTENCY_DISABLED env variable has a truthy value (using EnvironmentVariableService)
  • use the provided config object or instantiate a new one if none is passed
  • register the Lambda context into the config object (used to manage timeouts)
  • instantiate an IdempotencyHandler
  • call & return the IdempotencyHandler.handle() method

This last step will ensure that the IdempotencyHandler will perform all the actions needed to make the function idempotent.

Alternative solutions

No response

Acknowledgment

Metadata

Metadata

Assignees

No one assigned

    Labels

    feature-requestThis item refers to a feature request for an existing or new utilityidempotencyThis item relates to the Idempotency UtilityrejectedThis is something we will not be working on. At least, not in the measurable future

    Type

    No type

    Projects

    Status

    Closed

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions