4. @rlondner
Where do you host your databases & apps?
• Physical server in private datacenter
• Virtual server in private datacenter
• VMWare vSphere, Microsoft Hyper-V, other
• Physical or virtual server in private cloud
• Rackspace, DigitalOcean, BlueMix, other
• Virtual server in public cloud
• Amazon Web Services, Microsoft Azure, Google Cloud
8. @rlondner
Anatomy of a Lambda function (Node.js)
• Handler function in index.js
• exports.myHandler = function(event, context,
callback) => {…}
• Handler value in Lambda: index.myHandler
9. @rlondner
Anatomy of a Lambda function (Java)
• Handler function in example.Hello class
• package example;
public class Hello {
public String myHandler(type inputVal, Context context) {...}
• Handler value in Lambda: example.Hello::myHandler
10. @rlondner
Anatomy of a Lambda function (Python)
• Handler function in hello_python.py file
• def my_handler(event, context):
…
return some_value
• Handler value in Lambda: hello_python.my_handler
11. @rlondner
Anatomy of a Lambda function (C#)
• Handler function in Example.Hello class
• Namespace Example;
public class Hello {
public Stream MyHandler(type inputVal, ILambdaContext
context) {...}
• Handler format & value in Lambda:
• Assembly::Namespace.ClassName::MethodName
• HelloWorldApp::Example.Hello::MyHandler
13. @rlondner
How to develop with Atlas and Lambda
• Sign up for Atlas and create an M0 cluster
• Grab your Atlas connection string
• Initialize a Node.js project
• Import MongoDB Node.js driver (or Mongoose)
• AWS SDK for JavaScript if necessary
• Write code! (using your Atlas connection string)
14. @rlondner
How to test and deploy your Lambda
• Use the lambda-local NPM package to test locally
• Zip your Node.js code with ALL modules
• Except aws-sdk if used
• Use ‘zip -r archive.zip node_modules/ index.js’ from inside
the code folder!
• Create your Lambda function and upload your package
to AWS Lambda
15. @rlondner
Best practices
• Db connection string stored as env. variable
• Use –E parameter with lambda-local
• Reference it with process.env['MONGODB_ATLAS_URI']
• Encrypt Db connection string in AWS Lambda
• Use AWS.KMS() to decrypt() the connection string
18. @rlondner
What’s the problem?
• AWS Lambda is stateless
• DB connections are initialized at every call…
• Well, almost…
• Lambda containers are alive for 5 minutes
• Ways to “cache” resources in Lambda across recent
calls
19. @rlondner
Performance best practices
• Declare the db object outside the handler method
• Set context.callbackWaitsForEmptyEventLoop = false
• Try to re-use the db object if
db.serverConfig.isConnected() returns true
• Do NOT close the db object!
20. @rlondner
References
• Serverless development with Node.js, AWS Lambda and
MongoDB Atlas
• Optimizing AWS Lambda performance with MongoDB Atlas
and Node.js
• Lambda Node.js code sample (callbacks)
• Lambda Node.js code sample (promises
21. June 20-21, 2017
Chicago, IL
Chicago, IL
MongoDB World is where the world’s fastest growing database
community comes to connect, explore, and learn.
Join us for educational, hands-on and deep-dive technical sessions, giving you the tools
you need to build and deploy your giant ideas.
● Connect with the fastest-growing database community
● Explore new features, technologies and methodologies with the experts
● Learn how to deploy mission-critical applications at scale
Register at mongodbworld.com. Use code RaphaelLondner to get 25% off tickets!
Groups of 3 or more get an additional 25% off.