-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
How to include node_modules when using typescript aws-cdk? #110
Comments
I have prepared a short example of how to include a set of node_modules below. The basic answer is that you need your node_modules folder to be in the same folder as specified in
The final tree structure should look something like this: ├── bin
│ └── lambda.ts
├── cdk.json
├── cdk.out
│ ├── LambdaStack.template.json
│ ├── asset.lotsOfLetters
│ │ ├── basic.js
│ │ ├── node_modules # Node modules found here
│ │ ├── package-lock.json
│ │ └── package.json
│ ├── cdk.out
│ ├── manifest.json
│ └── tree.json
├── lib
│ └── lambda-stack.ts
├── package-lock.json
├── package.json
├── src
│ ├── basic.js
│ ├── node_modules # this is inside the src, (in addition to the root one required to build)
│ ├── package-lock.json
│ └── package.json For reference, my import lambda = require('@aws-cdk/aws-lambda')
import cdk = require('@aws-cdk/core');
export class LambdaStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const myLambda = new lambda.Function(this, 'iHaveNodeModules', {
code: new lambda.AssetCode('src'),
handler: 'basic.handler',
runtime: lambda.Runtime.NODEJS_10_X
})
}
} My
After deploying, see the screenshot of what the Lambda console looks like with running a test event: |
Josh, it looks like a complete hack & absolutely dirty solution. Project structure supposed to look like this:
I expect cdk to be able to create all the stuff inside aws by I advise you to get out of cavern and look at something like serverless framework. |
Yes, I agree that it is hacky and dirty but I do not believe CDK was designed to be an entire build system for every single part of AWS' enormous stack. Serverless Framework is certainly better in its ability to package It's really no extra work to have your
and then replace the code in my example above with const myLambda = new lambda.Function(this, 'iHaveNodeModules', {
code: lambda.Code.fromAsset('lambdas.zip'),
handler: 'src.basic.handler',
runtime: lambda.Runtime.NODEJS_10_X
}) (Obviously make sure your CI only runs P.S. We actually switched away from Serverless Framework in favour of CDK because of CDK's much greater range of product offerings (Elastic Beanstalk) and intuitive IAM role creation (something we found to be lacking in Serverless because of its multi-vendor approach). |
@zoonman Sorry that no one got back to you sooner. @JoshM1994 is correct however, the CDK is not designed to be the end-all-be-all build system for everything in AWS. The AWS ecosystem is far too complex to have such specific deployment structure. Josh's solution mirrors exactly what I would have encouraged to deploy lambdas with dependencies. It is not the most optimal solution, but it is how you would deploy a lambda without the CDK, and therefore what it is mirroring. Each step can be very easily automated within All that said, this is a fairly specific structure, and I'm not sure it makes sense to create an example for it in this repo. If you would like to request adding a specific lambda feature, I recommend you create an issue on the main CDK repo rather than the one for code samples |
Closing this issue since a solution has been provided. Feel free to reopen. |
#14 It looks like this is where it has to be: the same place that I reference in code: lambda.Code.fromAsset('../backend/dist'), aws-samples/aws-cdk-examples#110
Thanks @JoshM1994 your suggestion worked great and seems like a simple solution to work around the dependency issue. I hadn't considered that you could have multiple node_modules within one "project". Simple workaround that doesn't feel like a hack at all. Thank you. |
Thanks for one of the best answers I've seen on github @JoshM1994 , it was exactly what I was looking for. |
Another vector on the original ask - albeit quite limited - is to include a specific |
Have you all tried aws-lambda-nodejs from the CDK main repo? |
We are using those and are really happy about it. We are experiencing small lambda deploys resulting in faster executions as well. Still experimental construct though. |
https://github.com/aws/aws-cdk/tree/master/packages/%40aws-cdk/aws-lambda-nodejs is the correct link |
Very Cool! I happened to a new issue. how to add two or more functions in Stack. I hope that trigger to deploy multi lambda function when commit the code Could you give me help |
In 2022...It was quite hard for me to figure out, whether this is still state-of-the-art or we can simplify dependencies now. Turns out it's possible now to include dependencies without hacks or complicated setup. For me, creating the lambda with Sample repositoryhttps://github.com/AntoniusGolly/cdk-lambda-typescript-boilerplate What it does:
I hope this helps someone as I would have appreciated it. |
❓ How to include node_modules using typescript aws-cdk?
My lambda is supposed to rely on the whole bunch of different modules, part of which is private.
How should I tackle this problem with conformity with aws-cdk?
Environment
Other information
Could you provide example when used some external node module, axios or joi or something else?
The text was updated successfully, but these errors were encountered: