aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[aws-lambda-nodejs] LambdaNodeJs bundles lambda code into cdk.staging directory when using cdk synth --no-staging

Open dave-graham opened this issue 4 years ago • 6 comments

When cdk synth --no-staging is run, it should still refer to the lambda file that has been developed, to enable local development and debugging.

I would expect the aws:asset:path to still refer to the original file, rather than bundled version when adding --no-staging, but it's looking like:

aws:asset:path: /Users/user-name/path-to-project/.cdk.staging/asset-bundle-L5zSs8

This is preventing me from using breakpoints in vscode to debug, using for example:

echo '{}' | sam local invoke -d 9999 ServerlessFunctionName

Or even just general debugging, as I need to run a cdk synth each time I make a change to the lambda.

Also thanks for the work on lambda-nodejs, it's great, and really simplifies some workflows I would otherwise have to manually put together.

This is :bug: Bug Report

dave-graham avatar Jul 21 '20 13:07 dave-graham

@eladb what do you think here? Should we simply disable bundling when staging is disabled?

if (props.bundling && !stagingDisabled) { https://github.com/aws/aws-cdk/blob/d28752513175c94fb2bc4da43374d7f2e66d6550/packages/%40aws-cdk/core/lib/asset-staging.ts#L91-L93

jogold avatar Jul 23 '20 10:07 jogold

Feels weird to completely disable asset bundling for all assets (whether they are part of your app or not). Would it make sense to specify which assets we want to skip? This whole behavior around disabling asset staging feels a bit hacky, but the use case of local lambda debugging still stands.

Also, I would include a warning if bundling is disabled.

eladb avatar Jul 26 '20 09:07 eladb

Curious what the status is on this bug? I'm experiencing the same issue.

danielleletartesonos avatar May 10 '21 16:05 danielleletartesonos

This issue has not received any attention in 1 year. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar May 10 '22 20:05 github-actions[bot]

I write this comment so the issue is not closed and can be solved

Sevi7 avatar May 10 '22 20:05 Sevi7

@Sevi7 @dave-graham Is this still an issue for you? I'm not sure I follow how you would be able to have the Lambda code updated without re-running synth since synth is what bundles the code?

corymhall avatar Aug 09 '22 18:08 corymhall

This issue has not received a response in a while. If you want to keep this issue open, please leave a comment below and auto-close will be canceled.

github-actions[bot] avatar Aug 11 '22 20:08 github-actions[bot]

@corymhall I think we did not mention to have the Lambda code updated without re-running synth. We are running synth to have the code updated, the problem is the cloudformation output template should still refer to the lambda file that has been developed instead of the path that it is written (see aws:asset:path):

Metadata:
      aws:cdk:path: shared/kinesis/DataStream/publishKinesisEvents/Resource
      aws:asset:path: /Users/adrian/app-2/packages/service-shared/cdk.out/asset.2bee75b1cdb5b7419c14402cb3dd78ec288cde085122101f6465e4eb1b9429d1
      aws:asset:is-bundled: true
      aws:asset:property: Code

The documentation for cdk synth --staging says:

Copy assets to the output directory (use --no-staging to disable, needed for local debugging the source files with SAM CLI)

And this is not happening. For aws-lambda it works but not for aws-lambda-nodejs as it is the case

Sevi7 avatar Aug 17 '22 10:08 Sevi7

@Sevi7 I'm not super familiar with how SAM CLI works. Does the SAM CLI also perform some bundling? I think the piece that I'm struggling to understand is why you would want it to point to the source code instead of the bundled code.

corymhall avatar Aug 17 '22 11:08 corymhall

@corymhall For sam local invoke it is not bundling, it is using the template.yaml generated by cdk synth to invoke locally the lambda in order to debug it. We want the lambdas in the template to point to the source code instead of the bundled code so we can use breakpoints in vscode to debug it (it's impossible to debug bundled code)

Sevi7 avatar Aug 17 '22 13:08 Sevi7

I would like to use this feature for local debugging too with SAM CLI. Any updates @corymhall?

bestickley avatar Dec 19 '22 00:12 bestickley

I'm surprised this hasn't seen more traction. How do other people debug/test lambda functions? Or run e2e tests.

@eladb what do you think here? Should we simply disable bundling when staging is disabled?

if (props.bundling && !stagingDisabled) {

https://github.com/aws/aws-cdk/blob/d28752513175c94fb2bc4da43374d7f2e66d6550/packages/%40aws-cdk/core/lib/asset-staging.ts#L91-L93

I go back to @jogold comments about

if (props.bundling && !stagingDisabled) {

This combined with a warning I don't see a problem with. I don't see a practical way of conditionally disabling staging only for selected resources.

In the situation where you're performing --no-staging I'm not seeing other use cases for not staging other than a debugging/local testing using sam or similiar where the use case is not testing CDK but testing the lambda code.

Here is the process I am using right now.

npx cdk synth --no-staging -e stack-name-api-rest 
sam local start-api --template cdk.out/stack-name-api-rest.template.json --region us-east-2

But like others have said --no-staging doesn't do what the label says it does.

blade2005 avatar Nov 08 '23 17:11 blade2005