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

Serverless for frontend developers

Serverless for frontend developers

Aleksandar Simovic

June 23, 2018
Tweet

More Decks by Aleksandar Simovic

Other Decks in Programming

Transcript

  1. Who is? Aleksandar Simovic Senior software engineer
 ScienceExchange JS Belgrade

    co-organizer Author of “Serverless Applications with Node.js” Core team member of Claudia.js
  2. Warning! I’m about to talk about something that might change

    the path you will take in your career as a frontend developer
  3. • A great and popular tool for showing off your

    HTML / CSS* / JavaScript masterpieces of design & engineering • Free • Easy to use, easy to share • Provides multiple preprocessors (SASS, LESS, Babel…)
  4. Key goals • Being able to serve is the key

    business goal • Provide quality services • Make money to survive • Reduce cost (continually)
  5. Problems • Unexpected spikes - scaling issues • Unable to

    deliver content, getting stuck • Spending lots on provisioning servers • Keeping lots of servers idle • Small team, mostly frontend developers
  6. How? 1. Lack of resources 2. Their DevOps engineer recommended

    serverless 3. Lead frontend engineer migrated a SASS preprocessor 4. Moved even more services 5. ??? 6. Profit! $$$
  7. • Migrated all preprocessors into serverless functions • Almost everything

    now is in JavaScript • Automatic scaling • > 200,000 requests / h • Significant cost reduction
  8. S3? • Simple Storage Service, mostly used for files /

    images • No infrastructure setup / provisioning, no servers to handle • Pay-per-use per GB stored / transferred • Everyone knows & uses S3
  9. What is serverless anyway… • No need to manage or

    configure infrastructure / servers • Pay only for used time • Automatically scaled • Auto-failover and automated high scalability
  10. Serverless Functions • Simple functions - compute • No infrastructure

    setup / provisioning, no servers to handle • Pay-per-request (spent time ~100ms) • Writable in Node.js, Python, Go, C#, Java …
 • If you know how to write a single JavaScript function,
 … you’ll know how to write a Serverless one
  11. Example (ES6) exports.handler = (event) => { const body =

    event.body; // Do some async calculation if (someError) {
 return Promise.reject(someError);
 }
 return processData(body); };
  12. Example ES6 + DynamoDB const AWS = require(‘aws-sdk’); const dynamoDB

    = new AWS.DynamoDB.DocumentClient();
 
 
 exports.handler = (event) => { const body = event.body; return dynamoDB.put({ TableName: ‘chernivitsijs—attendees’, Item: body
 
 }).promise() .then(response => { statusCode: 200, body: ‘success’ }); }; Thats it! This is the database part!
  13. Serverless functions 
 enable you to
 
 effortlessly write backend

    functionality
 
 as a frontend developer!
  14. SPAs on S3 • Out-of-the-box HTTPS • One command -

    CDN integration • Easy domain setup with Route53 - 2 clicks / 1 command • Automatic scaling • Pay-per-use Not only for static files and images!
  15. SPAs on S3 - How? npm install scottyjs -g
 


    
 scotty --spa 1. Create your React / Vue / Angular app
 2. Choose either AWS CLI / console or use Scotty.js
  16. SPAs on S3 - Cost? - Storage :
 First 50

    TB / Month - $0.023 per GB
 - Requests:
 GET, SELECT - $0.001 per 1,000 requests
 
 
 ~100.000 user visits => costs 1$?
  17. exports.handler = (event) => { const yourHTML = `<html> <body>

    Hello ChernivtsiJS !</body> </html>`; return {
 
 statusCode: 200, headers: { ‘Content-Type’: ‘text/html’ }, body: yourHTML }; }; Server(less) side rendering
  18. Summary - Frameworks mean nothing, if you’re unable to scale.

    - Serverless enables frontend developers to provide service-level value too. - Serverless saves time, reduces complexity and also the cost. - You already are serverless. S3.