从 NodeJS lambda 中的层请求文件

Iva*_*van 3 amazon-web-services aws-lambda

我正在尝试将 config.json 文件从 lambda 层导入到在 NodeJS 8.10 上运行的 lambda 中。我在网上找到了一些教程,但无法导入文件。至于官方文档,几乎没有说明如何进行。

我有一个LayerTester使用以下index.js文件调用的 Lambda:

const a = require('/opt/config/config.json');

exports.handler = event => {

    console.log(a);

    return true;

};
Run Code Online (Sandbox Code Playgroud)

我创建了一个名为 的新图层config。我准备了一个.zip文件,其中包含一个名为config.json.

呃 我尝试压缩文件夹并压缩文件本身。

然后我将该层链接到 lambda:

在此输入图像描述

我收到以下错误消息:

Response:
{
  "errorMessage": "Cannot find module '/opt/config/config.json'",
  "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/index.js:1:73)",
    "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)"
  ]
}

Request ID:
"2295bde4-fbaf-4880-89f4-a5b5eeca4a8b"

Function Logs:
START RequestId: 2295bde4-fbaf-4880-89f4-a5b5eeca4a8b Version: $LATEST
Unable to import module 'index': Error
    at Function.Module._resolveFilename (module.js:547:15)
    at Function.Module._load (module.js:474:25)
    at Module.require (module.js:596:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/var/task/index.js:1:73)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
END RequestId: 2295bde4-fbaf-4880-89f4-a5b5eeca4a8b
Run Code Online (Sandbox Code Playgroud)

我尝试了不同的路径,但没有成功。如何成功导入我的配置文件?

Rez*_*iri 5

如果您的config.json文件位于 zip 文件的根目录中,则应将其导入为/opt/config.json. 图层名称不是图层提取路径的一部分