我对Node.js和SAM都是新手。
除了我使用的是Node.js之外,我一直在关注亚马逊网络服务快速入门指南在线here。具体地说,我运行了以下命令:
版本:
▶ sam --version
SAM CLI, version 0.10.0
▶ node --version
v8.15.0内部版本:
▶ sam init --runtime nodejs
▶ cd sam-app/
▶ sam build
▶ sam package \
--template-file template.yaml \
--output-template-file packaged.yaml \
--s3-bucket $s3_bucket
▶ sam deploy \
--template-file packaged.yaml \
--stack-name sam-app \
--capabilities CAPABILITY_IAM这一切都很好地部署了堆栈和函数,但是当我测试它时,它就被破坏了,因为axios模块不在那里:
{
"errorMessage": "Cannot find module 'axios'",
"errorType": "Error",
"stackTrace": [
"Function.Module._load (module.js:474:25)",
"Module.require (module.js:596:17)",
"require (internal/module.js:11:18)",
"Object.<anonymous> (/var/task/app.js:2:15)",
"Module._compile (module.js:652:30)",
"Object.Module._extensions..js (module.js:663:10)",
"Module.load (module.js:565:32)",
"tryModuleLoad (module.js:505:12)",
"Function.Module._load (module.js:497:3)"
]
}但是,Axios模块似乎确实在build目录中:
▶ ls -1 sam-app/.aws-sam/build/HelloWorldFunction/node_modules
axios/
debug/
follow-redirects/
is-buffer/
ms/但不是在Lambda中:

我见过this的其他SO答案,但它没有帮助,因为我认为SAM应该包装它的所有依赖项。
有人知道哪里出了问题吗?
发布于 2019-01-08 04:45:00
sam build将使用node_modules创建sam-app artefact。但当您使用--template-file template.yaml执行sam package时,上载到s3的工件将不会包括应用程序依赖项,因为它会根据定义的模板文件打包您的应用程序,而不是从sam build构建的工件。
您应该删除sam package命令的--template-file参数。只需执行以下操作:
sam build
sam package --s3-bucket <your-bucket> --output-template-file packaged.yaml
sam deploy \
--template-file packaged.yaml \
--stack-name sam-app \
--capabilities CAPABILITY_IAM现在,应该使用package.json中定义的依赖项创建Lambda。
https://stackoverflow.com/questions/54074428
复制相似问题