首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >AWS与AWS DocumentDB通信-超时错误

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

Stack Overflow用户
提问于 2021-12-05 11:39:34
回答 1查看 848关注 0票数 0

我已经在互联网上跟踪了很多话题,但不知怎么的,我还是很难让我的Lambda与我的AWS DocumentDB进行沟通。

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

下面是截图

我得到以下错误

代码语言:javascript
运行
复制
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中部署的代码

代码语言:javascript
运行
复制
'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)
    }
  }
}

我哪里会出错?

EN

回答 1

Stack Overflow用户

发布于 2021-12-05 11:58:46

对于Lambda文档DB,您使用的都是默认的安全组。这个安全组应该允许端口27017上的出站连接,也应该允许来自端口27017的入站连接,因为它同时连接到Lambda和DocumentDB。在这两种情况下,您都可以引用安全组本身:

或者你可以访问VPC的CIDR。

这可能有效,但我个人建议为Lambda和DocumentDB创建不同的安全组,并将彼此作为源流量引用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70233898

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档