1

Hey, does Anyone face a MongoDB connection error/issue on AWS lambda? and if you know the solution, please help me, guys!

my code works perfectly in my local system!

But when I try to post, using the API gateway endpoint, MongoDB disconnect! ( session time-out error )

Image description

Image description

5
  • I don't think it is a session timeout error since according to the timestamp, there was less then 30s that have passed. My thinking is more around reusing the same connection between two lambda invocations. Can you try to do a disconnect before terminating the lambda? Commented Nov 4, 2022 at 8:46
  • i don't get your point! can you please explain Commented Nov 4, 2022 at 8:57
  • Everytime you invoke a lambda, there is a whole execution environment that is created for your lambda to be able to run. This execution environment would be shared across the next lambda invocation (so that you don't have a cold start on every lambda execution). In your case, you trigger the lambda within the same execution environment which result in reusing the same runtime context between invocation. It is like your are doing two time mongoose.connect(). When you do on your laptop, the whole execution context is deleted so it works fine. Please disconnect before ending the lambda. Commented Nov 4, 2022 at 9:03
  • what you could do as well, is moving the connect outside of the handler. So that the same client is being used across the same lambda execution context. Commented Nov 4, 2022 at 9:05
  • ok, i call connectDB() inside of handler Commented Nov 4, 2022 at 9:07

1 Answer 1

1

This is because of your code that doesn't take care of lambda execution environment.

As mentioned in the comment above, between two lambda invocation, you are not releasing the connection to the DB. It is like doing two times in a row mongoose.connect()

I suggest to do something like this:

let cachedClient = null;

async function connectDB() {
  if (cachedClient) {
    return cachedClient;
  }

  // Connect to our MongoDB database hosted on MongoDB Atlas
  const client = await mongoose.connect(MONGO_DB_URL);

  cachedClient = client;
  return client;
}

This will reuse the same client across multiple lambda invocation (within the same execution environment). When a new lambda execution environment is created, cachedClient will be null and a new client will be created.

Hope it clarifies.

Sign up to request clarification or add additional context in comments.

6 Comments

it's want not brother! but another new error now: Serverless SDK Warning: Following trace spans didn't end before end of lambda invocation: node.http.request
share your updated code please. Otherwise, as I mentioned before, call mongoose.disconnect() after printing Mongo Connected.
Ok, then try something like this: let cachedClient = null; async function connectDB() { if (cachedClient) { console.log('MongoDB connected') } else { // Connect to our MongoDB database hosted on MongoDB Atlas const client = await mongoose.connect(MONGO_DB_URL); cachedClient = client; console.log('MongoDB connected') } }
ohm! really sorry brother! now my code work! my GitHub ci/cd is failed! so i can't deploy new code! i testing my old code! really thank you : ) @brnaba if you free, can you answer in dev.to it will helpfull for many developers https:// dev. to /jacksonkasi/ how-to-fix-mongodb-connection-error-in-aws-lambda-i8d
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.