繁体   English   中英

AWS Lambda 与 AWS DocumentDB 通信 - 超时错误

[英]AWS Lambda communication with AWS DocumentDB - Timeout Error

我在互联网上关注了许多主题,但不知何故仍在努力让我的 Lambda 与我的 AWS DocumentDB 通信。

我确保 lambda 和 DocumentDB 都具有相同的安全组。

下面是截图

在此处输入图像描述

在此处输入图像描述

我收到以下错误

2021-12-05T11:21:09.895Z    874572da-feda-4725-80c2-9a9f0c65f859    INFO    MongoServerSelectionError: Server selection timed out after 30000 ms
    at Timeout._onTimeout (/var/task/node_modules/mongodb/lib/sdam/topology.js:330:38)
    at listOnTimeout (internal/timers.js:554:17)
    at processTimers (internal/timers.js:497:7) {
  reason: TopologyDescription {
    type: 'ReplicaSetNoPrimary',
    servers: Map {
      'docdb-2021-12-05-11-02-31.cluster-xxxxxxxxx .us-east-2.docdb.amazonaws.com:27017' => [ServerDescription]
    },
    stale: false,
    compatible: true,
    heartbeatFrequencyMS: 10000,
    localThresholdMS: 15,
    setName: 'rs0',
    logicalSessionTimeoutMinutes: undefined
  }
}

以下是我在 Lambda 中部署的代码

'use strict';
const MongoClient = require('mongodb').MongoClient;
let client;

async function open() {
  if (client != undefined) return client
  client = await MongoClient.connect("mongodb://documentDB:mypassword@mycluster:27017/?ssl=true&tlsCAFile=rds-combined-ca-bundle.pem&replicaSet=rs0&readPreference=secondaryPreferred&retryWrites=false", { useUnifiedTopology: true, useNewUrlParser: true, maxIdleTimeMS: 270000, minPoolSize: 2, maxPoolSize: 4 })
  console.log('mongo db connected');
}

module.exports.helloWorld = async (event, context) => {

   var mongoClient = new MongoMethods();
  

  mongoClient.get().catch(console.dir).then(result => {
    if (result == undefined) {
        return {
        "statusCode": 404,
        "headers": {},
        "body": JSON.stringify({ err: "no data found" }, null, 2),
      };
    } else {
      return {
        "statusCode": 200,
        "headers": {},
        "body": JSON.stringify(result, null, 2),
      };
    }
  })
}

class MongoMethods {
  constructor() {

  }

  async get() {
    try {
      await open()
      const db = client.db("n2DB");
      const collection = db.collection("acd_script_ids");
      const val = await collection.findOne({ "scanName": "os to cou - japan" });
      return val
    } catch (err) {
      console.log(err)
    }
  }
}

我哪里错了???

您正在为 Lambda 和文档数据库使用默认安全组。 此安全组应允许端口27017上的出站连接以及来自端口27017的入站连接,因为它连接到 Lambda 和 DocumentDB。 您可以在这两种情况下引用安全组本身:

在此处输入图像描述

或者,您可以授予对 VPC 的 CIDR 的访问权限。

这可能有效,尽管我个人建议为 Lambda 和 DocumentDB 创建不同的安全组,并相互引用作为源流量。

暂无
暂无

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

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