Skip to content

Feature request: idempotent decorator for function methods #1304

Closed as not planned
@saragerion

Description

@saragerion

Use case

Users who write their Lambda functions as classes should be able to make any calls method idempotent via decorator.

Solution/User Experience

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

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

class Lambda implements LambdaInterface {
  // Decorate your handler class method
  @idempotentMethod({
    persistenceStore: ddbPersistenceLayer,
    dataArgument: 'message',
    config
  })
  private myMethod(message: Record<string, unknown>, time: number): void {
    /* ...Function logic here... */
  }

  public async handler(_event: unknown, _context: unknown): Promise<void> {
    this.myMethod({ foo: 'bar' }, Date.now());
  }
}

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

The idempotentMethod decorator should be able to decorate any class method of a LambdaInterface class, both async and sync. The decorator should accept an object with mandatory persistenceStore and dataArgument fields, as well as an optional config one. The former should be an instance of any class that extends BasePersistenceLayer, the second should be a string that represents the argument to be used for idempotency (in the decorated method), 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