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

Building a Serverless company with Node.js, Rea...

Building a Serverless company with Node.js, React and the Serverless Framework

Interactive slides available at: http://loige.link/jsday2017

Planet9energy.com is a new electricity company building a sophisticated analytics and energy trading platform for the UK market. Since the earliest draft of the platform, we took the unconventional decision to go serverless and build the product on top of AWS Lambda and the Serverless framework using Node.js. In this talk, I want to discuss why we took this radical decision, what are the pros and cons of this approach and what are the main issues we faced as a tech team in our design and development experience. We will discuss how normal things like testing and deployment need to be re-thought to work on a serverless fashion but also the benefits of (almost) infinite self-scalability and the piece of mind of not having to manage hundreds of servers. Finally, we will underline how Node.js seems to fit naturally in this scenario and how it makes developing serverless applications extremely convenient.

Luciano Mammino

May 11, 2017
Tweet

More Decks by Luciano Mammino

Other Decks in Technology

Transcript

  1. BUILDING A SERVERLESS COMPANY WITH NODE, REACT AND THE SERVERLESS

    FRAMEWORK MAY 10TH 2017, Verona Luciano Mammino loige.link/jsday2017 1
  2. Agenda What is Serverless History & definition Advantages & costs

    How it Works Example on AWS Lambda Example with Serverless Framework Serverless at Planet 9 Architecture Security Quality Monitoring / Logging Step Functions 5
  3. “ The essence of the serverless trend is the absence

    of the server concept during software development. — Auth0 loige.link/what-is-serverless 12
  4. Focus on business logic, not on infrastructure Virtually “infinite” auto-scaling

    Pay for invocation / processing time (cheap!) Advantages of serverless 14
  5. Car analogy Cars are parked 95% of the time (

    ) How much do you use the car? loige.link/car-parked-95 Own a car (Bare metal servers) Rent a car (VPS) City car-sharing (Serverless) 16
  6. What can we build? Mobile Backends APIs & Microservices Data

    Processing pipelines ⚡ Webhooks Bots and integrations ⚙ IoT Backends Single page web applications 18
  7. Serverless and JavaScript Frontend Serverless Web hosting is static, but

    you can build SPAs (React, Angular, Vue, etc.) Backend Node.js is supported by every provider ⚡ Fast startup (as opposed to Java) Use all the modules on NPM Support other languages/dialects (TypeScript, ClojureScript, ESNext...) 22
  8. exports.myLambda = function ( event, context, callback ) { //

    get input from event and context // use callback to return output or errors } Anatomy of a Node.js lambda on AWS 23
  9. 25

  10. 26

  11. 27

  12. 28

  13. 29

  14. 30

  15. 31

  16. 33

  17. Recap 1. Login to AWS Dashboard 2. Create new Lambda

    from blueprint 3. Configure API Gateway trigger 4. Configure Lambda and Security 5. Write Lambda code 6. Configure Roles & Publish ⚡Ready! No local testing ... Manual process 34
  18. // handler.js 'use strict'; exports.helloWorldHandler = (event, context, callback) =>

    { const name = (event.queryStringParameters && event.queryStringParameters.name) ? event.queryStringParameters.name : 'Verona'; const response = { statusCode: 200, headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({message: `Hello ${name}`}) }; callback(null, response); }; 38
  19. # serverless.yml service: sls-helloWorldApi provider: name: aws runtime: "nodejs6.10" functions:

    helloWorld: handler: "handler.helloWorldHandler" events: - http: path: / method: get 39
  20. Recap 1. Install serverless framework 2. Create handler & serverless

    config 3. Test locally (optional) 4. Deploy 5. Party hard! 43
  21. 46

  22. a Big Data company E.g. Meter readings per customer/year 2

    × 24 × 365 Half Hours × 25 ~ meter reading points × 24 ~ data versions = load™ of data 47
  23. Limited number of “Full stack” engineers ⚡ Write & deploy

    quality code fast Experiment different approaches over different features Adopt hot and relevant technologies Limited number of servers = LIMITED CALLS AT 2 AM! Why Serverless 48
  24. Serverless Frontend & Backend Cloufront & S3 API Gateway &

    Lambda planet9energy.io api.planet9energy.io Access-Control-Allow-Origin: https://planet9energy.io CORS HTTP HEADER Custom error page index.html Serverless Web Hosting Serverless APIs 50
  25. JWT Token Authorizer Login user: "Podge" pass: "Unicorns<3" Users DB

    Check Credentials JWT token Validate token & extract userId API request 52 API 1 API 2 API 3
  26. User Action Resource Podge trade Account17 Podge changeSettings Account17 Luciano

    delete * ... ... ... Custom ACL module used by every lambda Built on & Persistence in Postgres node-acl knex.js 54
  27. import { can } from '@planet9/acl'; export const tradingHandler =

    async (event, context, callback) => { const user = event.requestContext.userId; const account = event.pathParameters.accountId; const isAuthorized = await can(user, 'trade', account); if (!isAuthorized) { return callback(new Error('Not Authorized')); } // ... business logic } 55
  28. Security: Sensitive Data export const handler = (event, context, callback)

    => { const CONN_STRING = process.env.CONN_STRING; // ... } 56
  29. Split business logic into small testable modules Use dependency injection

    for external resources (DB, Filesystem, etc.) Mock stuff with Jest Aim for 100% coverage Nyan Cat test runners! Quality: Unit Tests 58
  30. Use child_process.exec to launch "sls invoke local" Make assertions on

    the JSON output Test environment simulated with Docker (Postgres, Cassandra, etc.) Some services are hard to test locally (SNS, SQS, S3) Quality: Functional Tests 59
  31. Git-Flow Feature branches Push code GitHub -> CI Quality: Continuous

    integration CircleCI Lint code (ESlint) Unit tests Build project Functional tests if commit on "master": create deployable artifact 60
  32. ... How do we debug then console.log... Using the module

    Enable detailed logs only when needed (e.g. export DEBUG=tradingCalculations) debug 63
  33. Step Functions Coordinate components of distributed applications Orchestrate different AWS

    Lambdas Visual flows Different execution patterns (sequential, branching, parallel) 64
  34. 65

  35. Some lessons learned... Think serverless as microservices " " ❄

    ! Be aware of There is still some infrastructure: use proper tools (Cloudformation, Terraform, ...) microfunctions Cold starts soft limits 66
  36. Serverless architectures are COOL! Infinite scalability at low cost Managed

    service Still, has some limitations Managing a project might be hard but: Technology progress and open source (e.g. Serverless.com) will make things easier Recap 67
  37. Thanks loige.link/jsday2017 @loige (special thanks to , & ) @Podgeypoos79

    @quasi_modal @augeva Feedback joind.in/talk/79fa0 68