【问题标题】:AWS Lambda Node module version mismatchAWS Lambda 节点模块版本不匹配
【发布时间】:2019-12-04 02:06:06
【问题描述】:

我正在关注automating deployments of Lambda-based applications 上的 AWS Lambda 教程,并通过 CodePipeline 和 CloudFormation 将一个简单的 lambda 函数上传到 AWS,但在尝试运行我的 lambda 函数时出现以下错误:

{
  "errorMessage": "Module version mismatch. Expected 48, got 46.",
  "errorType": "Error",
  "stackTrace": [
    "Object.Module._extensions..node (module.js:597:18)",
    "Module.load (module.js:487:32)",
    "tryModuleLoad (module.js:446:12)",
    "Function.Module._load (module.js:438:3)",
    "Module.require (module.js:497:17)",
    "require (internal/module.js:20:19)",
    "bindings (/var/task/node_modules/bindings/bindings.js:81:44)",
    "Object.<anonymous> (/var/task/node_modules/time/index.js:8:35)",
    "Module._compile (module.js:570:32)"
  ]
}

我的lambda函数内容如下...

samTemplate.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Outputs the time
Resources:
  TimeFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: nodejs6.10
      CodeUri: ./
      Events:
        MyTimeApi:
          Type: Api
          Properties:
            Path: /TimeResource
            Method: GET

buildspec.yml

version: 0.1
phases:
  install:
    commands:
      - npm install time
      - aws cloudformation package --template-file samTemplate.yaml --s3-bucket <redacted> 
                                   --output-template-file NewSamTemplate.yaml
artifacts:
  type: zip
  files:
    - NewSamTemplate.yaml

和 index.js

var time = require('time');
exports.handler = (event, context, callback) => {
    var currentTime = new time.Date(); 
    currentTime.setTimezone("America/Los_Angeles");
    callback(null, {
        statusCode: '200',
        body: 'The time in Los Angeles is: ' + currentTime.toString(),
    });
};

在 Lambda 函数的 AWS 页面上,我确实将 NodeJS 6.10 设置为运行时,所以我很困惑为什么会收到此错误。有什么想法吗?

【问题讨论】:

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


    【解决方案1】:

    我发现了问题。在构建步骤中,我使用的是 Node v4(根据教程的指导),而它应该是 v6。检查您的构建步骤是否与您的 lambda 函数正在使用的 Node 版本匹配!

    【讨论】:

    • 您能否准确说明您在何处或如何错误地将节点版本指定为 4?哪个文件包含此信息?等等?
    • @dashmug 在 CodeBuild 项目 -> 环境 -> 运行时/运行时版本中。在此处查看屏幕截图:imgur.com/a/4LlJH。此处使用 Lambda 设置 CodePipeline 的 AWS 教程:docs.aws.amazon.com/lambda/latest/dg/build-pipeline.html 在步骤 2.10 中显示“在版本中选择 aws/codebuild/nodejs:4.3.2”不正确,因为在步骤 1 的前面 ->“添加 samTemplate.yaml 文件...” YAML 文件状态 Runtime: nodejs6.10 所以你应该在步骤 2.10 中使用aws/codebuild/nodejs:6.3.1
    • 我是为未来的读者着想。你应该把它放在你的答案中,而不是作为评论,因为它更难阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-04-03
    • 2017-08-14
    • 2015-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-01
    相关资源
    最近更新 更多