通过 lambda 从 node.js 连接到 AWS DocumentDB 时出现以下错误
{"errorMessage":"ENOENT: 没有这样的文件或目录,打开 'rds-combined-ca-bundle.pem'","errorType":"Error","stackTrace":["Object.fs.openSync (fs. js:646:18)","Object.fs.readFileSync (fs.js:551:33)","Object. (/var/task/base/mongoose.base.js:8:13)","模块._compile (module.js:652:30)","Object.Module._extensions..js (module.js:663:10)","Module.load (module.js:565:32)","tryModuleLoad (module.js:505:12)","Function.Module._load (module.js:497:3)","Module.require (module.js:596:17)","require (internal/module. js:11:18)","对象。(/var/task/library/mongoLib/room.lib.js:1:84)","Module._compile (module.js:652:30)","对象.Module._extensions..js (module.js:663:10)","Module.load (module.js:565:32)","tryModuleLoad (module.js:505:12)","Function.Module._load (module.js:497:3)" ]}
这是我在 lambda 中的节点 js 文件
var ca = fs.readFileSync(path.join('./','rds-combined-ca-bundle.pem'));
var options = {
keepAlive: true,
poolSize: 30,
socketTimeoutMS: 30000,
autoReconnect: true,
reconnectTries: Number.MAX_VALUE,
reconnectInterval: 500,
useCreateIndex: true,
auth: {authdb: 'admin'},
useFindAndModify: false,
sslValidate: true,
sslCA:ca,
useNewUrlParser: true
}
var uri = 'mongodb://'+globalData.getConfigurationSettings("documentdb_username")+':'+globalData.getConfigurationSettings("documentdb_password")+'@'+globalData.getConfigurationSettings("documentdb_server")+':'+globalData.getConfigurationSettings("documentdb_port")+'/'+globalData.getConfigurationSettings("documentdb_db_name")+'?ssl=true&replicaSet=rs0&readPreference=secondaryPreferred';
mongoose.connect(uri, options)
.then(() => console.log('Connection to DB successful'))
.catch((err) => console.error(err,'Error'));