Upgrade to Pro — share decks privately, control downloads, hide ads and more …

A Gentle Introduction to Building Serverless Apps

A Gentle Introduction to Building Serverless Apps

Do you want to crank out features the business wants, or spend a lot of time writing code and capacity planning for authentication, authorization, and complicated data access? Of course we all know the answer.

In this session will begin a beginner friendly introduction to Serverless computing. We will then do a quick overview of the MongoDB ecosystem in the 3 major cloud providers for rapid application building. Then we will walk through a guided tutorial of how to use the MongoDB Stitch serverless platform to build your elastically scalable microservices based app within minutes, complete with end-user authentication and access rules, on a fully managed MongoDB cluster in your favorite cloud platform.

Joe Karlsson

May 01, 2020
Tweet

More Decks by Joe Karlsson

Other Decks in Programming

Transcript

  1. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], } twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, tiktok: “joekarlsson”, website: “joekarlsson.com”, opinions: “my own”, links: “bit.ly/IntroToServerless”
  2. Why is Serverless so cool? No managing of infrastructure, whatsoever

    No provisioning No patching No capacity planning No scaling
  3. 77% 
 increase in delivery speed Serverless Survey: +77% Delivery

    Speed, 4 Dev Workdays/Mo Saved & -26% AWS Monthly Bill by Annika Helendi
  4. When should you go Serverless? Occasional Server needs on a

    static site Variable traffic levels Additional compute without extending current system Any web app that you want to be cheaper!
  5. @JoeKarlsson1 App Architecture Stitch MongoDB Atlas Journal.entries Trigger onSharedItems Function

    notifyUsersOfShare Services Amazon SES React Journal App Amazon SES
  6. import React, { Component } from "react"; import ReactDOM from

    “react-dom"; class StitchApp extends Component { static propTypes = { }; } import { Stitch, UserPasswordCredential, RemoteMongoClient } from "mongodb-stitch-browser-sdk"; ReactDOM.render( <StitchApp appId={“YOURE_STICH_ID”} />, document.getElementById("root") ); appId: PropTypes.string.isRequired
  7. import React, { Component } from "react"; import ReactDOM from

    "react-dom"; import PropTypes from "prop-types"; import { Stitch, UserPasswordCredential, RemoteMongoClient } from "mongodb-stitch-browser-sdk"; class StitchApp extends Component { static propTypes = { appId: PropTypes.string.isRequired }; } ReactDOM.render( <StitchApp appId={“YOURE_STICH_ID”} />, document.getElementById("root") ); import Page from "./components/Page"; import Journal from "./components/Journal"; constructor(props) { super(props); this.appId = props.appId; } this.client = Stitch.initializeDefaultAppClient(this.appId); this.mongodb = this.client.getServiceClient( RemoteMongoClient.factory, "mongodb-atlas" ); render() { return ( <Page> <Journal mongodb={this.mongodb} /> </Page> ); } index.js
  8. import React, { Component } from "react"; import Card from

    “../Card”; class Journal extends Component { constructor(props) { super(props); Journal.js const { mongodb } = this.props; this.entries = mongodb.db(“Journal”).collection(“entries”); this.state = { entries: [] }; } async componentDidMount() { // Fetch existing journal entries const entries = await this.entries.find({}).asArray(); // Add entries to Component State this.setState({ entries }); } render() { return ( <JournalContainer> { this.state.entries.map((entry) => { return <Card data={entry} /> }); } </JournalContainer> ); } } export default Journal;
  9. addEntry = async (title = "Untitled", body) => { const

    { currentUser } = this.props; const newEntry = { title, body, owner_id: currentUser.id, author: currentUser.profile.data.email, date: new Date(), sharedWith: [] }; // Add newEntry to MongoDB here const result = await this.entries.insertOne(newEntry); newEntry._id = result.insertedId; // Add newEntry to Component State this.setState(({ entries }) => ({ entries: [...entries, newEntry] })); }; removeEntry = async entryId => { // Delete the entry from MongoDB await this.entries.deleteOne({ _id: entryId }); // Remove Entry from Component State this.setState(({ entries }) => ({ entries: entries.filter(entry => entry._id !== entryId) })); }; updateEntry = async (entryId, newBody) => { // Update the Entry body in MongoDB await this.entries.updateOne({ _id: entryId }, { $set: { body: newBody } }); // Update the Entry body and disable editing in Component State this.setState(({ entries }) => ({ entries: entries.map( entry => entry._id === entryId ? { ...entry, body: newBody, isEditable: false } : entry ) })); };
  10. @JoeKarlsson1 ▪ Fire in Response to Data Changes ▪ Built

    on MongoDB Change Streams ▪ Pass Change Events to Functions ▪ Use Multiple Triggers per Collection Triggers
  11. Stitch Logs @JoeKarlsson1 Useful for auditing any events in your

    application Can also be invaluable in root cause analysis for any other issues
  12. FAQ ▪ Can I use this for free? ▪ Yes!

    Stitch provides a free tier: ▪ Do I have to use the GUI? ▪ No, There is a CLI available ▪ Do I have to use JS to write serverless functions? ▪ Yes
  13. The future of Serverless Abstraction is on the rise More

    serverless services will have a Serverless option MongoDB acquired Realm Any web app that you want to be cheaper!
  14. Why is serverless so cool? No managing of infrastructure, whatsoever

    No provisioning No patching No capacity planning No scaling Intro to Serverless
  15. Server’s need to “warmed up” State must be external DevOps

    is still a thing Serverless, what’s the catch? Intro to Serverless Why is serverless so cool?
  16. Occasional Server needs on a static site Variable traffic levels

    Additional compute without extending current system Any web app that you want to be cheaper! When should you go Serverless? Serverless, what’s the catch? Intro to Serverless Why is serverless so cool?
  17. MongoDB Stitch Avoid Vendor lock-in Works with a broad set

    of services Easy When should you go Serverless? Serverless, what’s the catch? Intro to Serverless Why is serverless so cool?
  18. Intro to Serverless Guided Serverless Demo Connecting to a Serverless

    Provider Querying MongoDB from the frontend
  19. Intro to Serverless Guided Serverless Demo Abstraction is on the

    rise More serverless services will have a Serverless option MongoDB acquired Realm Any web app that you want to be cheaper! The Future of Serverless
  20. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], } twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, tiktok: “joekarlsson”, website: “joekarlsson.com”, opinions: “my own”, links: “bit.ly/IntroToServerless”
  21. { name: “Joe Karlsson”, company: “MongoDB”, title: [ “Developer Advocate”,

    “Software Engineer” ], } twitter: “@JoeKarlsson1”, twitch: “joe_karlsson”, tiktok: “joekarlsson”, website: “joekarlsson.com”, opinions: “my own”, links: “bit.ly/IntroToServerless”