从Lambda连接到DocumentDB时超时

问题描述

我可以使用mongo shell从Cloud9控制台成功连接到群集(目前为1个实例),但是尝试从lambda函数中连接到群集浪费了时间。

设置:

  • 群集和lambda都在同一VPC中(认)
  • TLS已启用
  • 集群位于名为DemodocDB的安全组中,该组已入站 两个安全组27017的规则:cloud9和DefaultSG
  • Lambda在认VPC中,也在DefaultSG安全组中

代码

module.exports = {
    CONNECTION_STRING: 'mongodb://<user>:<pwd>@xxx.us-east-1.docdb.amazonaws.com:27017',    SSL_CERTIFICATE: returnCerts(),// SSL Cert
    DB_NAME: 'documentdb',// Database name
    COLLECTION_NAME: 'events' // Tablename;
}

function returnCerts() {
    // Trick to avoid filesystem read of https://s3.amazonaws.com/rds-downloads/rds-combined-ca-bundle.pem
    return `-----BEGIN CERTIFICATE-----bla blah blah`
}
  • index.js
const {CONNECTION_STRING,SSL_CERTIFICATE,DB_NAME,COLLECTION_NAME} = require('./config');
const MongoClient = require('mongodb').MongoClient;

let client = null;

exports.handler = (event,context,callback) => {
    
    client = MongoClient.connect(CONNECTION_STRING,    { 
      sslValidate: true,      sslCA:SSL_CERTIFICATE,      useNewUrlParser: true
    },    function(err,client) {
        console.log('connection callback invoked')
        
        if(err){
            console.log(err)
        }    })              
    //callback();
    return {
        statusCode: 200,        body: JSON.stringify({"message":"hey"})
    };
};
  • 其他:Nodejs 12.x,mongodb 3.6.2
  • 错误
START RequestId: 5e135853-063b-4d5a-8a21-9a29d15c8750 Version: $LATEST
2020-11-01T02:21:43.912Z    5e135853-063b-4d5a-8a21-9a29d15c8750    ERROR   (node:9) DeprecationWarning: current Server discovery and Monitoring engine is deprecated,and will be removed in a future version. To use the new Server discover and Monitoring engine,pass option { useUnifiedTopology: true } to the MongoClient constructor.
2020-11-01T02:21:54.053Z    5e135853-063b-4d5a-8a21-9a29d15c8750    INFO    connection callback invoked
2020-11-01T02:21:54.091Z    5e135853-063b-4d5a-8a21-9a29d15c8750    INFO    MongoNetworkError: Failed to connect to server [docdb-2020-10-31-23-57-52.cluster-cgzg3t2i3zpn.us-east-1.docdb.amazonaws.com:27017] on first connect [MongoNetworkTimeoutError: connection 0 to docdb-2020-10-31-23-57-52.cluster-cgzg3t2i3zpn.us-east-1.docdb.amazonaws.com:27017 timed out
    at Socket.<anonymous> (/var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/connection.js:421:7)
    at Object.onceWrapper (events.js:421:28)
    at Socket.emit (events.js:315:20)
    at Socket._onTimeout (net.js:482:8)
    at listOnTimeout (internal/timers.js:549:17)
    at processtimers (internal/timers.js:492:7) {
  [Symbol(beforeHandshake)]: true
}]
    at Pool.<anonymous> (/var/task/LambdaDBTest/node_modules/mongodb/lib/core/topologies/server.js:438:11)
    at Pool.emit (events.js:315:20)
    at /var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/pool.js:562:14
    at /var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/pool.js:995:11
    at callback (/var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/connect.js:75:5)
    at /var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/connect.js:101:9
    at _callback (/var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/connection.js:329:7)
    at Connection.errorHandler (/var/task/LambdaDBTest/node_modules/mongodb/lib/core/connection/connection.js:344:7)
    at Object.onceWrapper (events.js:422:26)
    at Connection.emit (events.js:315:20)

解决方法

我最近在连接到启用了 TLS 的 DocumentDB 集群时遇到了类似的超时问题,我没有告诉 MongoClient 使用 SSL...我设置了 sslValidate 和 sslCA 选项,但没有设置 SSL 选项。

您必须将 ssl: true 添加到 MongClient.connect 选项或将 ssl=true 添加到连接 URL 查询字符串。看起来(从代码片段中)您也没有使用?