ServerlessLife / lambda-live-debugger

Remote debugging AWS Lambda functions
https://www.lldebugger.com/
Mozilla Public License 2.0
31 stars 3 forks source link

Issue with CloudFormation/SAM handler override #75

Closed rogerchi closed 3 days ago

rogerchi commented 3 days ago

We use the New Relic lambda layer for our SAM stacks, and as part of this we need to set the Handler property to newrelic-lambda-wrapper.handler, and set the actual handler in an environment variable: NEW_RELIC_LAMBDA_HANDLER: index.handler. We would like to be able to override the codePath using a custom getLambdas function in lldebugger.config.ts to account for this, but the custom getLambdas method is never reached because an error is thrown here: https://github.com/ServerlessLife/lambda-live-debugger/blob/6227d9032da8b061ec2015bbcd668749e0150d34/src/frameworks/samFramework.ts#L188-L190

What do you think of either deferring the check for codePath until after any custom getLambdas method is called or just returning codePath = fileWithExtension if file not found? https://github.com/ServerlessLife/lambda-live-debugger/blob/6227d9032da8b061ec2015bbcd668749e0150d34/src/frameworks/samFramework.ts#L183

ServerlessLife commented 3 days ago

Hi @rogerchi,

Thank you for opening the issue.

Yes, I can do that. I do not need to throw an error. I can just log a warning or an error and pick one of the files, as you suggested.

Could you tell me more about the use case? Is that a normal use or New Relic, or are you trying to make a workaround for an issue that there are two internal lambda expressions (New Relic and Lambda Live Debugger)? Note that I made this update that might help you.

Either way, the update makes a lot of sense, and I will prepare it right away.

rogerchi commented 3 days ago

For our CDK stacks the existing release works fine, this is for some of our stacks which are still on SAM. The SAM template is configured like this (I've actually been testing how to handle two different things, custom bundling without SAM's built-in esbuild path, and the New Relic changes):

  testDist:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: dist/testDist/
      Handler: newrelic-lambda-wrapper.handler
      Runtime: nodejs20.x
      Architectures:
        - x86_64
      MemorySize: 128
      Timeout: 10
      Policies:
        - AWSLambdaBasicExecutionRole
      Environment:
        Variables:
          NEW_RELIC_LAMBDA_HANDLER: lambda.lambdaHandler

After making the adjustment to the code to set codePath = fileWithExtension I have this custom config file that works:

import { type LldConfigTs } from 'lambda-live-debugger';

const config: LldConfigTs = {
  framework: 'sam',
  verbose: true,
  getLambdas: async (foundLambdas) => {
    console.log('foundLambdas', foundLambdas);
    const mappedLambdas = foundLambdas?.map((lambda) => {
      if (lambda.codePath.includes('dist/')) {
        return {
          ...lambda,
          codePath: lambda.codePath
            .replace('newrelic-lambda-wrapper', 'lambda.ts')
            .replace('dist/', 'services/')
            .replace('.js', '.ts'),
          handler: 'lambdaHandler',
          bundlingType: 'ESBUILD',
          forceBundle: true,
          esbuildOptions: {
            metafile: true,
            bundle: true,
            minify: true,
            sourcemap: true,
            target: 'es2020',
            format: 'cjs',
            entryPoints: ['lambda.ts'],
          },
        };
      }
      return lambda;
    });
    return mappedLambdas ?? [];
  },
};

export default config;
ServerlessLife commented 3 days ago

Thank you for the additional information.

I already prepared the PR: https://github.com/ServerlessLife/lambda-live-debugger/pull/76

PS: The Layer fix I mentioned :point_up: should work for any framework, not just CDK.

ServerlessLife commented 3 days ago

:tada: This issue has been resolved in version 1.2.2 :tada:

The release is available on:

Your semantic-release bot :package::rocket: