aws / aws-cdk

The AWS Cloud Development Kit is a framework for defining cloud infrastructure in code
https://aws.amazon.com/cdk
Apache License 2.0
11.41k stars 3.8k forks source link

[@aws-cdk/aws-lambda] local debugging #9390

Open MrZakos opened 3 years ago

MrZakos commented 3 years ago

@aws-lambda-nodejs

how to debug locally my lambda with @aws-cdk/aws-lambda-nodejs ? with @aws-cdk/aws-lambda I managed to debug successfully , but this is a similiar but diffrent creature because it's bundle it to a folder something like cdk.staging/asset-bundle-G0UqOf (where index.js and index.map.js are) if some 1 managed to do it - please share you findings (include launch.json)

UPDATE I managed to debug

{
    "version": "0.2.0",
    "configurations": [
      {
        "name": "Attach to SAM CLI",
        "type": "node",
        "request": "attach",
        "address": "localhost",
        "port": 9999,
        "remoteRoot": "/var/task",
        "localRoot": "${workspaceRoot}/.cdk.staging/asset-bundle-G0UqOf",   
        "protocol": "inspector",
        "stopOnEntry": false        
      }
    ]
  }

but I need to put a breakpoint inside /.cdk.staging/asset-bundle-G0UqOf/index.js which is HUGE LONG file.... not so user friendly

eladb commented 3 years ago

@jogold copy

eikeon commented 3 years ago

Adding the following to NodejsFunctionProps helps here (maps lines in stack traces back to source at least)

            environment: {
                NODE_OPTIONS: "--enable-source-maps",
            },
            bundling: {
                sourceMap: true, // include source map, defaults to false
            },
lprhodes commented 3 years ago

This one's driving me crazy. I'm doing all sorts of work-arounds to deal with the asset directory changing but then the result is ultimately that only the asset directory can be debugged using breakpoints.

@MrZakos @eikeon have you found a way to allow a debugger to be used on the original files (prior to the bundling)?

I'll keep digging around and create a blog post once complete, it's all been very frustrating.

MrZakos commented 3 years ago

@lprhodes you right , it drives you crazy, that's why I don't debug that way my current workaround : I created a test.ts file to run locally (node.js runtime) which runs local function to test whatever I need. it requires extra work to create a "service" which know to run other "services/functions"

//test.ts
(async () => {
    try {
        // await foo()
    } catch (error) {
        console.log("error >>", error)
    }
})()

//launch.json
{
    "version": "0.2.0",
    "configurations": [
    {
      "name": "Run Tester",
      "program": "${workspaceFolder}/lambda/tester/test.ts",
      "request": "launch",
      "skipFiles": [
        "<node_internals>/**"
      ],
      "type": "pwa-node"
    },
      {
        "name": "Attach to SAM CLI",
        "type": "node",
        "request": "attach",
        "address": "localhost",
        "port": 9999,
        "remoteRoot": "/var/task",
        "localRoot": "${workspaceRoot}/.cdk.staging/asset-bundle-VFH6MU",   
        "protocol": "inspector",
        "stopOnEntry": false        
      }
    ]
  }
pexxi commented 3 years ago

@MrZakos @lprhodes Using lambda-local (https://github.com/ashiina/lambda-local) has really helped in our development. Worth checking out.

danielleletartesonos commented 3 years ago

Curious if this feature request has been prioritized, or if there is another workaround besides creating a test that invokes the lambda or manually pasting the generated asset directory name cdk.out/asset.YYY.. into the launch config after running synth? When running synth --no-staging the template Metadata does not contain a reference to the source code like it does when the lambda is created via @aws-cdk/aws-lambda

huantbui commented 1 year ago

following

tim-finnigan commented 1 year ago

Linking related per @madeline-k: https://github.com/aws/aws-cdk/issues/9189