Skip to content

Feature request: Add pure functional middleware (willing to contribute) #1833

Open
@dbartholomae

Description

@dbartholomae

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

Future readers

Please react with 👍 and your use case to help us understand customer demand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    discussingThe issue needs to be discussed, elaborated, or refinedfeature-requestThis item refers to a feature request for an existing or new utilityneed-customer-feedbackRequires more customers feedback before making or revisiting a decision

    Type

    No type

    Projects

    Status

    Ideas

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions