node.js AWS lambda 函数在超时错误后停止工作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37232827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
AWS lambda function stops working after timed out error
提问by jjbskir
I have a simple lambda function that asynchronously makes an API calls and then returns data. 99% of the time this works great. When ever the API takes longer then the lambda configured timeout, it gives an error as expected. Now the issue is that when I make any subsequent calls to the lambda function it permanently gives me the timeout error.
我有一个简单的 lambda 函数,它异步进行 API 调用,然后返回数据。这在 99% 的情况下都很好用。当 API 花费的时间比 lambda 配置的超时时间长时,它会按预期给出错误。现在的问题是,当我对 lambda 函数进行任何后续调用时,它会永久给我超时错误。
"errorMessage": "2016-05-14T22:52:07.247Z {session} Task timed out after 3.00 seconds"
In order to test that this was the case I set the lambda timeout to 3 seconds and have a way to trigger these two functions within the lambda.
为了测试这种情况,我将 lambda 超时设置为 3 秒,并有办法在 lambda 中触发这两个函数。
Javascript
Javascript
function now() {
return response.tell('success');
}
function wait() {
setTimeout(function() { return response.tell('success'); }, 4000);
}
When I call the nowfunction there are no problems. When I call the waitfunction I get the timeout error and then any subsequent calls to nowgive me the same error.
当我调用该now函数时,没有问题。当我调用该wait函数时,我收到超时错误,然后任何后续调用都会now给我同样的错误。
Is this an expected behavior? I would think that any subsequent calls to the lambda function should work. I understand I can always increase the configuration timeout, but would rather not.
这是预期的行为吗?我认为对 lambda 函数的任何后续调用都应该有效。我知道我总是可以增加配置超时,但宁愿不这样做。
采纳答案by Jakub Koral
You should look for how your function handleworks with a specific
context.callbackWaitsForEmptyEventLoop
您应该寻找您的函数句柄如何与特定的
context.callbackWaitsForEmptyEventLoop
If that boolean-type is false, the setTimeout won't be ever fired, because you might've answered/handled the lambda invocation earlier.
But if the value of callbackWaitsForEmptyEventLoopis true- then your code will do what you are looking for.
如果那个布尔类型是false,setTimeout 将永远不会被触发,因为您可能已经回答/处理了更早的 lambda 调用。但是如果值callbackWaitsForEmptyEventLoop是true- 那么你的代码会做你想要的。
Also - it's probably easier to handle everything via callbacks directly, without the need for "hand-written" timeouts, changing configuration timeouts and so on...
此外 - 直接通过回调处理所有事情可能更容易,不需要“手写”超时,更改配置超时等等......
E.g.
例如
function doneFactory(cb) { // closure factory returning a callback function which knows about res (response)
return function(err, res) {
if (err) {
return cb(JSON.stringify(err));
}
return cb(null, res);
};
}
// you're going to call this Lambda function from your code
exports.handle = function(event, context, handleCallback) {
// allows for using callbacks as finish/error-handlers
context.callbackWaitsForEmptyEventLoop = false;
doSomeAsyncWork(event, context, doneFactory(handleCallback));
};
回答by A.Elnaggar
well if you defined 3 seconds in your function configuration, this timeout will override the time inside your code, so make sure to increase the timeout from your lambda function configs and try again the wait() and it should work!
好吧,如果您在函数配置中定义了 3 秒,则此超时将覆盖代码中的时间,因此请确保从 lambda 函数配置中增加超时并重试 wait(),它应该可以工作!
回答by Slava Elantsev
I've run into the same issue, in fact there are many cases when Lambda becomes unresponsive, e.g.:
我遇到了同样的问题,实际上有很多情况下 Lambda 变得无响应,例如:
Parsing not valid json:
exports.handler = function(event, context, callback) { var nonValidJson = "Not even Json"; var jsonParse = JSON.parse(nonValidJson);Accessing property of undefined variable:
exports.handler = function(event, context, callback) { var emptyObject = {}; var value = emptyObject.Item.Key;Not closing mySql connection after accessing RDS leads to Lambda timeout and then it becomes non-responsive.
解析无效的 json:
exports.handler = function(event, context, callback) { var nonValidJson = "Not even Json"; var jsonParse = JSON.parse(nonValidJson);访问未定义变量的属性:
exports.handler = function(event, context, callback) { var emptyObject = {}; var value = emptyObject.Item.Key;访问 RDS 后不关闭 mySql 连接会导致 Lambda 超时,然后变得无响应。
When I'm saying unresponsive it's literally not even loading, i.e. first print inside handler isn't printed, and Lambda just exits every run with timeout:
当我说没有响应时,它实际上甚至没有加载,即没有打印内部处理程序的第一次打印,并且 Lambda 只是在每次运行时退出并超时:
exports.handler = function(event, context, callback)
{
console.log("Hello there");
It's a bug, known by AWS team for almost a year:
https://forums.aws.amazon.com/thread.jspa?threadID=238434&tstart=0
这是一个错误,AWS 团队知道将近一年了:https:
//forums.aws.amazon.com/thread.jspa?threadID=238434&tstart=0
Unfortunately it's still not fixed, after some tests it's revealed that in fact Lambda tries to restart (reload the container?), there is just not enough time. If you set the timeout to be 10s, after ~4s of execution time Lambda starts working, and then in next runs comes to behave normally. I've also tried playing with setting:
不幸的是它仍然没有修复,经过一些测试后发现实际上 Lambda 试图重新启动(重新加载容器?),只是没有足够的时间。如果您将超时设置为 10 秒,则在大约 4 秒的执行时间后 Lambda 开始工作,然后在接下来的运行中开始正常运行。我也试过玩设置:
context.callbackWaitsForEmptyEventLoop = false;
and putting all 'require' blocks inside handler, nothing really worked. The only way to prevent Lambda becoming dead is setting bigger timeout, 10s should be more than enough as a workaround protection against this bug.
并将所有“需要”块放在处理程序中,没有任何效果。防止 Lambda 失效的唯一方法是设置更大的超时时间,10 秒应该足以作为针对此错误的解决方法保护。
回答by user2619052
In Amazon console AWS config you have to change the default timeout from 3 seconds to more (5 min max)
在 Amazon 控制台 AWS 配置中,您必须将默认超时从 3 秒更改为更多(最多 5 分钟)
回答by Karan
I think the problem is because of ip address we mention it in the AWS RDS inbound/ outbound.
我认为问题是因为我们在 AWS RDS 入站/出站中提到的 IP 地址。
If you are testing for now and your node.js is working on local ide and not on AWS, then you have to do following:
如果您现在正在测试并且您的 node.js 正在本地 ide 上运行而不是在 AWS 上运行,那么您必须执行以下操作:
Go to AWS RDS.
Click on DB instance.
- Click the name of that DB instance.
- Go to "Connect" section below, where you can find Security group roles.
- The type of the security groups will be inbound, outbound.
- Click on both one by one. It will open a new window.
- Again there will be two tabs for inbound and outbound.
- Click on both one after the other.
- Click "Edit".
- Select "Anywhere" instead of "Custom". P.S. Repeat for both inbound/outbound.
转到 AWS RDS。
单击数据库实例。
- 单击该数据库实例的名称。
- 转到下面的“连接”部分,您可以在其中找到安全组角色。
- 安全组的类型将是入站、出站。
- 一一点击。它将打开一个新窗口。
- 同样,入站和出站将有两个选项卡。
- 一个接一个地点击。
- 单击“编辑”。
- 选择“任何地方”而不是“自定义”。PS 对入站/出站重复。
All Set.
搞定。
回答by Amaresh
I just had to increase the timeout and the error is subsided. I increased it to 5 sec. This was okay for me because, I wasn't gonna use this Lambda in production.
我只需要增加超时时间,错误就会消退。我将其增加到 5 秒。这对我来说没问题,因为我不会在生产中使用这个 Lambda。

