gpt4 book ai didi

amazon-web-services - 调用 API "cannot find module"时出现 AWS lambda 层错误

转载 作者:行者123 更新时间:2023-12-03 23:48:06 26 4
gpt4 key购买 nike

我尝试使用 AWS Lambda 层,观看了有关它的教程,但我收到错误“找不到模块...”

service: aws-nodejs 

package:
exclude:
- .gitignore
- package.json
- .git/**

provider:
name: aws
profile: sandbox
runtime: nodejs12.x

layers:
testLayer:
path: testLayer
compatibleRuntimes:
- nodejs12.x
allowedAccounts:
- '*'

functions:
hello:
handler: handler.hello
layers:
- arn:aws:lambda:us-east-1:*:layer:testLayer:15
events:
- http:
path: test
method: get
cors: true
当我部署它时,我的终端没有任何错误,在 AWS 上,我看到了我的层,当我下载它时,我有我的 package.json具有时刻依赖关系,以及具有时刻的 node_modules 文件夹
我的 handler.js 看起来像这样:
'use strict';
module.exports.hello = async (event, context) => {
const moment = require('moment')
const a = moment('2016-01-01')
return {
statusCode: 200,
body: JSON.stringify({
message: 'Hey' + a
}),
};
};
我的文件结构:
testLayer/
node_modules/
moment/
package.json
serverless.yml
handler.js
package.json
你知道我做错了什么吗?

最佳答案

是的,您的节点模块可能在您的 lambda 层中,但 lambda 抛出“找不到模块..”的错误。
这可能是因为您创建的 zip 与 AWS documentation. 中提到的目录结构不同。
根据官方文档:

Including Library Dependencies in a Layer You can move runtimedependencies out of your function code by placing them in a layer.Lambda runtimes include paths in the /opt directory to ensure thatyour function code has access to libraries that are included inlayers.

To include libraries in a layer, place them in one of the folderssupported by your runtime.

Node.js – nodejs/node_modules, nodejs/node8/node_modules (NODE_PATH)

Example AWS X-Ray SDK for Node.js

nodejs/node_modules/aws-xray-sdk

确保您的 zip 包含正确的目录结构,否则请尝试从 /opt/your_node_module_directory 导入您的模块

关于amazon-web-services - 调用 API "cannot find module"时出现 AWS lambda 层错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61325987/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com