简体   繁体   中英

MongoDB Atlas connection occasionally fails from Lambda

I have an AWS Lambda that connects to Atlas through Mongoose.

It works fine on my dev environment (using a M0 Sandbox) but in prod, where I have an M2, every few requests fails with a MongooseServerSelectionError when I do the lambda's first query. It appears this only happens on (some) reused DB connections, never from a cold start.

My Network Access IP range is set to 0.0.0.0, so I don't think whitelisting is an issue.

For what its worth, these are my connection options:

conn = mongoose.createConnection(process.env.MONGO, {
      bufferCommands: false,
      bufferMaxEntries: 0,
      useNewUrlParser: true,
      useUnifiedTopology: true,
      useCreateIndex: true,
      socketTimeoutMS:4000,
      serverSelectionTimeoutMS: 1000
    });

Curious if anyone has run into a similar problem before?

I am not sure if this is the answer you are looking for but it is recommended that you initialize your db connection outside your function context. this might be of use to you. mongodb atlas documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM