I'm trying to invoke a lambda function from an external node.js app (i.e not on AWS). I used code samples from the AWS documenattion and it works great for nominal cases. However, in the case of an error, it never gets processed properly by my calling function.
My invocation code is as follow:
// AWS.config before
var pullParams = {
FunctionName : 'myFunctionName',
InvocationType : 'RequestResponse',
LogType : 'None',
Payload : JSON.stringify({
"myParam" : params
})
};
var lambda = new AWS.Lambda();
lambda.invoke(pullParams, function(error, data) {
console.log("error value: " + error);
if (error) {
console.log("Error invoking AWS " + error);
// process error
} else {
// process payload
}
});
In my lambda function, I raise an error if no params are provided and provide an error message
exports.handler = (event, context, callback) => {
var params = event.myParam;
if (!params) {
var error = new Error("Appropriate error message");
callback(error);
// In Node ≥8, could also be expressed making handler `async` and `throw`ing here
}
else {
// do normal processing and create payload
callback(null, "Payload");
}
}
However, in the invoke
callback, error
is always null (even when the lambda goes through the error code path), and when going through the error code path, then data
contains errorMessage, errorType, stackTrace
keys.
What am I missing here? Shouldn't the invoke
binding of aws-sdk
populate error
rather than making me check for data.errorMessage
?!
I can use a workaround such as testing on the payload received:
//if (invocationError){
if (data.FunctionError){ <== null if no error, "Handled" if an error is returned
This technically works but its different from every code snipplet I found around.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With