121

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?

1
  • Does this help at all? (Just a shot in the dark, I don't really know)
    – ajb
    Commented Apr 24, 2017 at 0:10

9 Answers 9

156

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.

12
  • I agree with @John . You need to increase lambda timeout limit which can increased upto 5 minues. Your code is taking more than 15 seconds to process. Commented Apr 24, 2017 at 0:53
  • 1
    That is what I tried, I have increased the timeout to 2 minutes and still no luck. My Lambda function runs every 15 minutes, it is triggered by Jenkins. I do not understand when run locally, it works absolutely fine, but when on Lambda, it fails. This also happens with one specific site, the others are fine and Lambda processes it correctly. Also, do you think it will be better not to store anything in /tmp directory and upload it directly from memory to S3 bucket? Thanks a lot.
    – jansv
    Commented Apr 24, 2017 at 8:29
  • Now that you've increased the timeout to 2 minutes, do you get a "timed out after 120 seconds" message? If so, it means things aren't finished yet. You should log as much information as possible and then use the logs to figure out what's taking all the time. (That's the fun of programming!) Definitely delete files from /tmp after you've uploaded them or you'll run out of space. It would be good to log the size of file before you upload, so if it fails you'll have an idea what it was trying to do. Commented Apr 24, 2017 at 10:06
  • 2
    And yes, still get the same message even if increasing time out. But I think 120 seconds is way too long, because usually it takes around 7 seconds.
    – jansv
    Commented Apr 24, 2017 at 10:59
  • 1
    @ricardofunke Once a function has timed out, the execution environment is frozen/terminated. It is not possible to trigger something after the function has timed-out. However, you might be able to use AWS Step Functions to run the Lambda function and respond to timeout and then trigger some different logic such as waiting and then executing a different Lambda function. Commented Aug 10, 2022 at 0:06
55

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

26

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

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.

2
  • 1
    Increasing the allocated RAM will increase CPU proportionally (and your share of network bandwidth to some extent), so your function runs faster, and finishes sooner.
    – jarmod
    Commented Feb 11, 2021 at 1:39
  • Same, Node.JS based AWS Lambda function times out without any logs being printed out. What I first thought was an issue importing a random library, turned out to be a memory issue. Increasing the memory allocation fixed the issue. Checking the 'max memory used' after running the function helped with finding the solution.
    – Guy
    Commented Dec 22, 2021 at 11:55
23

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

3
  • Hey lalit, thank you for your answer, but please provide some context and explanation. Based on the question and the descriptions of this user, he/she is obviously quite new to this and is therefore not aware of things like hitting timeouts during data transfer or memory allocation and why this things happen. So please take this into account and add more details to your answer. Commented May 3, 2020 at 22:41
  • I have added details to my answer. Commented May 5, 2020 at 17:51
  • 1
    Thanks alot I had an image compressor lambda going on with 128mb memory and 3 second timeout. It kept failing for png files, after upgrading to 256mb and 5 sec timout everything worked fine!
    – Jules
    Commented Jul 12, 2021 at 8:17
9

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

2
  • 5
    This is a good tip. For more details - this will help for two reasons: 1) The require now happens only on lambda instance startup, not for each lambda call, so you save some time when you aren't doing a cold start. 2) When the lambda instance starts up (cold start), it has an extra boost of CPU to use for things outside of the function body like this, so it happens faster than it would otherwise.
    – andreamc
    Commented Feb 27, 2019 at 21:25
  • This saved me after hours of debugging, a really simple fix! Commented Oct 23, 2021 at 22:04
7

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.

6
  • How is the error misleading? It timed out. Period. If you were executing a heavy task in a very low profile machine, that's another thing. Also, the memory usage is always seamlessly available in CloudWatch logs. If you read the logs you can judge whether you should increase the allocated memory or not. Commented Jul 19, 2019 at 13:35
  • This worked for me. Seems like you essentially have two ways to fix this issue: increase the memory allocation or increase the execution timeout limit.
    – openwonk
    Commented Aug 17, 2019 at 2:57
  • @ThalesMinussi I agree the error in Pankaj's case isn't misleading, however, it's often worth increasing the memory for lambdas even if the reported memory is way less than the provisioned memory. Say you have 1 GB of provisioned memory, and the reports say the function only needed 200 MB. You might think 'oh, might as well decrease the provisioned memory to 500 MB' - but in fact, it's not just about memory, with higher provision you get a higher-tier CPU as well, it seems. Eg. my functions use 250-350 MB and there's a huge difference in response time if I allocate 1GB, 2GB or 3GB of memory.
    – l.varga
    Commented Sep 4, 2019 at 0:20
  • Yes, it written under the setting: "Your function is allocated CPU proportional to the memory configured." Commented Apr 26, 2020 at 13:40
  • While increasing RAM helps in certain cases, it's not guaranteed to be a solution here. Imagine a scenario where the Lambda function makes a remote API call and waits for the response. If the remote server takes longer than the Lambda function timeout then there is no amount of RAM that can help you.
    – jarmod
    Commented Feb 11, 2021 at 1:44
2

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.

1
  • Thanks! I had the exact same issue. I don't understand the reason either, but keeping the private subnets really did the trick! Commented Oct 8, 2021 at 10:26
-2

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

3
  • 1
    /tmp/ is only location which lambda let you write files Commented Mar 7, 2018 at 10:45
  • Regardless of writing in /tmp/ or not, timeouts need to be tackled in long running lambda functions. This answer doesn't address the question in any way!
    – Syed Waqas
    Commented Jan 10, 2019 at 7:30
  • @SyedWaqas This is a very old answer and things might have changed in the way AWS Lambda gets executed. The answer advices an alternative. Just because the OP is trying to do something doesn't mean that is the right way.
    – Chenna V
    Commented Mar 20, 2020 at 17:26

Not the answer you're looking for? Browse other questions tagged or ask your own question.