Share db connection between lambdas - apollo and mongoose on lambda with serverless offline


I’m running apollo-server-lambda with a mongodb database and mongosse. Is there any proven way to share connections between lambdas with sls offline ( and aws lambdas, presumably)?

As of now when i run the playground on localhost, the playground pings the server every 100ms (or so), creating hundreds of open connections in seconds. I’m not using the playground in production, but i assume the question still remains the same if we serve a lot of requests on a Lambda in production.

I’ve read some of the other posts on this forum and other articles, including the reference on mongoDb but I can’t seem to make this work offline, which makes me a bit worried about putting it into produciton.

Related post on this forum: Reusing Database Connections (node.js) - #5 by codepressd
Mongoose docs: Mongoose v6.2.0: Using Mongoose With AWS Lambda
Mongdb best practices: do a search for “mongodb lambda best practices” to find link. (can only put 2 links as a new user)

reference code

import { ApolloServer, gql } from 'apollo-server-lambda'
import { connect } from 'mongoose';
import * as mongoose from 'mongoose';

const typeDefs = gql`
  type Query {
    hello: String

const resolvers = {
  Query: {
    hello: () => 'Hello world!',

const server = new ApolloServer(

const handler = server.createHandler({
  expressGetMiddlewareOptions: {
    cors: {
      origin: "*",
      methods: ["POST", "GET"],
      allowedHeaders: "*",
      credentials: true

let conn: any = null

exports.handler = async (event, context, callback) => {
  context.callbackWaitsForEmptyEventLoop = false
  if (conn == null) {

    conn = mongoose.connect(process.env.DATABASE_URL!, {
      serverSelectionTimeoutMS: 5000
      .then(() => {
        console.log('\x1b[34m', `💿 DB -> ${process.env.DATABASE_NAME}\n`);
        return mongoose
      .catch(error => {;
    await conn

  return handler(event, context, callback)

Resolvers here doesn’t need the DB, but connect block still runs, maxing out the connections to mongo in a matter of seconds.

   │                                                                                │
   │   POST | http://localhost:3000/dev/lambdaServer                                │
   │   POST | http://localhost:3000/2015-03-31/functions/lambdaServer/invocations   │
   │   GET  | http://localhost:3000/dev/lambdaServer                                │
   │   POST | http://localhost:3000/2015-03-31/functions/lambdaServer/invocations   │
   │                                                                                │

Server ready: http://localhost:3000 🚀

Enter "rp" to replay the last request

GET /dev/lambdaServer (λ: lambdaServer)
 💿 DB -> MyMongoDb

Promise { undefined }
(λ: lambdaServer) RequestId: ckz6sw37i0002mwop3r97274r  Duration: 2010.15 ms  Billed Duration: 2011 ms

POST /dev/lambdaServer (λ: lambdaServer)

POST /dev/lambdaServer (λ: lambdaServer)

POST /dev/lambdaServer (λ: lambdaServer)

POST /dev/lambdaServer (λ: lambdaServer)

POST /dev/lambdaServer (λ: lambdaServer)


Im pretty new with serverless, so there might be some limitations with the serverless offline plugin i don’t know of.

Thanks in advance for any insight you can provide!