为什么即使已经调用API网关回调,我的Lambda函数也会超时? [英] Why does my Lambda function time out even though the API Gateway callback has already been called?

查看:105
本文介绍了为什么即使已经调用API网关回调,我的Lambda函数也会超时?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个AWS API Gateway方法,可以将请求代理到AWS Lambda.但是,它在三秒钟后错误,并在日志中显示以下内容:

I have an AWS API Gateway method that proxies requests through to AWS Lambda. However, it errors after three seconds with the following in the logs:

转换前的端点响应主体:{"errorMessage":"2017-09-05T16:30:49.987Z 922186c0-9257-11e7-9db3-51921d5597a2任务在3.00秒后超时"}

Endpoint response body before transformations: {"errorMessage":"2017-09-05T16:30:49.987Z 922186c0-9257-11e7-9db3-51921d5597a2 Task timed out after 3.00 seconds"}

因此,我继续检查Node 6.10 AWS Lambda函数,以了解其为何超时.我在每个函数调用之前和之后都添加了日志记录语句.令人惊讶的是,它完成了它应该做的所有事情:调用了API Gateway回调,然后对数据库运行查询.只需0.6s,据我所知,没有其他代码可以运行了.但是,在三秒钟的剩余时间内,它似乎一直保持运行状态,然后超时. (我想这是因为我保持与数据库的连接保持打开状态.)

Thus, I went on to check my Node 6.10 AWS Lambda function to see why it was timing out. I added logging statements before and after every function call. Surprisingly, it did everything it's supposed to do: called the API Gateway callback, and run a query against the database after that. All that takes 0.6s, and as far as I'm aware there's no other code left to run. Nevertheless, it appears to keep on running for the rest of the three seconds and then timing out. (This is, I think, because I'm leaving a connection to the database open.)

我在callback调用之前和之后放置的logs语句指示该调用在不到半秒钟的时间内执行.但是,该响应似乎并没有传递给API Gateway,而三秒钟后的错误就是 .

The logs statements I placed before and after the callback call indicate that the that call is executed in under half a second. Yet, that response doesn't seem to make it to API Gateway, whereas the error after three seconds does.

造成这种情况的潜在原因是什么,我该如何调试呢?

What could be potential reasons for this, and how can I debug it?

推荐答案

默认情况下,在NodeJS Lambda函数中调用callback()函数不会结束该函数的执行.它将继续运行,直到事件循环为空.当您坚持打开数据库连接时,NodeJS Lambda函数在调用callback之后继续运行的一个常见问题会发生.您尚未发布任何代码,因此我无法提供具体建议,但是您需要确定是否要在代码中打开数据库连接或其他类似内容.

By default calling the callback() function in a NodeJS Lambda function does not end the function execution. It will continue running until the event loop is empty. A common issue with NodeJS Lambda functions continuing to run after callback is called occurs when you are holding on to open database connections. You haven't posted any code, so I can't give specific recommendations, but you would need to determine if you are leaving database connections open in your code or something similar.

或者,您可以更改行为,以使通过在context对象上设置callbackWaitsForEmptyEventLoop = false调用callback函数而立即终止执行.

Alternatively, you can change the behavior such that the execution ends as soon as the callback function is called by setting callbackWaitsForEmptyEventLoop = false on the context object.

这篇关于为什么即使已经调用API网关回调,我的Lambda函数也会超时?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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