ts2asl icon indicating copy to clipboard operation
ts2asl copied to clipboard

bug: asLambda implementation prevents access to Lambda context, and prevents use of middleware

Open dmeehan1968 opened this issue 1 year ago • 0 comments

Here is a typical example of using Powertools Logger, then adding the Lambda context so that its added to the structure log entries sent to CloudWatch:

import { Logger } from '@aws-lambda-powertools/logger';

const logger = new Logger({ serviceName: 'serverlessAirline' });

export const handler = async (_event, context): Promise<void> => {
  logger.addContext(context)
};

The existing asLambda implementation specifically expects the provided function have only one argument (the input) so that the signature can be used both for the exported handler (for the benefit of NodeJsFunction) and also as a call from within a state machine. If a second argument is added, it causes state machine calls to show a type error (missing argument), and when passed through the ts2asl compiler and error is thrown:

// logger setup as above
export const myLambda = asl.deploy.asLambda(async (input: any, context: Context) => {
    logger.addContext(context)
})

The error:

Error: Lambda declaration must be callable with 0 or 1 argument

With Lambda Powertools, clients for logging, metrics etc can be added by using middy middleware, which wraps the handler declaration. I've not tried it, but it looks like it would be incompatible with how ts2asl currently identifies lambda declarations.

dmeehan1968 avatar Oct 26 '23 17:10 dmeehan1968