Description
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 handler
s. 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 (usingEnvironmentVariableService
) -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
- This feature request meets Lambda Powertools Tenets
- Should this be considered in other Lambda Powertools languages? i.e. Python, Java