Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is AWS.Lambda.invoke `error` callback argument never populated?

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 ?!

like image 743
Francis Limousy Avatar asked Mar 08 '17 12:03

Francis Limousy


1 Answers

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.

like image 152
Francis Limousy Avatar answered Oct 03 '22 01:10

Francis Limousy