Skip to content

Feature request: idempotency Middy middleware #1293

Closed
@saragerion

Description

@saragerion

Use Case

Users who already use Middy middleware in their functions should be able to make their function handler idempotent via a makeHandlerIdempotent Middy-compatible middleware.

Solution/User Experience

import { 
  makeHandlerIdempotent,
  DynamoDBPersistenceLayer,
  IdempotencyConfig
} from '@aws-lambda-powertools/idempotency';
import middy from '@middy/core';

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

const lambdaHandler = async (_event: any, _context: any): Promise<void> => {
  /* ...Function logic here... */
}

export const handler = middy(lambdaHandler)
  .use(makeHandlerIdempotent({ config, persistenceLayer });

The makeHandlerIdempotent middleware should work with both sync and async function handlers. The middleware 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 decorator implementation, the middleware should:

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

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

Given how Middy handles requests and errors, there's a chance that the current public methods of IdempotencyHandler might not be enough and we'd need to add additional public methods to call in the onAfter or onError phase.

Alternative solutions

No response

Acknowledgment

Metadata

Metadata

Assignees

Labels

completedThis item is complete and has been merged/shippedfeature-requestThis item refers to a feature request for an existing or new utilityidempotencyThis item relates to the Idempotency Utility

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions