We are attempting to build our Lambdas using NodeJS and ES Modules instead of Common JS. The main reason for this is to support top level await. This is working well. If we use Top Level Await in order to `MongoClient.connect` and keep the promise, everything seems to connect and work.
We are using provisioned concurrency with our lambdas.
Our expectation is that the connection would be good and our first actual invocation would not need to suffer the pain of creating a connection. Currently, a connection to our Mongo Atlas instance seems to take ~250ms in us-east-1 and ~800ms in us-west-2 (since the primary is in us-east-1). Our thought is if this can happen during the "init" phase, we would be able to move that time away from the invocations giving our app a performance boost.
After the first invocation, subsequent invocations do benefit from an existing connection and gain the benefit.
We had problems using mongodb 3.6.7 and mongodb 4.4.1
- Create a lambda
- use the following code in the GLOBAL portion of the lambda (not inside the handler)
```const client = new MongoClient(process.env.MONGODB_URI);
const promise = await client.connect();```
- In the handler, use the promise in order to write a query or any other operation against mongo.
You will notice a connection being formed again on the first invocation within the handler.