Java AWS Lambda:任务超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43577746/
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: Task timed out
提问by jansv
We have been asked for my school project to write a Java code that runs in AWS Lambda. It is supposed to get the source code of the specific URLs and then upload it to an S3 bucket. The Java code should be running on AWS Lambda.
我们被要求为我的学校项目编写在 AWS Lambda 中运行的 Java 代码。它应该获取特定 URL 的源代码,然后将其上传到 S3 存储桶。Java 代码应该在 AWS Lambda 上运行。
I get the source code to the String variable in Java. Then I have while loop that tries to write the String into a file in /tmp directory. Then the file is uploaded to S3.
我得到了 Java 中 String 变量的源代码。然后我有 while 循环尝试将字符串写入 /tmp 目录中的文件。然后将文件上传到 S3。
Everything works but I get stuck with one specific URL. I have tracked the problem to this point:
一切正常,但我被一个特定的 URL 卡住了。我已经跟踪了这个问题:
try {
BufferedWriter out = new BufferedWriter(new FileWriter("/tmp/url.txt"));
out.write(source_code); //Replace with the string
//you are trying to write
out.close();
}
catch (IOException e) {
System.out.println("Exception ");
}
The weirdest thing is, when I test the code locally, everything works. File is created in /tmp directory on my computer and then it is uploaded to an S3 bucket. However, when I run the code in Lambda, I get the following error:
最奇怪的是,当我在本地测试代码时,一切正常。文件在我计算机上的 /tmp 目录中创建,然后上传到 S3 存储桶。但是,当我在 Lambda 中运行代码时,出现以下错误:
Task timed out after 15.00 seconds
Any idea why Lambda fails to write the file into its temp directory in this specific case and it works with others?
知道为什么 Lambda 在这种特定情况下无法将文件写入其临时目录并且可以与其他人一起使用吗?
回答by John Rotenstein
Amazon Lambda is designed to be used as an event-driven system that responds to events. The flow is:
Amazon Lambda 旨在用作响应事件的事件驱动系统。流程是:
- Something happens somewhere that triggersLambda (eg an upload to Amazon S3, data coming into an Amazon Kinesis stream, an application invoking the Lambda function directly)
- The Lambda function is created, data from the trigger event is passed
- The Lambda function runs
- 某处发生了触发Lambda 的事情(例如,上传到 Amazon S3、数据进入 Amazon Kinesis 流、直接调用 Lambda 函数的应用程序)
- Lambda 函数被创建,来自触发器事件的数据被传递
- Lambda 函数运行
Lambda functions are limited to a maximum execution time of 15 minutes (this was recently increased from the original 5 minutes timeout).The actual limit is configured when the Lambda function is created. The limit is in place because Lambda functions are meant to be small and quick rather than being large applications.
Lambda 函数的最大执行时间限制为 15 分钟(最近从最初的 5 分钟超时增加了)。实际限制是在创建 Lambda 函数时配置的。限制已经到位,因为 Lambda 函数旨在小而快速,而不是大型应用程序。
Your error message says Task timed out after 15.00 seconds
. This means that AWS intentionally stopped the task once it hit a run-time of 15 seconds. It has nothing to do with what the function was doing at the time, nor the file that was being processed.
您的错误消息说Task timed out after 15.00 seconds
。这意味着一旦任务运行时间达到 15 秒,AWS 就会有意停止该任务。它与函数当时在做什么无关,也与正在处理的文件无关。
To fix:Increase the timeout setting on the configuration page of your Lambda function.
解决方法:增加 Lambda 函数配置页面上的超时设置。
回答by blueskin
Firstly, why write into /tmp/? You write to the same location where the Lambda function is getting executed?
首先,为什么要写入/tmp/?您写入执行 Lambda 函数的同一位置?
However, a better thing to do is, if you want to write a string as an S3 file then you can create an S3Object and write it directly to AWS S3. Here's a post that shows an example: https://stackoverflow.com/a/29844224/358013
但是,更好的做法是,如果您想将字符串写入 S3 文件,那么您可以创建一个 S3Object 并将其直接写入 AWS S3。这是一个显示示例的帖子:https: //stackoverflow.com/a/29844224/358013
回答by andreamc
In my case when the task worked fine locally but timed out on Lambda, it was because I needed to increase the Memory allocated to the Lambda instance.
在我的情况下,当任务在本地运行良好但在 Lambda 上超时时,这是因为我需要增加分配给 Lambda 实例的内存。
回答by Wolfgang Bischoff
I solved the problem by placing the AWS-SDK outside the function body:
我通过将 AWS-SDK 放在函数体之外解决了这个问题:
var AWS = require("aws-sdk");
exports.handler = function(event, context, callback)
{
//var AWS = require("aws-sdk"); //Error: Task timed out after 3.00 seconds
var docClient = new AWS.DynamoDB.DocumentClient();
console.log("Lambda starts");
...
回答by user2226400
回答by jperezov
For those running into this timeout problem when using async
, note that the pattern is different for the handler for async functions.
对于那些在使用 时遇到此超时问题的人async
,请注意异步函数的处理程序的模式不同。
Instead of
代替
exports.handler = function (event, context, callback) {
callback(null, {
statusCode: 200,
body: JSON.stringify({/* return stuff here */})
});
};
it's
它是
exports.handler = async function (event, context) {
return {
statusCode: 200,
body: JSON.stringify({/* return stuff here */})
};
};
回答by Pankaj Jain
Recently, I was working on a POC to work with AWS Lambda function. I was also facing same issue (Task timed out after 15.01 seconds). I just increased memory allocation and it resolved the problem. Beauty is that I could get response with in couple of seconds. So, I think error is little misleading. It should provide exact root cause of failure.
最近,我正在研究一个 POC 以使用 AWS Lambda 函数。我也面临同样的问题(任务在 15.01 秒后超时)。我只是增加了内存分配,它解决了问题。美妙之处在于我可以在几秒钟内得到响应。所以,我认为错误几乎没有误导性。它应该提供失败的确切根本原因。
回答by lalit gangwar
You can increase the timeout for lambda function upto 15 min or you can increase the memory allocation to lambda function to make it fast. You can increase memory upto 3008MB. Its minimum value is 128MB.
It goes like this if you have:
Allocated large memory allocation to your lambda function than it takes less time to execute that lambda function and vice versa. Large memory allocation would cost you more per execution of lambda function. You need to figure out your balance with timeout time and memory allocated to lambda function. Don't just assign large chunk of memory so that you can see the result soon analyse the cost and your need also.
A figure is attached where change timeout and memory allocation. Goto Your Lambda function -> Configuration -> Basic Setting to find settings.
您可以将 lambda 函数的超时时间增加到 15 分钟,或者您可以增加对 lambda 函数的内存分配以使其更快。您可以将内存增加到 3008MB。其最小值为 128MB。如果您有以下情况,它会像这样: 为您的 lambda 函数分配大内存分配比执行该 lambda 函数所需的时间更少,反之亦然。每次执行 lambda 函数时,大内存分配会花费更多。您需要计算超时时间和分配给 lambda 函数的内存的平衡。不要只是分配大块内存,以便您可以很快看到结果,分析成本和您的需求。附一张图,其中更改超时和内存分配。转到您的 Lambda 函数 -> 配置 -> 基本设置以查找设置。