What problem are you facing?
having issue trying to connect with mongo atlas using aws lambda.
What driver and relevant dependency versions are you using?
nodejs 14.x
mongodb 5.5 ( mongo client )
mongo atlas 6.0.6
Steps to reproduce?
this is my code when try to connect:
// marcador de posición de código
const { MongoClient } = require('mongodb')
const URI = process.env.MONGO_URI
let cachedDb = null
const connect = async () => { const payload = { success: false, data: null, message: [], errors: [] } try { if (!cachedDb) { // Si no está conectado, establece la conexión const client = await MongoClient.connect(URI) cachedDb = client.db() payload.success = true payload.message.push('Mongo connection established successfully') payload.data = cachedDb } return payload } catch (error) { console.error('Error connecting with database:', error) payload.success = false payload.data = null payload.errors.push('Uncontrolled error in mongoConnection.service.connect') return payload }}
module.exports = { connect }
then when I try to run it in aws lambda, serverless.com report me this error (from aws)
// marcador de posición de código
2023-05-22T22:30:55.830Z b4f960ec-6c55-43d6-b6a0-b4ea59a63538 ERROR Error conecting with database: MongoServerSelectionError: Server selection timed out after 30000 ms at Timeout._onTimeout (/var/task/node_modules/mongodb/lib/sdam/topology.js:278:38) at listOnTimeout (internal/timers.js:557:17) at processTimers (internal/timers.js:500:7) { reason: TopologyDescription { type: 'ReplicaSetNoPrimary', servers: Map(3) { 'plata-dev-shard-00-01.w4zuh.mongodb.net:27017' => [ServerDescription], 'plata-dev-shard-00-02.w4zuh.mongodb.net:27017' => [ServerDescription], 'plata-dev-shard-00-00.w4zuh.mongodb.net:27017' => [ServerDescription] }, stale: false, compatible: true, heartbeatFrequencyMS: 10000, localThresholdMS: 15, setName: 'atlas-yu8x8e-shard-0', maxElectionId: null, maxSetVersion: null, commonWireVersion: 0, logicalSessionTimeoutMinutes: null }, code: undefined, [Symbol(errorLabels)]: Set(0) {} }
—
I already peering with my VPC, allow public connection, and did all the tips that I found in internet, but I don’t know why it still failling