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

Building with Firebase

Building with Firebase

This talk looks at converting an existing GCP serverless application into one build using Firebase. Firebase helps to simplify deployment, particularly around simple web hosting. The talk also looks at how easy it is to use GCP services integrated with Firebase such as authentication and Cloud Firestore.


Mike Fowler

October 07, 2019


  1. Building with Firebase Mike Fowler, SRE - Google Professional Cloud

    Architect, Data Engineer & Developer PLACE CUSTOMER LOGO HERE
  2. London PostgreSQL Meetup January 24th 2019 About Me

  3. None
  4. Scene: Being On Call https://www.silicon.co.uk/wp-content/uploads/2017/02/Pager.jpg

  5. Scene: Our Engineer Rests Peacefully Source: https://peakcare.wordpress.com/2011/10/05/heads-in-the-sand/ https://i.pinimg.com/originals/cb/32/5f/cb325f9c268bf2135125f512d95

  6. Scene: Red Alert! Source: https://peakcare.wordpress.com/2011/10/05/heads-in-the-sand/ https://vignette.wikia.nocookie.net/memoryalpha/images/6/6b/RedAlert.jpg/revision/latest?cb=20100117050244&path-prefix=en

  7. Scene: Peace Source: https://peakcare.wordpress.com/2011/10/05/heads-in-the-sand/ https://www.lakelouiseinn.com/wp-content/uploads/2019/01/LakeLouise2-1.jpg

  8. Identify a Problem to Solve

  9. The Problem Many PagerDuty incidents resolve before I respond disrupting

    my sleep needlessly
  10. First Approach: Tuning https://i.ytimg.com/vi/5ZfaNpKvg5w/maxresdefault.jpg

  11. Second Approach: Machine Learning https://irishtechnews.ie/wp-content/uploads/2017/07/chi-inc-robots-doing-more-office-work-bsi-hub-20150617.jpg

  12. The Model is only a Keystone https://io9.gizmodo.com/https://miro.medium.com/max/3036/1*SQg9Buf5w-rR2T8vCIVy3g.jpeg

  13. Going Serverless https://datacenterfrontier.com/wp-content/uploads/2017/11/equinix-dc12-data-hall.jpg

  14. Introducing Mr Data

  15. Introducing Mr Data

  16. Introducing Mr Data

  17. Introducing Mr Data

  18. A Basic Serverless Architecture

  19. AI Platform • Hosted Jupyter notebooks • Distributable training with

    automatic resource provisioning • Supports CPUs, GPUs and TPUs • Run across many nodes and multiple experiments • Automated hyperparameter tuning with HyperTune • Exportable models • Model hosting for online prediction
  20. Cloud Functions • Function-as-a-Service supporting: - Node.js 6, 8 &

    10 - Python 3.7.1 - Go 1.11.6 • Triggerable from: - HTTP - Cloud Storage - Pub/Sub - Cloud Scheduler
  21. A Node.JS Cloud Function exports.pdnotify = (req, res) => {

    //Handle PagerDuty notification };
  22. Function Deployment $ gcloud functions deploy mrdata --entry-point pdnotify --runtime

    nodejs10 --trigger-http --service-account mrdata@myproject.iam.gserviceaccount.com
  23. Needs Improvement http://www.bellabeachproperties.com/wp-content/uploads/2014/04/house-falling-apart-1.jpg

  24. Needs Improvement http://www.bellabeachproperties.com/wp-content/uploads/2014/04/house-falling-apart-1.jpg

  25. Improved Serverless Architecture

  26. Motivation for Building with Firebase • Ease of deployment •

    Simple hosting • Simple, but powerful, authentication
  27. Getting Started $ firebase init ◯ Database: Deploy Firebase Realtime

    Database Rules ◉ Firestore: Deploy rules and create indexes for Firestore ◉ Functions: Configure and deploy Cloud Functions ❯◉ Hosting: Configure and deploy Firebase Hosting sites ◯ Storage: Deploy Cloud Storage security rules
  28. Getting Started === Functions Setup A functions directory will be

    created in your project with a Node.js package pre-configured. Functions can be deployed with firebase deploy. ? What language would you like to use to write Cloud Functions? JavaScript ? Do you want to use ESLint to catch probable bugs and enforce style? No ✔ Wrote functions/package.json ✔ Wrote functions/index.js ✔ Wrote functions/.gitignore ? Do you want to install dependencies with npm now? Yes
  29. Refactoring to Firebase exports.pdnotify = (req, res) => { //Handle

    PagerDuty notification }; const functions = require('firebase-functions'); exports.pdnotify = functions.https.onRequest((req, res) => { //Handle PagerDuty notification });
  30. Deployment $ firebase deploy --only functions === Deploying to 'omicron-theta-2336'...

    i deploying functions i functions: ensuring necessary APIs are enabled... ✔ functions: all necessary APIs are enabled i functions: preparing functions directory for uploading... i functions: packaged functions (1.04 KB) for uploading ✔ functions: functions folder uploaded successfully i functions: creating Node.js 8 function pdnotify(us-central1)... ✔ functions[pdnotify(us-central1)]: Successful create operation. Function URL (pdnotify): https://us-central1-omicron-theta-2336.cloudfunctions.net/pdnotify
  31. Hosting === Hosting Setup Your public directory is the folder

    (relative to your project directory) that will contain Hosting assets to be uploaded with firebase deploy. If you have a build process for your assets, use your build's output directory. ? What do you want to use as your public directory? public ? Configure as a single-page app (rewrite all urls to /index.html)? Yes ✔ Wrote public/index.html i Writing configuration info to firebase.json...
  32. Enabling Google Authentication

  33. Getting Authenticated var provider = new firebase.auth.GoogleAuthProvider(); firebase.auth().getRedirectResult().then(function(result) { if

    (result.credential) { //User is authenticated var user = result.user; var token = result.credential.accessToken; } else { //User is unauthenticated firebase.auth().signInWithRedirect(provider); } }).catch(function(error) { // Oops });
  34. Hosting Deployment $ firebase deploy --only hosting === Deploying to

    'omicron-theta-2336'... i deploying hosting i hosting[omicron-theta-2336]: beginning deploy... i hosting[omicron-theta-2336]: found 1 files in public ✔ hosting[omicron-theta-2336]: file upload complete i hosting[omicron-theta-2336]: finalizing version... ✔ hosting[omicron-theta-2336]: version finalized i hosting[omicron-theta-2336]: releasing new version... ✔ hosting[omicron-theta-2336]: release complete ✔ Deploy complete! Project Console: https://console.firebase.google.com/project/omicron-theta-2336/overview Hosting URL: https://omicron-theta-2336.firebaseapp.com
  35. Authenticate!

  36. Cloud Pub/Sub • Publish/Subscribe messaging service • At-least-once delivery •

    Seek & Replay - A subscription only sees from after it was created
  37. Topic & Subscription Creation $ gcloud pubsub topics create pd-notify

    $ gcloud pubsub subscriptions create --topic pd-notify pd-notify-model $ gcloud pubsub subscriptions create --topic pd-notify pd-notify-firestore
  38. Cloud Firestore • Serverless NoSQL document database • ACID transactions

    • Automatic scaling & indexing • Multi-region replication • Client libraries provide live and offline synchronization
  39. Adding a Document to a Collection db.collection("predictions").doc("PRX7NJU").set({ incident: "PRX7NJU", prediction:

    true, confidence: 0.7677249 }) .then(function() { //Document stored });
  40. Reporting

  41. A Firebase Architecture

  42. What Next? https://nerdist.com/article/star-trek-picard-data-where-he-is-now/

  43. Fin

  44. Mike Fowler mlfowler Questions ? gh-mlfowler @mlfowler_ www.mlfowler.com mike.fowler@claranet.uk

  45. None