![](/img/trans.png)
[英]connection error while connecting to AWS DocumentDB through lambda
[英]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)
}
}
}
我哪里错了???
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.