问题
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:
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;
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.
回答1:
It turns out this was an issue with variable scoping. In the shared function, we reuse the variable name aws
. Once I changed this to a different name, the problem went away.
回答2:
I just got the same error that you had, and for me the update of the aws-sdk version resolved the issue.
UPDATED the package.json [aws-sdk version]
old (with 'TypeError: lambda.invoke is not a function')
"dependencies": {
"aws-sdk": "^2.1.17"
}
to (fixed the error)
"dependencies": {
"aws-sdk": "^2.18.0"
}
来源:https://stackoverflow.com/questions/38621134/invoke-a-lambda-from-a-node-package-function