admin管理员组

文章数量:1355570

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
        }
     });

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

Share Improve this question edited Jul 10, 2018 at 19:10 Ronan Jouchet 1,3531 gold badge16 silver badges30 bronze badges asked Mar 8, 2017 at 12:57 Francis LimousyFrancis Limousy 1011 gold badge1 silver badge4 bronze badges 4
  • My question is... does err only get set when it fails to invoke it, and not when you return an error response? that seems to be the behavior you're seeing. I haven't used the javascript sdk to invoke lambda functions yet so i'm not sure of what's intended. The docs are somewhat unclear – Kevin B Commented Mar 8, 2017 at 23:06
  • 1 Looking at the docs over here that seems to support the idea that returning an error from the function won't result in err being set and an error status code, and that the error you provide will be in the payload. – Kevin B Commented Mar 8, 2017 at 23:10
  • 1 yes indeed; and it's not necessarily a bad way to do things; but the code samples such as link would be incorrect, hence my suspicion that this approach is wrong – Francis Limousy Commented Mar 8, 2017 at 23:22
  • Filed aws/aws-sdk-js issue #2134 - Lambda.invoke's error callback argument is never populated – Ronan Jouchet Commented Jul 10, 2018 at 18:58
Add a ment  | 

3 Answers 3

Reset to default 3

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.

You can solve this by checking the data.Payload for an error message

 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);
    //data.Payload is returned as a string, to check it, turn into JSON object
    let payload = JSON.parse(data.Payload);
    if (error) {
        console.log("Error invoking AWS " + error);
        // process error
    } else {
        // process payload
        // check for Lambda error  
        if(!payload.errorMessage) {
             console.log(data.Payload);
        } else {
            //Lambda error
            console.log(payload.errorMessage);
        }
    }
 });

Use context.fail() instead of callback(err) to get handler failing

本文标签: javascriptWhy is AWSLambdainvoke error callback argument never populatedStack Overflow