Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda: Task timed out

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.

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.

Everything works but I get stuck with one specific URL. I have tracked the problem to this point:

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:

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?

like image 618
jansv Avatar asked Apr 23 '17 23:04

jansv


People also ask

How do you fix Lambda timeout?

To troubleshoot the retry and timeout issues, first review the logs of the API call to find the problem. Then, change the retry count and timeout settings of the AWS SDK as needed for each use case. To allow enough time for a response to the API call, add time to the Lambda function timeout setting.

What happens when AWS Lambda timeout?

When the specified timeout is reached, AWS Lambda terminates execution of your Lambda function. As a best practice, you should set the timeout value based on your expected execution time to prevent your function from running longer than intended.

Does AWS Lambda have a timeout?

Even though a Lambda function's maximum invocation timeout limit is 15 minutes, other AWS services may have different timeout limits. For example, Amazon API Gateway waits a maximum of 29 seconds for a Lambda function proxy invocation to complete.


1 Answers

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.

like image 150
John Rotenstein Avatar answered Oct 07 '22 19:10

John Rotenstein