任务超时

我们已经被要求为我的学校项目编写一个 Java 代码,运行在 AWS Lambda。它应该获取特定 URL 的源代码,然后将其上传到 S3桶。Java 代码应该在 AWS Lambda 上运行。

我得到了 Java 中 String 变量的源代码。然后我有 while 循环,它尝试将 String 写入/tmp 目录中的文件。然后文件被上传到 S3。

一切都很正常,但是我被一个特定的网址困住了。我一直跟踪这个问题到现在:

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

最奇怪的是,当我在本地测试代码时,一切都正常。文件在我的计算机上的/tmp 目录中创建,然后将其上载到 S3桶中。但是,当我在 Lambda 中运行代码时,会得到以下错误:

Task timed out after 15.00 seconds

知道为什么 Lambda 在这种情况下不能将文件写入它的 temp 目录并与其他文件一起工作吗?

167693 次浏览

Amazon Lambda is designed to be used as an event-driven system that responds to events. The flow is:

  • Something happens somewhere that triggers Lambda (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 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.

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.

To fix: Increase the timeout setting on the configuration page of your Lambda function.

Firstly, why write into /tmp/? You write to the same location where the Lambda function is getting executed?

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

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.

I solved the problem by placing the AWS-SDK outside the function body:

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");
...

It seems you configured the timeout for 15 seconds.You may increase the timeout as described in this screenshot and as per lambda settings maximum time it allow you to execute is 15 minutes.

timeout configuration

For those running into this timeout problem when using async, note that the pattern is different for the handler for async functions.

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

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.

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. Goto Your Lambda function -> Configuration -> Basic Setting

I faced the same issue but it was occurring intermittently. I wasted way more time in debugging with Insights n XRay, but didnt get anything from that.

Finally i tried one thing, i have 2 subnet in my VPC, private n public. So i kept only Private subnet and removed the other one.

And Voila!!

Its not failing anymore.. But reason for this i still dont know, will keep it posted. Try running lambda with single subnet if it works or not.