简体   繁体   English

AWS Lambda:任务超时

[英]AWS Lambda: Task timed out

We have been asked for my school project to write a Java code that runs in AWS Lambda.我们被要求为我的学校项目编写在 AWS Lambda 中运行的 Java 代码。 It is supposed to get the source code of the specific URLs and then upload it to an S3 bucket.它应该获取特定 URL 的源代码,然后将其上传到 S3 存储桶。 The Java code should be running on AWS Lambda. Java 代码应该在 AWS Lambda 上运行。

I get the source code to the String variable in Java.我得到了 Java 中 String 变量的源代码。 Then I have while loop that tries to write the String into a file in /tmp directory.然后我有 while 循环尝试将字符串写入 /tmp 目录中的文件。 Then the file is uploaded to S3.然后将文件上传到 S3。

Everything works but I get stuck with one specific URL.一切正常,但我被一个特定的 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.文件在我计算机上的 /tmp 目录中创建,然后上传到 S3 存储桶。 However, when I run the code in Lambda, I get the following error:但是,当我在 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 在这种特定情况下无法将文件写入其临时目录并且可以与其他人一起使用吗?

Amazon Lambda is designed to be used as an event-driven system that responds to events. Amazon Lambda 旨在用作响应事件的事件驱动系统。 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)某处发生了触发Lambda 的事情(例如,上传到 Amazon S3、数据进入 Amazon Kinesis 流、直接调用 Lambda 函数的应用程序)
  • The Lambda function is created , data from the trigger event is passed Lambda 函数被创建,来自触发器事件的数据被传递
  • The Lambda function runs Lambda 函数运行

Lambda functions are limited to a maximum execution time of 15 minutes (this was recently increased from the original 5 minutes timeout). Lambda 函数的最大执行时间限制为 15 分钟(最近从最初的 5 分钟超时增加了)。 The actual limit is configured when the Lambda function is created.实际限制是在创建 Lambda 函数时配置的。 The limit is in place because Lambda functions are meant to be small and quick rather than being large applications.限制已经到位,因为 Lambda 函数旨在小而快速,而不是大型应用程序。

Your error message says Task timed out after 15.00 seconds .您的错误消息说Task timed out after 15.00 seconds This means that AWS intentionally stopped the task once it hit a run-time of 15 seconds .这意味着一旦任务运行时间达到 15 秒,AWS 就会有意停止该任务 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.解决方法:增加 Lambda 函数配置页面上的超时设置。

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.您似乎将超时配置为 15 秒。您可以按照此屏幕截图中的说明增加超时,并且根据 lambda 设置,它允许您执行的最大时间为 15 分钟。

超时配置

在我的情况下,当任务在本地运行良好但在 Lambda 上超时时,这是因为我需要增加分配给 Lambda 实例的内存。

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

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.您可以将 lambda 函数的超时时间增加到 15 分钟,或者您可以增加对 lambda 函数的内存分配以使其更快。 You can increase memory upto 3008MB.您可以将内存增加到 3008MB。 Its minimum value is 128MB.其最小值为 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.如果您有以下情况,它会像这样: 为您的 lambda 函数分配大内存分配比执行该 lambda 函数所需的时间更少,反之亦然。 Large memory allocation would cost you more per execution of lambda function.每次执行 lambda 函数时,大内存分配会花费更多。 You need to figure out your balance with timeout time and memory allocated to lambda function.您需要计算超时时间和分配给 lambda 函数的内存的平衡。 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 函数 -> 配置 -> 基本设置以查找设置。 转到您的 Lambda 函数 -> 配置 -> 基本设置

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

Recently, I was working on a POC to work with AWS Lambda function.最近,我正在研究一个 POC 以使用 AWS Lambda 函数。 I was also facing same issue (Task timed out after 15.01 seconds).我也面临同样的问题(任务在 15.01 秒后超时)。 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.它应该提供失败的确切根本原因。

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.我在使用 Insights n XRay 进行调试时浪费了更多时间,但没有从中得到任何好处。

Finally i tried one thing, i have 2 subnet in my VPC, private n public.最后我尝试了一件事,我的 VPC 中有 2 个子网,私有和公共。 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.如果可行,请尝试使用单个子网运行 lambda。

Firstly, why write into /tmp/?首先,为什么要写入/tmp/? You write to the same location where the Lambda function is getting executed?您写入执行 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.但是,更好的做法是,如果您想将字符串写入 S3 文件,那么您可以创建一个 S3Object 并将其直接写入 AWS S3。 Here's a post that shows an example: https://stackoverflow.com/a/29844224/358013这是一个显示示例的帖子: https : //stackoverflow.com/a/29844224/358013

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM