DynamoDB导致Lambda超时 [英] DynamoDB causing Lambda timeout

查看:77
本文介绍了DynamoDB导致Lambda超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,其中Lambda函数偶尔会超时,而没有通知该函数超时的错误消息。

I am experiencing an issue where Lambda functions occasionally time out without any error message other than a notification that the function timed out.

为了找到根目录这个问题,我在整个函数的各个位置添加了日志记录,并确定一切正常,直到第一个getItem()请求从DynamoDB读取数据为止。读取似乎花费了超过3.00秒的超时时间。

In order to find the root of the issue, I added logging at various points throughout my function and determined that everything functions properly until the first getItem() request to read data from DynamoDB. The read seems to be taking more than the 3.00 second timeout.

自然地,我检查了DynamoDB表以查看是否存在任何限制读取或错误。 DynamoDB的指标没有任何限制或错误,读取时间最多保持两位数的毫秒数。

Naturally, I checked my DynamoDB table to see if there were any throttled reads or errors. DynamoDB's metrics show no throttles or errors, and read times remain in the double-digit milliseconds at most.

很明显,某些地方出了问题或一路掉线。我该如何解决这个问题,或者至少发现它并重试读取?

Clearly something is going wrong or getting dropped along the way. How can I fix this issue or at least catch it and retry the read?

这是Web API的面向读取功能,因此响应时间至关重要。因此,增加的超时将无法解决问题。

This is a read-oriented function for a web API, so response times are critical. Hence, an increased timeout will not solve the issue.

dynamodb.getItem({
  "TableName": "tablename",
  "Key": { "keyname": { "S": "keyvalue" } },
  "AttributesToGet": [ "attributeA", "attributeB" ]
}, function(err, data) {
  if(err){
    context.done(err);
  } else {
    if("Item" in data){
      nextFunction(event, context);
    } else {
      context.done("Invalid key");
    }
  }
});

推荐答案

在大大增加了超时时间之后,我发现最终会引发网络错误:

After significantly increasing the timeout, I found that a network error is eventually thrown:

{
    "errorMessage": "write EPROTO",
    "errorType": "NetworkingError",
    "stackTrace": [
        "Object.exports._errnoException (util.js:870:11)",
        "exports._exceptionWithHostPort (util.js:893:20)",
        "WriteWrap.afterWrite (net.js:763:14)"
    ]
}

根据此线程。听起来问题似乎影响到Node.js 4.x及更高版本,但不影响0.10。这意味着您可以通过将Lambda运行时降级到Node.js 0.10来解决该问题,或者在使用aws-sdk时添加以下代码:

This issue appears to be caused by an issue between Node.js and OpenSSL according to this thread. It sounds like the issue affects Node.js 4.x and up but not 0.10. This means you can either resolve the issue by downgrading the Lambda runtime to Node.js 0.10 or adding the following code when using aws-sdk:

new AWS.DynamoDB({
  httpOptions: {
    agent: new https.Agent({
      rejectUnauthorized: true,
      secureProtocol: "TLSv1_method",
      ciphers: "ALL"
    })
  }
});

这篇关于DynamoDB导致Lambda超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆