Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

How We Got Here

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

No content

Slide 9

Slide 9 text

Cloud Computing Evolution

Slide 10

Slide 10 text

Software Architecture Evolution

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

No content

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

No content

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

No content

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

Why Serverless

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

No content

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

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!

Slide 24

Slide 24 text

Drawbacks of Serverless ● Vendor lock-in ● Limited degree of customization & control ○ Memory ○ Timeouts ○ Disk space ● Decentralization ○ Debugging / Monitoring challenges ○ Integration testing can be hard

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

Serverless Backend

Slide 27

Slide 27 text

Serverless Asset Processing

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

Events Functions Resources

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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.

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

Hands-On

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

Kids these days

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

What we’re building it with ● AWS and the Serverless framework ○ Lambda: Compute service ○ S3: File Storage ○ DynamoDB: NoSQL Database ○ Rekognition: Image Recognition API

Slide 45

Slide 45 text

No content

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Events Functions Resources

Slide 48

Slide 48 text

HTTP Event AWS S3 downloadImage Function #0: Download Image

Slide 49

Slide 49 text

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

Slide 50

Slide 50 text

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

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

AWS S3 DynamoDB processImage Function #2: Process Image

Slide 53

Slide 53 text

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

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

Event-Driven Pipelines downloadImage analyzeImage processImage viewImage

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

Serverless Architectures

Slide 58

Slide 58 text

Serverless Asset Processing

Slide 59

Slide 59 text

Serverless Backend

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

Migrating Legacy: Strangler Pattern

Slide 62

Slide 62 text

Serverless GraphQL

Slide 63

Slide 63 text

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

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

Further Learning

Slide 66

Slide 66 text

leanpub.com/going-serverless

Slide 67

Slide 67 text

meetup.com/Serverless-Singapore

Slide 68

Slide 68 text

Thanks Yos Riady yos.io

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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,

Slide 71

Slide 71 text

No content

Slide 72

Slide 72 text

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

Slide 73

Slide 73 text

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

Slide 74

Slide 74 text

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

Slide 75

Slide 75 text

Appendix F: AWS Lambda Limits

Slide 76

Slide 76 text

Appendix H: AWS Step Functions