In my lambda function, I tried to close the mongo connection as soon as I send a callback. But it has a problem.
db.close()
things work perfectly.I think lambda re-use the connection for all the functions because I open the connection in the top of the handler:
// Connect to database
mongoose.connect(process.env.DATABASE_URL);
const handleCreateUser = async (event, context, callback) => {
// eslint-disable-next-line no-param-reassign
context.callbackWaitsForEmptyEventLoop = false;
const data = JSON.parse(event.body);
const { user, userProfile } = data;
await createUser({ callback, user, userProfile });
};
Any idea what how to fix this? Do we really have to close the connection at this point?
Either move the mongoose.connect
code inside the handler, or stop calling db.close()
. You currently have a single database connection being reused by multiple invocations of your Lambda function, but you are closing it after the first invocation completes.
Lambda will executes your handleCreateUser function in each invocation but everything outside handleCreateUser will be executed only on cold start. Lambda will cache that values for further invocations and will not execute
mongoose.connect(process.env.DATABASE_URL);
in every invocation. So i think you should move this code in handleCreateUser function.
My usage is Python but you will do this in your preferred language. Best solution for lambda, considering pay per time: Run this globally before lamba function (i do it in a config class)
if self.conn == None or self.conn.close == 1:
self.make_connection()
Up to you how you implement make_connection(). Do not use db.close() at all.
AWS calls loads your lambda function and runs you global functions once. After that at every call it ONLY RUNS the lambda which it keeps loaded for a while (from some tests 20min to 50min). The connection will be closed by the db driver on an internal timeout.
Advantages -you only open connection once in a long time saving you time for every lambda run.
Disadvantages - you hold on connection all the time lambda is in memory.
In my opinion it is worth it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With