Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(@aws-cdk/aws-lambda-nodejs): "error: Could not resolve" while bundling without Docker #12115

Closed
sbassin opened this issue Dec 16, 2020 · 13 comments · Fixed by #12125
Closed
Assignees
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.

Comments

@sbassin
Copy link

sbassin commented Dec 16, 2020

I'm trying to bundle my Lambda using NodejsFunction but am failing. If I set forceDockerBundling to true (default is false), bundling (during cdk synth) is successful.

Reproduction Steps

    this.lambdaFunction = new lambdaNodeJs.NodejsFunction(this, 'MyLambda', {
      functionName: 'my-function',
      runtime: lambda.Runtime.NODEJS_12_X,
      memorySize: 256,
      depsLockFilePath: '../lambda',
      entry: '../lambda/build/index.js',
      handler: 'main',
      bundling: {
        externalModules: ['graphql'], // I found out this might be a problem due to an error I saw when forceDockerBundling was set to true
      },
      environment: {
        MY_FLAG: 'true',
      },
    });

For what it's worth, my project is structured like:

├─ my-project
├── cdk
├── lambda

What did you expect to happen?

I expected the Lambda and (hopefully) its dependencies would be packaged using esbuild.

What actually happened?

$ cdk synth
Bundling asset FooStack/FooLambda/MyLambda/Code/Stage...
 > error: Could not resolve "../lambda/build/index.js"

1 error

Environment

  • CDK CLI Version : 1.78.0
  • Framework Version: 1.78.0
  • Node.js Version: 12.20.0
  • OS : macOS 10.15.7
  • Language (Version): TypeScript 3.9.7

Other

I've tried setting the entry to a file that doesn't exist, and I get a different error altogether. So I'm pretty sure that the file is being found, but some other problem is happening.

# cdk synth
Cannot find entry file at ../lambda/build/foo.js

This is 🐛 Bug Report

@sbassin sbassin added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Dec 16, 2020
@jogold
Copy link
Contributor

jogold commented Dec 16, 2020

Can you show the content of the file at ../lambda/build/index.js?

Also can you share the full stack trace of the error?

@sbassin
Copy link
Author

sbassin commented Dec 16, 2020

@jogold, the ../lambda/build/index.js is the result of a tsc build. Now that I think of it, when I was trying to bundle with Docker, I noticed a number of warnings about how dd-trace imports are set up. I didn't address them since they're just warnings, but now I'm curious… 🤔

"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const datadog_lambda_js_1 = require("datadog-lambda-js");
const dd_trace_1 = __importDefault(require("dd-trace"));
const processIncomingEvent_1 = __importDefault(require("./processIncomingEvent"));
dd_trace_1.default.init();
exports.main = datadog_lambda_js_1.datadog(processIncomingEvent_1.default);
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi9zcmMvaW5kZXgudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6Ijs7Ozs7QUFBQSx5REFBNEM7QUFDNUMsd0RBQThCO0FBQzlCLGtGQUEwRDtBQUUxRCxrQkFBTSxDQUFDLElBQUksRUFBRSxDQUFDO0FBRWQsT0FBTyxDQUFDLElBQUksR0FBRywyQkFBTyxDQUFDLDhCQUFvQixDQUFDLENBQUMiLCJzb3VyY2VzQ29udGVudCI6WyJpbXBvcnQgeyBkYXRhZG9nIH0gZnJvbSAnZGF0YWRvZy1sYW1iZGEtanMnO1xuaW1wb3J0IHRyYWNlciBmcm9tICdkZC10cmFjZSc7XG5pbXBvcnQgcHJvY2Vzc0luY29taW5nRXZlbnQgZnJvbSAnLi9wcm9jZXNzSW5jb21pbmdFdmVudCc7XG5cbnRyYWNlci5pbml0KCk7XG5cbmV4cG9ydHMubWFpbiA9IGRhdGFkb2cocHJvY2Vzc0luY29taW5nRXZlbnQpO1xuIl19

Here's the stack trace I get when running cdk synth -v:

Error: Subprocess exited with error 1
    at ChildProcess.<anonymous> (/Users/sbassin/.nvm/versions/node/v12.20.0/lib/node_modules/cdk/node_modules/aws-cdk/lib/api/cxapp/exec.ts:122:23)
    at ChildProcess.emit (events.js:314:20)
    at ChildProcess.EventEmitter.emit (domain.js:483:12)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

@jogold
Copy link
Contributor

jogold commented Dec 16, 2020

What happens if you point your entry to the .ts file?

Can you please share the full console output of the error from Bundling... and also with the Could not resolve part?

@sbassin
Copy link
Author

sbassin commented Dec 16, 2020

Pointing to the .ts file gives me the same error:

$ cdk synth
Bundling asset FooStack/FooLambda/MyLambda/Code/Stage...
 > error: Could not resolve "../lambda/src/index.ts"

1 error
Failed to bundle asset FooStack/FooLambda/MyLambda/Code/Stage, bundle output is located at /Users/sbassin/Development/my-project/cdk/cdk.out/bundling-temp-d9c47ce4518849903f729fa37b1175435ea4dc7ce04d627c791c0fb3fb76f676-error: Error: bash exited with status 1
Subprocess exited with error 1
Error: Subprocess exited with error 1
    at ChildProcess.<anonymous> (/Users/sbassin/.nvm/versions/node/v12.20.0/lib/node_modules/cdk/node_modules/aws-cdk/lib/api/cxapp/exec.ts:122:23)
    at ChildProcess.emit (events.js:314:20)
    at ChildProcess.EventEmitter.emit (domain.js:483:12)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:276:12)

Is that what you're looking for, @jogold?

And there's nothing in the directory indicated as the location of the bundle output.

I appreciate the help!

@jogold
Copy link
Contributor

jogold commented Dec 16, 2020

depsLockFilePath should point to your lockfile (not the directory containing it), can you test this?

If this doesn't work, it would help if you could share a full repro.

@jogold
Copy link
Contributor

jogold commented Dec 16, 2020

Can you also test with absolute paths?

@sbassin
Copy link
Author

sbassin commented Dec 16, 2020

I changed to:

      depsLockFilePath: '../lambda/package-lock.json',

but got the same error.

I just saw your edit about absolute paths and tried that. I haven't confirmed if I can deploy a working Lambda that way, but I did get cdk synth to complete successfully! That'll be a difficult long-term solution, but I understand this is an experimental feature you're working on. Thanks! 🎉

Please let me know if there's anything else you'd like me to try on my end. If you want a full repository, I'll have a bit of sanitizing to do.

@jogold
Copy link
Contributor

jogold commented Dec 16, 2020

Can you just confirm which of the paths must be absolute? Is it depsLockFilePath only?

@jogold
Copy link
Contributor

jogold commented Dec 16, 2020

Also note that the idea of the NodejsFunction is that you don't have to split your project between CDK and Lambda. Everything can live together and have a common package.json file. And you can point to a TS file.

@sbassin
Copy link
Author

sbassin commented Dec 16, 2020

Looks like only depsLockFilePath is requiring the absolute path. entry doesn't seem to require that.

For what it's worth, I was actually able to run cdk synth while providing the absolute path to the folder containing package-lock.json. I didn't have to include /package-lock.json.

I'm curious what the project organization would look like if I don't split the project. Possibly this?

├─ my-project
├── src
├─── cdk
├─── lambda1
├─── lambda2

One concern I'd have about merging them would be if I want different versions of Node.js or TypeScript in the Lambda from what CDK itself supports. I'm pretty new to JS/TS and CDK, so I'm not sure if that's a valid concern, though.

jogold added a commit to jogold/aws-cdk that referenced this issue Dec 17, 2020
@jogold
Copy link
Contributor

jogold commented Dec 17, 2020

I'm curious what the project organization would look like if I don't split the project.

I recommend the approach described in https://github.com/aws/aws-cdk/blob/v1.78.0/packages/@aws-cdk/aws-lambda-nodejs/README.md#nodejs-function where your handler lives next to the file where the NodejsFunction is defined.

If everything lives together you can share utils and types between your infra code and Lambda code.

(also you don't have to specify depsLockFilePath in this case, there's only one lock file at the root of your repo and it will be detected)

@mergify mergify bot closed this as completed in #12125 Dec 17, 2020
mergify bot pushed a commit that referenced this issue Dec 17, 2020
…th (#12125)

Ensure it's a file and make it absolute.

Closes #12115


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

@sbassin
Copy link
Author

sbassin commented Dec 17, 2020

Thanks for the help and the fix, @jogold! 🙇‍♂️

flochaz pushed a commit to flochaz/aws-cdk that referenced this issue Jan 5, 2021
…th (aws#12125)

Ensure it's a file and make it absolute.

Closes aws#12115


----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-lambda-nodejs bug This issue is a bug. needs-triage This issue or PR still needs to be triaged.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants