从节点包功能调用Lambda [英] Invoke a Lambda from a node package function

查看:52
本文介绍了从节点包功能调用Lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Lambda函数,该函数可以导入具有常用功能的节点包. Lambda 1将消息放入SQS,Lambda 2进行错误日志记录.共享功能之一调用Lambda 2,但第二次调用存在错误.

I have a Lambda function that imports a node package with common functions. Lambda 1 puts messages into SQS, Lambda 2 does error logging. One of the shared functions invokes Lambda 2, but there's an error on that second invocation.

Lambda 1:

exports.handler = function (event, context) {
  var pnmacCommon = require('./pnmacCommon.js'); //loading node package
  try {
    // this part omitted for space
    var aws = require('aws-sdk');
    var sqs = new aws.SQS({ region : 'us-west-2' });
    var params = {
      MessageBody: JSON.stringify(event),
      QueueUrl: '[url]'
    };
    sqs.sendMessage(params, function(err,data){
      if(err) {
        console.log('error:',"FAIL Send Message: " + err);
        context.done(err, "ERROR Put SQS");  // ERROR with message
        pnmacCommon.SvtLogErrorToElmah(application, "FAIL Send Message: " + err, context);
      }else{
        console.log('Message Sent:', queueUrl);
        console.log('data:',data.MessageId);
        context.done(null,'');  // SUCCESS 
      }
    }
  });
}
catch (error) {
  pnmacCommon.SvtLogErrorToElmah(application, 'SVTMessageBus_Client' +  error, context);
  context.done(error, "ERROR put SQS");
}

pnmacCommon.js:

pnmacCommon.js:

var SvtLogErrorToElmah = function (application, error, context) {
  console.log("SvtLogErrorToElmah=" + JSON.stringify(error, null, 2));
  // this part omitted for space
  var aws = require('aws-sdk');
  var lambda = new aws.Lambda({region: 'us-west-2' });
  lambda.invoke({
    FunctionName: "SVTExceptionLogger",
    Payload: JSON.stringify(message, null, 2)
  }, function (error2, data) {
    if (error2) {
      context.fail(error2);
    } else {
      context.fail(error);
  });
  context.done(null, message);
}
module.exports.SvtLogErrorToElmah = SvtLogErrorToElmah;

在Cloudwatch中,我可以看到SvtLogErrorToElmah函数被调用,但是在尝试调用第二个Lambda时失败.错误消息是TypeError: lambda.invoke is not a function.

Looking in Cloudwatch, I can see that the SvtLogErrorToElmah function gets called, but it fails when it tries to invoke the second Lambda. The error message is TypeError: lambda.invoke is not a function.

有什么想法吗?预先谢谢你.

Any ideas? Thank you in advance.

推荐答案

我刚刚遇到了与您相同的错误,而对我来说,aws-sdk版本的更新解决了该问题.

I just got the same error that you had, and for me the update of the aws-sdk version resolved the issue.

更新了 package.json [aws-sdk版本]

old(带有"TypeError:lambda.invoke不是函数")

old (with 'TypeError: lambda.invoke is not a function')

"dependencies": {
    "aws-sdk": "^2.1.17"
}

到(已修复错误)

"dependencies": {
    "aws-sdk": "^2.18.0"
}

这篇关于从节点包功能调用Lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆