MongoDb(AWS documentDB) connection with mongoose ODB - Nodejs

We are using AWS Lambda (Node.js) with Express and Mongoose to connect to an AWS DocumentDB. Below is a code snippet of our connection file. The connection setup is working fine, but we are encountering MongoDB connection timeout errors when we try to call the Lambda function after a while. We have attached a screenshot of the error below. Could you please take a look and let us know if you have a solution? Thank you in advance.

`const mongoose = require('mongoose');
const path = require('path');

let conn = null;
const connect = async connectionString => {
  try {
    console.log('Trying to establish the connection::');
    if (conn == null) {
      mongoose.set('strictQuery', false);
      conn = await mongoose.connect(connectionString, {
        tlsCAFile: path.join(__dirname, './rds-combined-ca-bundle.pem'),
        keepAlive: true
      console.log('New DB connection established::');
    } else {
      console.log('DB connection from cache::');
    return conn;
  } catch (err) {
    console.log('MongoDB connection failed::', err);
    throw err;

module.exports = { connect };

Hello @Zil_D

Please review:

const mongoose = require('Mongoose');
mongoose.connect("MongoDB://localhost:<PortNumberHereDoubleCheckPort>/<DatabaseName>", {useNewUrlParser: true});
const <nameOfDbschemahere> = new mongoose.schema({
  name: String,
  rating: String,
  quantity: Number,
  someothervalue: String,
  somevalue2: String,

const Fruit<Assuming as you call it FruitsDB> = mongoose.model("nameOfCollection" , <nameOfSchemeHere>);

const fruit = new Fruit<Because FruitsDB calling documents Fruit for this>({
  name: "Watermelon",
  rating: 10,
  quantity: 50,
  someothervalue: "Pirates love them",
  somevalue2: "They are big",

I would also look into is whether you’re hitting the connection max limits, if so I would refer to AWS support specifically in determining the solution for the AWS Lambda timeouts and it would be awesome to share them here.

As Lambda timeout is something that is frequently brought up here, I personally haven’t experienced it myself in my AWS lab so I’m not exactly sure what specifically may cause it.

But for Mongoose the above is pretty much what I do for the most part to connect and I use a URI instead of local.