node.js - 从 Lambda 连接到 DocumentDB 时超时

标签 node.js mongodb amazon-web-services aws-lambda aws-documentdb

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

设置:

  • 集群和 lambda 位于同一 VPC 中(默认)
  • TLS 已开启
  • 集群位于名为 DemoDocDB 的安全组中,该安全组具有入站 27017 的规则适用于两个安全组:cloud9 和 DefaultSG
  • Lambda 位于默认 VPC 中,并且位于 DefaultSG 安全组中

代码:

  • config.js
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 查询字符串中。 (从代码片段来看)您也没有使用它?

关于node.js - 从 Lambda 连接到 DocumentDB 时超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64628370/

相关文章:

node.js - Windows 10 中的 npm 安装错误 ( npm install -g angular-cli )

javascript - Node.js 数据表编辑器 - 如何使用组成联接的 (knex) VIEW

mongodb - 通过 mongodb 加权平均评分

node.js - 如何让 node.js 使用 mongoose 连接到 mongolab

git - 将 AWS IAM ARN 值保留在公共(public) Git 存储库中是否安全?

amazon-web-services - S3 事件 -> Lambda 与 S3->SNS->Lambda

javascript - python bcrypt 和 node.js bcrypt

javascript - 如何拆分消息并将第二部分重新发送到文本 channel ?

javascript - NodeJS 和 MongoDB - ObjectId 数组如何返回对象

amazon-web-services - 我创建了一个ECS容器,如何绑定(bind)域名?