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

From Instances to Functions: Going Serverless

From Instances to Functions: Going Serverless

Serverless is a hot topic in the software architecture world. A serverless architecture approach replaces long-running virtual machines with ephemeral compute power that comes into existence on request and disappears immediately after use. Pay for the hamburger, not the cow!

Yos Riady

May 18, 2017
Tweet

More Decks by Yos Riady

Other Decks in Programming

Transcript

  1. How We Got Here Pros & Cons of the Serverless

    Architecture Background Why Concepts Code Next Steps Intro to Serverless Summary Walkthrough of a real Serverless application
  2. Functions as a Service • FaaS / Serverless platforms are

    compute services that runs your code in response to events.
  3. Anatomy of a Lambda Function exports.myFunction = function(event, context, callback)

    { console.log("value1 = " + event.key1); console.log("value2 = " + event.key2); var result = event.key1 + event.key2; callback(null, { result: result, success: true }); }
  4. How do FaaS platforms execute your code? • When a

    lambda function is invoked, ◦ A container (execution environment) is provisioned • It takes time to set up the container ◦ “Cold start” • After execution, the container is maintained for some time (e.g. 5 minutes) • “Warm” container reuse for subsequent invocations ◦ Significantly faster
  5. How We Got Here Pros & Cons of the Serverless

    Architecture Background Why Concepts Code Next Steps Intro to Serverless Summary Walkthrough of a real Serverless application
  6. Benefits of Serverless • Scalability baked in and high availability

    ◦ No need for capacity planning • Less Ops ◦ No infrastructure to manage • Low cost ◦ Billed in 100ms increments ◦ Pay only for what you use!
  7. Drawbacks of Serverless • Vendor lock-in • Limited degree of

    customization & control ◦ Memory ◦ Timeouts ◦ Disk space • Decentralization ◦ Debugging / Monitoring challenges ◦ Integration testing can be hard
  8. Serverless Use Cases • Web / Mobile backend APIs •

    Image / Asset Processing • Webhooks • DevOps automation • Log processing • ETL / Batch processing • Big data / Machine learning • Bots • Web scraping & automation
  9. How We Got Here Pros & Cons of the Serverless

    Architecture Background Why Concepts Code Next Steps Intro to Serverless Summary Walkthrough of a real Serverless application
  10. Introduction to the Serverless Framework • CLI that helps you

    develop and deploy your AWS Lambda functions, along with the AWS infrastructure resources they require. You get: ◦ Structure & best practices ◦ Automation ◦ Plugin ecosystem • Provider and runtime agnostic ◦ AWS ◦ Azure ◦ OpenWhisk ◦ GCP
  11. Deployable units of code Single Responsibility Principle (think Unix Philosophy)

    Languages and runtimes vary Functions Executable snippets of code
  12. AWS Lambda Runtimes (May 2017) • Node.js – v4.3.2 and

    6.10 • Java – Java 8 • Python – Python 3.6 and 2.7 • .NET Core – .NET Core 1.0.1 (C#)
  13. Anatomy of an AWS Lambda Node.js Function exports.myHandler = function(event,

    context, callback) { console.log("value1 = " + event.key1); console.log("value2 = " + event.key2); callback(null, "some success message"); // or // callback("some error type"); }
  14. HTTP request SNS notification DB events S3 events Scheduled events

    On-demand invocation Events Triggers functions
  15. AWS Lambda Events (May 2017) • HTTP (API Gateway) •

    Kinesis • DynamoDB • S3 • SNS • Alexa Skill • IoT • CloudWatch Event • CloudWatch Log • Schedule
  16. NoSQL / SQL Databases File storage REST APIs Third-party APIs

    Legacy services etc. Resources Supporting cloud services
  17. serverless.yml # serverless.yml service: users functions: # Your "Functions" usersCreate:

    events: # The "Events" that trigger this function - http: post users/create usersDelete: events: - http: delete users/delete resources: # The "Resources" your "Functions" use. Raw AWS CloudFormation goes in here.
  18. Serverless Development Workflow 1. serverless create to bootstrap a Serverless

    project 2. Write your functions 3. serverless deploy to deploy the current state of the project 4. serverless invoke to test the live function 5. serverless logs to view function logs 6. Write and run unit tests for your functions locally with mocks
  19. How We Got Here Pros & Cons of the Serverless

    Architecture Background Why Concepts Code Next Steps Intro to Serverless Summary Walkthrough of a real Serverless application
  20. What we’re building • An image processing backend for a

    photo filter app ◦ Users supply an image URL ◦ Images are analyzed and processed to add relevant emojis ◦ Users can view the processed images
  21. What we’re building it with • AWS and the Serverless

    framework ◦ Lambda: Compute service ◦ S3: File Storage ◦ DynamoDB: NoSQL Database ◦ Rekognition: Image Recognition API
  22. Problem decomposition • Divide-and-conquer: Serverless applications are composed of many

    granular functions, each solving a distinct sub-problem. ◦ downloadImage ◦ analyzeImage ◦ processImage ◦ viewImage
  23. Function #0: Download Image • Sub-problem: How do we save

    the image supplied by a user? • Solution: Create a new downloadImage function ◦ Triggered by an HTTP event ◦ Saves image from URL to an S3 bucket ◦ Returns an ID back to the client
  24. Function #1: Analyze Image • Sub-problem: How do we analyze

    what’s in the image? • Solution: Create a new analyzeImage function ◦ Triggered by an S3 event ◦ Calls the AWS Rekognition DetectFaces API ◦ Writes analysis results to DynamoDB
  25. Function #2: Process Image • Sub-problem: How do we add

    emojis to the image? • Solution: Create a new processImage function ◦ Triggered by a DynamoDB event ◦ Processes the image using ImageMagick ◦ Saves the processed image to S3
  26. Result: A scalable image processing backend • Less Ops: No

    servers to provision, faster development • Scalable: AWS Lambda will execute as many functions as needed • Cheap: You only pay for real customer usage
  27. Serverless Node.js const serverless = require('serverless-http'); const koa = require('koa');

    // construct your app as normal const app = koa(); // register your middleware as normal app.use(/* ... */); // this is it! module.exports.handler = serverless(app); https://github.com/dougmoscrop/serverless-http
  28. How We Got Here Pros & Cons of the Serverless

    Architecture Background Why Concepts Code Next Steps Intro to Serverless Summary Walkthrough of a real Serverless application
  29. Next Steps • Serverless is the next natural progression of

    cloud computing • To get started, start small ◦ Extract a single feature from your monolith ◦ Rewrite it in Serverless
  30. Appendix A: AWS Lambda Pricing • If you allocated 128MB

    of memory to your function, executed it 30 million times in one month, and it ran for 200ms each time: ◦ $5.83 / month • Requests ◦ $0.20 per 1 million requests • Duration (100ms increments) ◦ $0.00001667 for every GB-second used • Charges also depend on your Lambda’s allocated memory ◦ 256MB lambdas are 2x as expensive as 128MB lambdas per 100ms • API Gateway ◦ $3.50 per million API calls received, plus the cost of data transfer out,
  31. Appendix B: FaaS Code Execution • When a lambda function

    is invoked, ◦ A container (execution environment) is provisioned • It takes time to set up and initialize the container ◦ “Cold start” ⛄ • After execution, the container is maintained for some time (e.g. 5 minutes) • “Warm” container reuse for subsequent invocations ◦ Significantly faster
  32. Appendix D: Debugging Serverless • It’s a challenge to debug

    distributed systems • Helpful techniques: ◦ Logs! Logs! Logs! ◦ Correlation ID (AWS Request ID) ◦ Unit tests • Helpful tools: ◦ AWS X-Ray
  33. Appendix E: Serverless Auth • How do you do Authentication

    and / or Authorization with Serverless? • JSON Web Tokens ◦ An authorization provider issues an identity token ◦ Services can verify the token without communicating with an authorization server, via a shared secret