我已经在互联网上跟踪了很多话题,但不知怎么的,我还是很难让我的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)
}
}
}
我哪里会出错?
发布于 2021-12-05 11:58:46
对于Lambda文档DB,您使用的都是默认的安全组。这个安全组应该允许端口27017
上的出站连接,也应该允许来自端口27017
的入站连接,因为它同时连接到Lambda和DocumentDB。在这两种情况下,您都可以引用安全组本身:
或者你可以访问VPC的CIDR。
这可能有效,但我个人建议为Lambda和DocumentDB创建不同的安全组,并将彼此作为源流量引用。
https://stackoverflow.com/questions/70233898
复制相似问题