AWS Lambda Node模块版本不匹配

时间:2018-01-06 21:53:44

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

我正在关注automating deployments of Lambda-based applications上的AWS Lambda教程并通过CodePipeline和CloudFormation向AWS上传了一个简单的lambda函数,但在尝试运行我的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设置为运行时,所以我很困惑为什么我收到此错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

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