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

查看:19
本文介绍了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天全站免登陆