Using MongoDB Atlas with AWS Lambda How to build apps in a serverless world @rlondner
{ "name" : "Raphael Londner", "title" : "Developer Advocate @MongoDB", "location" : "Palo Alto, CA", "email" : "raphael.londner@mongodb.com", "twitter" : "@rlondner”, "languages": [{"name":"french" proficiency:"native"}, {"name":"english" proficiency:"professional"}, {"name":"italian" proficiency:"elementary"}] } @rlondner Who am I?
@rlondner Poll
@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
@rlondner Poll: Which server-side programming language do you use?
@rlondner Use cases • Web applications • Data processing (real-time streaming analytics) • Scalable back ends (mobile apps, IoT devices) • Amazon Alexa • Chatbots
@rlondner AWS Lambda Deep Dive
@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
@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
@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
@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
@rlondner Back to Node.js…
@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)
@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
@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
@rlondner Demo (finally!)
@rlondner Performance optimizations
@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
@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!
@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
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.
@rlondner Thank you Questions?

Building serverless apps with MongoDB Atlas and AWS Lambda

  • 1.
    Using MongoDB Atlas withAWS Lambda How to build apps in a serverless world @rlondner
  • 2.
    { "name" : "RaphaelLondner", "title" : "Developer Advocate @MongoDB", "location" : "Palo Alto, CA", "email" : "raphael.londner@mongodb.com", "twitter" : "@rlondner”, "languages": [{"name":"french" proficiency:"native"}, {"name":"english" proficiency:"professional"}, {"name":"italian" proficiency:"elementary"}] } @rlondner Who am I?
  • 3.
  • 4.
    @rlondner Where do youhost 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
  • 5.
  • 6.
    @rlondner Use cases • Webapplications • Data processing (real-time streaming analytics) • Scalable back ends (mobile apps, IoT devices) • Amazon Alexa • Chatbots
  • 7.
  • 8.
    @rlondner Anatomy of aLambda function (Node.js) • Handler function in index.js • exports.myHandler = function(event, context, callback) => {…} • Handler value in Lambda: index.myHandler
  • 9.
    @rlondner Anatomy of aLambda 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 aLambda 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 aLambda 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
  • 12.
  • 13.
    @rlondner How to developwith 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 testand 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 • Dbconnection 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
  • 16.
  • 17.
  • 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 developmentwith 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.
  • 22.