Does anyone have sample code for node.js, mongoDB using mongoose? I need to do CRUD on mongo collection and I would like to use lambda and serverless.
Checkout the https://github.com/serverless/examples/tree/master/aws-node-rest-api-with-dynamodb example.
It’s using dynamoDB but you could replace the get/post/delete functions with your calls to your remote MongoDB table.
You will need a hosted mongoDB solution because the lambda function node environment only lives for 5minutes max.
You will need to connect to your remote MongoDB. Checkout http://mongoosejs.com/docs/connections.html
Appreciate your help on above answer. dynamaDB example is very helpful.
I am trying to see if code for connecting to DB I can put somewhere in initialize code of lambda. I know all the threads says that we should not rely that container will be reused but just trying to see if I get lot of concurrent requests lambda container might get reused. So instead of creating mongo connection for every request only during container initialization I can get connection.
Is there a way in lambda that some code block will get executed only during lambda container initialization ?
As I understand it, the code outside of the handler function is executed once. You could connect to the database there but with container reuse there’s no guarantee the connection would still be around when the handler is executed. If you have a way to test the connection is still active prior to using it then that might be viable solution.
Hey mate, I understand you, and I’ve been where u’re at. Heheheh
I lost a lot of time trying to implement it and I finally found a useful way of doing it, it’s not the optimal way of doing it but it works pretty well…
Since I’m not using Serverless as the framework, I’m using ClaudiaJS deploying a full ExpressJS application. For every request recieved I do connect to the database and disconnect from the database. (using middleware)
The problem that I was facing with mongoose was that I was unable to keep a connection, I’ve tried several things and none but this worked.
(I’ve done it before with serverless also but that was before the new version of it and it’s not useful anymore)
Github project(under development) link: https://github.com/TCEES-NTI/SAPO
thanks lucianopf for sharing your github repo.
how was your experience with ClaudiaJS as compare to serverless framework? why did you move ClaudiaJS? I am new to serverless and still trying to find out whats best option for development for lambda and AWS api gateway.
rowanu, thanks for sharing very nicely written article.
That is an awesome point, and I’ve been deeply trying to do this, since this is a thing to take care since the release of the lambda functions.
The problem is that mongo won’t let us have multiple connections, so you can’t predict in which context that you’ll be and inevitactbly you’ll invoke a new context that will try to make a new connection with the mongo.
Unfortunetly the only solution that I could think of is to release the connection after every request =(
If someone know’s a way to open multiple connections on a lambda function I’ll be really happy to hear!
In case someone finds this post here goes some useful sample code
@DavidWells I hate people who answer questions with a different solution, it’s NOT helpful!
dynamodb is nothing better than a redis key/value store, no query support, no aggregation, don’t bother answer freaking questions if you don’t know the damn answer.
Here is a mongo example for ya https://github.com/maciejmatu/smoothielicious/blob/b7a5a76eb29af076ed228c9b5d190fb56d0df1d2/src/lambda/add-page-visit.js
Have a wonderful day