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