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
Use Case
Users who already use Middy middleware in their functions should be able to make their function
handleridempotent via amakeHandlerIdempotentMiddy-compatible middleware.Solution/User Experience
The
makeHandlerIdempotentmiddleware should work with bothsyncandasyncfunctionhandlers. The middleware should accept an object with mandatorypersistenceStoreand an optionalconfigone. The former should be an instance of any class that extendsBasePersistenceLayer, while the latter should be an instance of the classIdempotencyConfig.Following the Powertools for Python decorator implementation, the middleware should:
POWERTOOLS_IDEMPOTENCY_DISABLEDenv variable has a truthy value (usingEnvironmentVariableService) -onBeforeonBeforeonBeforeIdempotencyHandler-onBeforeIdempotencyHandler.handle()method -onBeforeThis last step will ensure that the
IdempotencyHandlerwill 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
IdempotencyHandlermight not be enough and we'd need to add additional public methods to call in theonAfteroronErrorphase.Alternative solutions
No response
Acknowledgment