我有一个运行 lambda 函数并使用 lambda 层的代码。该代码在 AWS 环境中按预期运行。
对于简单的 lambda,我可以通过提供先决条件“env”文件并使用命令运行来在本地运行 lambda
node --input-type=module -e "import {handler} from './index.js'; console.log(await handler(JSON.parse(fs.readFileSync('./event.json'))));";
对于只有简单逻辑且没有节点模块的层,我尝试创建一个目录 /opt 并复制该层的内容,它按预期工作,无需任何代码更改。
但是当我尝试使用具有节点模块引用的 lambda 层运行代码时,它在本地抛出错误 module not found
有办法做到这一点吗?任何参考将不胜感激。
谢谢
您可以使用 AWS SAM CLI 运行本地测试吗?这就是 AWS 的建议,也是我过去在遇到类似问题时所做的。
# Invoking function with event file
$ sam local invoke "Ratings" -e event.json
# Invoking function with event via stdin
$ echo '{"message": "Hey, are you there?" }' | sam local invoke --event - "Ratings"
# For more options
$ sam local invoke --help