简体   繁体   English

AWS Lambda 无法(间歇性地)连接到 AWS VPC 内的 MongoDB

[英]AWS Lambda unable to connect (intermittently) to MongoDB inside an AWS VPC

I am working with some AWS Lambda functions running inside VPC that needs to connect to a backend Mongo Atlas database for data storage/retrieval.我正在使用一些在 VPC 内运行的 AWS Lambda 函数,这些函数需要连接到后端 Mongo Atlas 数据库以进行数据存储/检索。

I am coding with Javascript, so i naturally used the official MongoDB Node JS client to establish the connection.我用Javascript编码,所以我自然使用官方的MongoDB Node JS客户端来建立连接。 The issue i am facing is that whenever i made a few concurrent lambda invocations, a few of them are unable to successfully establish a connection and basically hangs until Lambda timeout.我面临的问题是,每当我进行一些并发 lambda 调用时,其中一些无法成功建立连接并且基本上挂起直到 Lambda 超时。 For example, if 5 different functions were to be invoked, 2 might hang at connection attempt until Lamdba timeout.例如,如果要调用 5 个不同的函数,则 2 个可能会在连接尝试时挂起,直到 Lamdba 超时。 The connection is saved outside of the handler function for reuse in subsequent function invocation and the code is based on best practices specified here: https://docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/连接保存在处理程序函数之外,以便在后续函数调用中重用,代码基于此处指定的最佳实践: https : //docs.atlas.mongodb.com/best-practices-connecting-to-aws-lambda/

I have read through all the online resources and mongo documentations available as well but i am unable to find any solution.我也通读了所有可用的在线资源和 mongo 文档,但我找不到任何解决方案。 Has anyone ever encountered such issue with Lambda in VPC connection to Mongo and getting timeout?有没有人在与 Mongo 的 VPC 连接中遇到 Lambda 的此类问题并超时? How did you resolve it?你是怎么解决的? Thanks.谢谢。

I was having exactly the same issue.我遇到了完全相同的问题。

Resolved it by explicitly NOT following the best-practices, and instead opening (and closing) a new connection for every lambda request that requires one.通过明确不遵循最佳实践来解决它,而是为每个需要一个的 lambda 请求打开(和关闭)一个新连接。

In my setup, the connect() method takes between 20 and 70 milliseconds to complete, which I suppose I will more eagerly suffer than having intermittent and unreproducible hanging connections.在我的设置中, connect()方法需要 20 到 70 毫秒才能完成,我想我会比间歇性和不可复制的挂起连接更容易受苦。

For good measure, I added { poolSize: 1 } to the MongoClient connection opts.为了更好地衡量,我将{ poolSize: 1 }添加到 MongoClient 连接选项。

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

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