Today we are releasing a – proof of conecpt – open source AWS Lambda function that serves as a MongoDB connection “Proxy”.

With Proxy, your Lambda functions don’t need to establish its own database connection Instead, they will invoke “Proxy”, sending the query parameters. Once invoked, the Proxy will run the query using the standard MongoDB Node.js driver, and reply back with the query results.

It uses MongoDB native driver, Lambda & Secrets Manager.

This will help you to reduce database stress, idle connections, etc. It has support for find, update, delete, aggregate, etc.

See webiny’s blog to understand more about the problem that Proxy solves.

Check the source code and how to start using it:

https://github.com/joseconstela/mongodb-lambda-proxy

Working code example for your Javascript projects:

  const dbQuery = {
    collection: 'users',
    op: 'findOne',
    query: {
      name: 'Jon'
    },
    options: {
      fields: { email: 1 },
    }
  }

  return new Promise((resolve, reject) => {
      var params = {
          FunctionName: 'myDbProxyFunctionName',
          InvocationType: 'RequestResponse',
          LogType: 'None',
          Payload: JSON.stringify(dbQuery)
      }
      lambda.invoke(params, (err, result) => {
        if (err) return reject(err)
        const response = JSON.parse(result.Payload)
        if (!response.success) return reject({
          reason: response.reason,
          error: response.error
        })
        return resolve(response.result)
      })
  })

  // Example outputs:
  //
  // Resolves: { email: 'jon@example.com' }
  // Resolves: [ { email: 'jon@example.com' } ]
  // Rejects: { reason: 'Unrecognized pipeline stage name: \'$matcsh\'', error: '......' }
  // Rejects: { reason: 'failed to connect to server...', error: '......' }