使用图层的 Lambda 函数

Lambda function using Layers

AWS Layer 的新手,尝试将 Layer 用作我的 Lambda 函数的依赖项。不过敲这个问题"errorType": "Runtime.ImportModuleError",谁能指出我的错误吗?

以下是我所做的:

  1. 压缩并通过 CLI 将我的 Firebase-Admin 依赖项上传到 Lambda。在 zip 中它包含 (node_modules, package-lock.json, pacakage.json)

  2. 在我的 Lambda 函数中,我已设置使用具有通过 AWS GUI 指定的版本的层。

我的 Lambda 函数代码

var admin = require('firebase-admin');
var serviceAccount = require("../maskedPath.json");

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: "https://maskedProject.firebaseio.com"
  });

var registrationToken = 'maskedToken';

exports.handler =  function(event, context, callback) {
    var message = {
        data: {
          score: '850',
          time: '2:45'
        },
        token: registrationToken
      };

    admin.messaging().send(message)
        .then((response) => {
            // Response is a message ID string.
            console.log('Successfully sent message:', response);
        })
        .catch((error) => {
            console.log('Error sending message:', error);
        });
}
  1. 用空测试用例执行函数{}

  2. 问题如下:

{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'firebase-admin'\nRequire stack:\n- /var/task/index.js\n- /var/runtime/UserFunction.js\n- /var/runtime/index.js",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'firebase-admin'",
    "Require stack:",
    "- /var/task/index.js",
    "- /var/runtime/UserFunction.js",
    "- /var/runtime/index.js",
    "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
    "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
    "    at Object.<anonymous> (/var/runtime/index.js:43:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:955:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:991:10)",
    "    at Module.load (internal/modules/cjs/loader.js:811:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:723:14)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:1043:10)",
    "    at internal/main/run_main_module.js:17:11"
  ]
}

我还尝试通过 CLI 使用下面的代码行配置我的 lambda 函数以使用该层,结果是 "LastUpdateStatus": "Successful"

aws lambda update-function-configuration --function-name maskedData --layers arn:aws:lambda:maskedData:layer:maskedData:1

设法找到了原因,显然它需要一个额外的文件夹层。

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

对于Lambda中使用的不同语言,你应该有一个额外的文件夹层,名称不同。对于我的情况,我创建了 nodejs 文件夹并将 node_modules 移入其中,然后将其重新上传为第二个版本。

xray-sdk.zip

└ nodejs/node_modules/aws-xray-sdk 

阅读更多内容:Including Library Dependencies in a Layer