阅读背景:

AWS Lambda调用函数并不总是返回。

来源:互联网 

I am running an AWS Lambda function for image/video processing with Node 4.3 as a runtime. I am invoking my function from my webapp code with the node aws-sdk.

我正在运行一个AWS Lambda函数,用于将节点4.3作为运行时进行图像/视频处理。我正在用节点aws-sdk从webapp代码调用我的函数。

The issue is that when the function takes a long time to execute (e.g. 250 seconds), the invocation callback is never received, although I can clearly see in the aws cloudwatch logs that the function executed properly. On top of that the function is re-run again at least twice (this is probably related to the maxRetries parameter from the invocation: since it doesn't get a response back it retries).

问题是,当函数需要很长时间执行(例如250秒)时,不会收到调用回调,尽管我可以在aws cloudwatch日志中清楚地看到函数正确执行。除此之外,该函数至少要重新运行两次(这可能与调用的maxretry参数有关:因为它没有得到响应,所以要重试)。

What confuses me is that it works for quicker functions and my lambda function never times out. Anyone having such an issue ? Can it be related to the new 4.3 runtime? Note that I omit the context.succeed()or context.fail() as it is not required anymore, but I tried with it and it doesn't change a thing.

令我困惑的是,它适用于更快的函数,而我的lambda函数从不超时。有人有这样的问题吗?它是否与新的4.3运行时有关?注意,我省略了context.succeed()或context.fail(),因为它不再是必需的,但是我尝试了它,它不会改变任何东西。

Lambda code

λ代码

...
var handler = function (event, context, callback) {
    // video/image processing code
    // 
    // callback function
    ..., function(err, result) {
        if (err) {
            console.log("Error", err, err.stack);
            callback(err);
        } else {
            console.log("Done");
            callback(null, result);
        }
    }
};

Lambda Invocation

λ调用

var lambda = new AWS.Lambda;

var myEventObject = {...};
var payload = JSON.stringify('myEventObject');

var params = {
    FunctionName: 'myLambdaFunction'
    InvocationType: 'RequestResponse',
    LogType: 'None',
    Payload: payload
};

lambda.invoke(params, function(err, data) {
    if (err) console.log(err, err.stack); // an error occurred
    else     console.log(data);           // successful response
});

Lambda CloudWatch Log

λCloudWatch日志

REPORT RequestId: xxx   Duration: 206757.82 ms  Billed Duration: 206800 ms Memory Size: 1536 MB Max Memory Used: 354 MB 

1 个解决方案

#1


4  

as stated within the comments, the aws sdk has its own timeout config setting, that is by default set to 120 seconds (this is a different timeout setting than the lambda one).

如注释中所述,aws sdk有自己的超时配置设置,默认设置为120秒(这与lambda设置不同)。

in the current case, as the lambda runs for more this timeout, the sdk is assuming a failure and firing retries.

在当前情况下,随着lambda运行更多的超时,sdk假设出现故障并重新启动。

setting this config to 300 seconds should solve the problem. As Daniel T showed in the comment, temporary changing this can be achieved this way: var lambda = new AWS.Lambda({httpOptions{timeout: 300000}});

将这个配置设置设置为300秒应该可以解决这个问题。正如Daniel T在评论中所指出的,临时更改可以通过以下方式实现:var lambda = new AWS。λ({ httpOptions {超时:300000 } });


分享到: