Description
Use case
Especially for small lambdas, adding middy
as a dependency might not be worth it. Also, middy
doesn't provide good type checking for middlewares. But manual instrumentation is still a lot of work.
Why not allow for a simple way to auto-instrument that doesn't require another library or decorators? This would Keep it lean, ease the adoption of best practices, and be progressive.
Solution/User Experience
import { Tracer, lambdaTracer } from '@aws-lambda-powertools/tracer';
// No additional library needed:
// import middy from '@middy/core';
const tracer = new Tracer({ serviceName: 'serverlessAirline' });
const lambdaHandler = async (
_event: unknown,
_context: unknown
): Promise<void> => {
tracer.putAnnotation('successfulBooking', true);
};
// Use the middleware by passing the Tracer instance as a parameter, pass the handler to the middleware
// Still only one line of code.
export const handler = lambdaTracer(tracer)(lambdaHandler);
In case, multiple middlewares are needed, they can be composed like any other JavaScript function, e.g.
export const handler = lambdaTracer(tracer)(lambdaLogging(logger)(lambdaHandler));
or, with a compose
utility,
export const handler = compose(
lambdaTracer(tracer),
lambdaLogging(logger),
)(lambdaHandler);
See https://github.com/dbartholomae/lambda-middleware for more similar middlewares.
I'm happy to provide PRs (or a joint PR if that is preferred) to extract the shared code from the middy-specific middleware and add the functional middlewares.
Alternative solutions
This just adds another, simpler way to do the things that are already possible. The alternative would be to force all users to either rely on middy as dependency, use decorators, or do their own, manual setup.
Acknowledgment
- This feature request meets Powertools for AWS Lambda (TypeScript) Tenets
- Should this be considered in other Powertools for AWS Lambda languages? i.e. Python, Java, and .NET
Future readers
Please react with 👍 and your use case to help us understand customer demand.
Metadata
Metadata
Assignees
Labels
Type
Projects
Status