$30 off During Our Annual Pro Sale. View Details »

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. From Instances to Functions
    Going Serverless
    Yos Riady
    yos.io
    goo.gl/hvrw9s

    View Slide

  2. View Slide

  3. Serverless:
    Moving away from servers and
    infrastructure concerns & allowing
    developers to primarily focus on code

    View Slide

  4. View Slide

  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

    View Slide

  6. How We Got Here

    View Slide

  7. View Slide

  8. View Slide

  9. Cloud Computing Evolution

    View Slide

  10. Software Architecture Evolution

    View Slide

  11. Functions as a Service
    ● FaaS / Serverless platforms are compute services that
    runs your code in response to events.

    View Slide

  12. View Slide

  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 });
    }

    View Slide

  14. View Slide

  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

    View Slide

  16. View Slide

  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

    View Slide

  18. Why Serverless

    View Slide

  19. View Slide

  20. View Slide

  21. View Slide

  22. View Slide

  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!

    View Slide

  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

    View Slide

  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

    View Slide

  26. Serverless Backend

    View Slide

  27. Serverless Asset Processing

    View Slide

  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

    View Slide

  29. View Slide

  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

    View Slide

  31. Events Functions Resources

    View Slide

  32. Deployable units of code
    Single Responsibility Principle
    (think Unix Philosophy)
    Languages and runtimes vary
    Functions
    Executable snippets of code

    View Slide

  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#)

    View Slide

  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");
    }

    View Slide

  35. HTTP request
    SNS notification
    DB events
    S3 events
    Scheduled events
    On-demand invocation
    Events
    Triggers functions

    View Slide

  36. AWS Lambda Events (May 2017)
    ● HTTP (API Gateway)
    ● Kinesis
    ● DynamoDB
    ● S3
    ● SNS
    ● Alexa Skill
    ● IoT
    ● CloudWatch Event
    ● CloudWatch Log
    ● Schedule

    View Slide

  37. NoSQL / SQL Databases
    File storage
    REST APIs
    Third-party APIs
    Legacy services
    etc.
    Resources
    Supporting cloud services

    View Slide

  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.

    View Slide

  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

    View Slide

  40. Hands-On

    View Slide

  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

    View Slide

  42. Kids these days

    View Slide

  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

    View Slide

  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

    View Slide

  45. View Slide

  46. Problem decomposition
    ● Divide-and-conquer: Serverless applications are composed of many granular
    functions, each solving a distinct sub-problem.
    ○ downloadImage
    ○ analyzeImage
    ○ processImage
    ○ viewImage

    View Slide

  47. Events Functions Resources

    View Slide

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

    View Slide

  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

    View Slide

  50. S3 Event AWS Rekognition
    AWS DynamoDB
    analyzeImage
    Function #1: Analyze Image

    View Slide

  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

    View Slide

  52. AWS S3
    DynamoDB processImage
    Function #2: Process Image

    View Slide

  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

    View Slide

  54. Function #3: View Image
    AWS DynamoDB
    AWS S3
    HTTP viewImage

    View Slide

  55. Event-Driven Pipelines
    downloadImage
    analyzeImage
    processImage
    viewImage

    View Slide

  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

    View Slide

  57. Serverless Architectures

    View Slide

  58. Serverless Asset Processing

    View Slide

  59. Serverless Backend

    View Slide

  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

    View Slide

  61. Migrating Legacy: Strangler Pattern

    View Slide

  62. Serverless GraphQL

    View Slide

  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

    View Slide

  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

    View Slide

  65. Further Learning

    View Slide

  66. leanpub.com/going-serverless

    View Slide

  67. meetup.com/Serverless-Singapore

    View Slide

  68. Thanks
    Yos Riady
    yos.io

    View Slide

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

    View Slide

  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,

    View Slide

  71. View Slide

  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

    View Slide

  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

    View Slide

  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

    View Slide

  75. Appendix F: AWS Lambda Limits

    View Slide

  76. Appendix H: AWS Step Functions

    View Slide