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!

1b0ab2500efc1b91424fb49045312929?s=128

Yos Riady

May 18, 2017
Tweet

Transcript

  1. From Instances to Functions Going Serverless Yos Riady yos.io goo.gl/hvrw9s

  2. None
  3. Serverless: Moving away from servers and infrastructure concerns & allowing

    developers to primarily focus on code
  4. None
  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. How We Got Here

  7. None
  8. None
  9. Cloud Computing Evolution

  10. Software Architecture Evolution

  11. Functions as a Service • FaaS / Serverless platforms are

    compute services that runs your code in response to events.
  12. None
  13. 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 }); }
  14. None
  15. 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
  16. None
  17. 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
  18. Why Serverless

  19. None
  20. None
  21. None
  22. None
  23. 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!
  24. Drawbacks of Serverless • Vendor lock-in • Limited degree of

    customization & control ◦ Memory ◦ Timeouts ◦ Disk space • Decentralization ◦ Debugging / Monitoring challenges ◦ Integration testing can be hard
  25. 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
  26. Serverless Backend

  27. Serverless Asset Processing

  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. None
  30. 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
  31. Events Functions Resources

  32. Deployable units of code Single Responsibility Principle (think Unix Philosophy)

    Languages and runtimes vary Functions Executable snippets of code
  33. 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#)
  34. 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"); }
  35. HTTP request SNS notification DB events S3 events Scheduled events

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

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

    Legacy services etc. Resources Supporting cloud services
  38. 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.
  39. 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
  40. Hands-On

  41. 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
  42. Kids these days

  43. 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
  44. What we’re building it with • AWS and the Serverless

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

    granular functions, each solving a distinct sub-problem. ◦ downloadImage ◦ analyzeImage ◦ processImage ◦ viewImage
  47. Events Functions Resources

  48. HTTP Event AWS S3 downloadImage Function #0: Download Image

  49. 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
  50. S3 Event AWS Rekognition AWS DynamoDB analyzeImage Function #1: Analyze

    Image
  51. 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
  52. AWS S3 DynamoDB processImage Function #2: Process Image

  53. 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
  54. Function #3: View Image AWS DynamoDB AWS S3 HTTP viewImage

  55. Event-Driven Pipelines downloadImage analyzeImage processImage viewImage

  56. 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
  57. Serverless Architectures

  58. Serverless Asset Processing

  59. Serverless Backend

  60. 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
  61. Migrating Legacy: Strangler Pattern

  62. Serverless GraphQL

  63. 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
  64. 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
  65. Further Learning

  66. leanpub.com/going-serverless

  67. meetup.com/Serverless-Singapore

  68. Thanks Yos Riady yos.io

  69. From Instances to Functions Going Serverless Yos Riady yos.io goo.gl/hvrw9s

  70. 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,
  71. None
  72. 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
  73. 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
  74. 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
  75. Appendix F: AWS Lambda Limits

  76. Appendix H: AWS Step Functions