【问题标题】:Lambda code works on node v8.10 but not nodejs v10.xLambda 代码适用于 node v8.10 但不适用于 nodejs v10.x
【发布时间】:2020-01-14 07:50:31
【问题描述】:

我正在学习使用 v8.10 完成的 lambda 课程。我正在尝试使用 nodejs v10.x,因为这是我将来想在我的项目中使用的。

我不明白为什么“const uuid = require('uuid');”行仅在 nodejs v10 中引发导入错误,但在 v8.10 中代码运行良好。

代码:

const aws = require('aws-sdk');
const s3 = new aws.S3();
const uuid = require('uuid');

exports.handler = async (event) => {
    console.log("Get the event to our S3POC class - " + JSON.stringify(event));

    const newUUID = uuid.v4();

    console.log("The file name is:" + newUUID);

    //put our sentence into the s3 bucket
    return s3.putObject({
        Bucket: "helloworld-s3.arkhadbot.com",
        Key: "test" + ".json"
    });
};

错误

Response:
{
  "errorType": "Runtime.ImportModuleError",
  "errorMessage": "Error: Cannot find module 'uuid'",
  "trace": [
    "Runtime.ImportModuleError: Error: Cannot find module 'uuid'",
    "    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:45:30)",
    "    at Module._compile (internal/modules/cjs/loader.js:778:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
    "    at Module.load (internal/modules/cjs/loader.js:653:32)",
    "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
    "    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
    "    at startup (internal/bootstrap/node.js:283:19)"
  ]
}

Request ID:
"e9c300ff-d12e-47b9-a45a-996a1f78b5f5"

Function Logs:
START RequestId: e9c300ff-d12e-47b9-a45a-996a1f78b5f5 Version: $LATEST
2019-09-12T12:18:34.743Z    undefined   ERROR   Uncaught Exception  {"errorType":"Runtime.ImportModuleError","errorMessage":"Error: Cannot find module 'uuid'","stack":["Runtime.ImportModuleError: Error: Cannot find module 'uuid'","    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:45:30)","    at Module._compile (internal/modules/cjs/loader.js:778:30)","    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)","    at Module.load (internal/modules/cjs/loader.js:653:32)","    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)","    at Function.Module._load (internal/modules/cjs/loader.js:585:3)","    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)","    at startup (internal/bootstrap/node.js:283:19)"]}
END RequestId: e9c300ff-d12e-47b9-a45a-996a1f78b5f5
REPORT RequestId: e9c300ff-d12e-47b9-a45a-996a1f78b5f5  Duration: 5098.92 ms    Billed Duration: 5100 ms    Memory Size: 128 MB Max Memory Used: 36 MB  
XRAY TraceId: 1-5d7a3795-41360f9ed871797c8ff32c3e   SegmentId: 4f526b023d16ac66 Sampled: false  
Unknown application error occurred
Runtime.ImportModuleError

【问题讨论】:

  • 您是否在节点版本更改后重新安装您的模块?
  • 我不知道该怎么做? UUID 应该像 aws-sdk 一样内置到 AWS 中。我还没有安装或做任何事情来向 lambda 添加自定义/第 3 方库
  • 对不起,我没有意识到这一点,看着npm package,你需要指定你想要的uuid版本。试试const uuidv1 = require('uuid/v1')
  • 同样的错误:“errorType”:“Runtime.ImportModuleError”,“errorMessage”:“错误:找不到模块'uuid/v4'”,“trace”:[
  • 相信 uuid/v4 等现在是外部包,因此您必须在本地 npm install uuid 并将其与您的 Lambda 函数捆绑。

标签: node.js amazon-web-services aws-lambda


【解决方案1】:

我有同样的问题。我所做的是在 lambda 的相应文件夹中初始化 npm 并将 uuid 包添加到其中。

【讨论】:

    【解决方案2】:

    根据接受的答案,现在要求 lambda 不会直接获得第三方支持。 (因为 Node 8 版本现在已被弃用,它可能仍然被接受)。

    const aws = require('aws-sdk');
    const s3 = new aws.S3();
    // const uuid = require('uuid'); comment this out
    
    exports.handler = async (event, context) => {
        console.log("Get the event to our S3POC class - " + JSON.stringify(event));
    
        // const newUUID = context.awsRequestId();
        const newUUID = context.awsRequestId;
    
        console.log("The file name is:" + newUUID);
    
        //put our sentence into the s3 bucket
        return s3.putObject({
            Bucket: "helloworld-s3.arkhadbot.com",
            Key: "test" + ".json"
        });
    };
    

    AWS 请求 ID 可能如下所示:requestId: 'daf9dc5e-1628-4437-9e2d-2998efaa73b4'

    【讨论】:

      【解决方案3】:

      由于 node.js v10 aws lambda 不支持从 lambda 本身导入库。

      来自文档:

      部署包是一个 ZIP 存档,其中包含您的功能代码 和依赖关系。如果使用,则需要创建部署包 用于管理函数的 Lambda API,或者如果您需要包含 AWS SDK 以外的库和依赖项。

      如果你的函数 依赖于 SDK for JavaScript 以外的库,安装它们 到带有 NPM 的本地目录,并将它们包含在您的部署中 包裹。如果需要,您还可以包含适用于 JavaScript 的 SDK 比运行时包含的版本更新,或确保 以后版本不变。

      更多关于AWS Lambda Deployment Package in Node.js

      2020 年 2 月 5 日更新:

      node.js 8.10 现已弃用,您应该使用 node.js 10 或 12。

      https://docs.aws.amazon.com/lambda/latest/dg/runtime-support-policy.html

      【讨论】:

        猜你喜欢
        • 2018-11-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-13
        • 2022-01-18
        • 2016-05-20
        • 1970-01-01
        • 2021-10-27
        相关资源
        最近更新 更多