I am getting "MongoServerSelectionError: Server selection timed out after 30000 ms" when my lambda is connecting to my database

I have an AWS lambda in my VPC which, using VPC peering, talks to my Atlas cluster.

Some of the time I am seeing the following error: { "errorType": "Runtime.UnhandledPromiseRejection", "errorMessage": "MongoServerSelectionError: Server selection timed out after 30000 ms", "reason": { "errorType": "MongoServerSelectionError", "errorMessage": "Server selection timed out after 30000 ms", "reason": { "type": "ReplicaSetNoPrimary", "servers": {}, "stale": false, "compatible": true, "heartbeatFrequencyMS": 10000, "localThresholdMS": 15, "setName": "atlas-cnjwgm-shard-0" }, "stack": [ "MongoServerSelectionError: Server selection timed out after 30000 ms", " at Timeout._onTimeout (/var/task/index.js:2:464693)", " at listOnTimeout (internal/timers.js:554:17)", " at processTimers (internal/timers.js:497:7)" ] }, "promise": {}, "stack": [ "Runtime.UnhandledPromiseRejection: MongoServerSelectionError: Server selection timed out after 30000 ms", " at process.<anonymous> (/var/runtime/index.js:35:15)", " at process.emit (events.js:314:20)", " at processPromiseRejections (internal/process/promises.js:209:33)", " at processTicksAndRejections (internal/process/task_queues.js:98:32)" ] }

I have not been able to find any solutions to this issue. Any help or pointers would be appreciated.

Hello @Edouard_Finet ,

Regarding connections into Atlas from AWS Lambda, the page Manage connections with AWS Lambda might be of interest to you. Also the blog post Write A Serverless Function with AWS Lambda and MongoDB | MongoDB might be useful.

Could you please provide below details for us to check on the error you are seeing:

  • Atlas deployment description (M0/M10, others)
  • As you mentioned Some of the time, so is it happening intermittently?
  • Are you able to connect to Atlas from inside your VPC?
  • How frequently are you seeing this issue?
  • Was this connection working previously and this error is a recent occurance?

If this issue persists and all network settings should be allowing the connection, have you contacted Atlas support to look into this issue? They would have better visibility into the state of your deployment and thus may be able to help you further.