问题
I am deploying some apis to API Gateway using cdk. My problem is the file that contains the lambda(index.ts) can't import any files or npm modules outside that folder(folder named get-users
).
I tried copying node_modules folder and other files (which were outside the folder get-users
) to the folder get-users
and it worked perfectly.
Example error when importing lodash is as follows,
"errorType": "Runtime.ImportModuleError",
"errorMessage": "Error: Cannot find module 'lodash'",
"stack": [
"Runtime.ImportModuleError: Error: Cannot find module 'lodash'",
I am importing lodash
as follows,
import * as _ from "lodash";
I am importing shared files as follows,
import { validator } from "./shared/validators" // This one works
import { validator } from "../../shared/validators" // This one doesn't work
回答1:
If your lambda function uses dependencies, you'll have to package everything into a ZIP file and tell CDK where to find the ZIP. It will then upload it for you to S3. But the packaging will be your responsibility. You need to include all the dependencies as well as your code.
CDK currently supports 3 kinds of "Assets":
- InlineCode - useful for one-line-lambdas
- AssetCode - one-file lambdas without dependencies
- S3Code - existing lambda packages already uploaded in an S3 bucket
For your use-case, you'll need AssetCode, but instead of a directory, you'll point to local ZIP file.
Related answer: How to install dependencies of lambda functions upon cdk build with AWS CDK
Since quite a few people ask about this, I'll see if I can open-source my lambda packaging construct in Python. If yes, I'll link it here.
Edit: I opensourced a CDK construct which I use for lambda packaging with more than a hundred dependencies including NumPy.
https://gitlab.com/josef.stach/aws-cdk-lambda-asset
回答2:
After spending some time contemplating the very same issue I finally resorted to using webpack
to build my lambda packages, which is definitely the way I would recommend.
What you would do in that case is to point your AssetCode
to a directory where Webpack has previously packaged the whole thing e.g. src/your-function/build
.
Additional benefit is that you can configure Webpack to minify the files, so you speed up your deployment, but also your lambda cold start.
回答3:
I found the answer after some research. Problem was CDK not deploying the node_modules folder and other folders which are outside the folder which contains the lambda source file.
When creating the lambda file root path has to be added to the 'code' attribute so that it will take all the folders/files inside it and deploy to the lambda.
const pathToRoot = //absolute path to the root folder
const pathToHandler = //path to handler from root folder
const lambdaFunction: Function = new Function(construct, name, {
functionName: name,
code: Code.asset(pathToRoot),
handler: `${pathToHandler}/index.handler`,
runtime: Runtime.NODEJS_10_X
});
来源:https://stackoverflow.com/questions/57674293/lambda-cant-find-modules-from-outer-folders-when-deployed-with-cdk