The Serverless Application

A presentation at DevFest Cardiff in November 2019 in Cardiff, UK by Rob Allen

Slide 1

Slide 1

The Serverless Application Rob Allen November 2019

Slide 2

Slide 2

Serverless? Rob Allen ~ @akrabat

Slide 3

Slide 3

Platform options Rob Allen ~ @akrabat

Slide 4

Slide 4

Platform options Rob Allen ~ @akrabat

Slide 5

Slide 5

Platform options Rob Allen ~ @akrabat

Slide 6

Slide 6

Platform options Rob Allen ~ @akrabat

Slide 7

Slide 7

Platform options Rob Allen ~ @akrabat

Slide 8

Slide 8

Platform options Rob Allen ~ @akrabat

Slide 9

Slide 9

Platform options Rob Allen ~ @akrabat

Slide 10

Slide 10

Serverless Serverless is all about composing software systems from a collection of cloud services. With serverless, you can lean on off-the-shelf cloud services resources for your application architecture, focus on business logic and application needs. Nate Taggart, CEO Stackery Rob Allen ~ @akrabat

Slide 11

Slide 11

FaaS Your code Rob Allen ~ @akrabat

Slide 12

Slide 12

FaaS Deployed to the cloud Rob Allen ~ @akrabat

Slide 13

Slide 13

FaaS Runs when needed Rob Allen ~ @akrabat

Slide 14

Slide 14

FaaS Scaled automatically Rob Allen ~ @akrabat

Slide 15

Slide 15

FaaS Pay only for execution Rob Allen ~ @akrabat

Slide 16

Slide 16

Where are the servers? Rob Allen ~ @akrabat

Slide 17

Slide 17

Rob Allen ~ @akrabat

Slide 18

Slide 18

Rob Allen ~ @akrabat

Slide 19

Slide 19

Use-cases Rob Allen ~ @akrabat

Slide 20

Slide 20

Use-cases Synchronous Service is invoked and provides immediate response (HTTP requests: APIs, chat bots) Rob Allen ~ @akrabat

Slide 21

Slide 21

Use-cases Synchronous Service is invoked and provides immediate response (HTTP requests: APIs, chat bots) Asynchronous Push a message which drives an action later (web hooks, timed events, database changes) Rob Allen ~ @akrabat

Slide 22

Slide 22

Benefits Rob Allen ~ @akrabat

Slide 23

Slide 23

Benefits • No need to maintain infrastructure Rob Allen ~ @akrabat

Slide 24

Slide 24

Benefits • No need to maintain infrastructure • Concentrate on application code Rob Allen ~ @akrabat

Slide 25

Slide 25

Benefits • No need to maintain infrastructure • Concentrate on application code • Pay only for what you use, when you use it Rob Allen ~ @akrabat

Slide 26

Slide 26

Benefits • • • • No need to maintain infrastructure Concentrate on application code Pay only for what you use, when you use it Language agnostic Rob Allen ~ @akrabat

Slide 27

Slide 27

Challenges Rob Allen ~ @akrabat

Slide 28

Slide 28

Challenges • Start up latency Rob Allen ~ @akrabat

Slide 29

Slide 29

Challenges • Start up latency • Time limit Rob Allen ~ @akrabat

Slide 30

Slide 30

Challenges • Start up latency • Time limit • State is external Rob Allen ~ @akrabat

Slide 31

Slide 31

Challenges • • • • Start up latency Time limit State is external Different way of thinking Rob Allen ~ @akrabat

Slide 32

Slide 32

When should you use serverless? Rob Allen ~ @akrabat

Slide 33

Slide 33

When should you use serverless? • Responding to web hooks Rob Allen ~ @akrabat

Slide 34

Slide 34

When should you use serverless? • Responding to web hooks • Additional features without extending current platform Rob Allen ~ @akrabat

Slide 35

Slide 35

When should you use serverless? • Responding to web hooks • Additional features without extending current platform • PWA/Static site contact form, et al. Rob Allen ~ @akrabat

Slide 36

Slide 36

When should you use serverless? • • • • Responding to web hooks Additional features without extending current platform PWA/Static site contact form, et al. Variable traffic levels Rob Allen ~ @akrabat

Slide 37

Slide 37

When should you use serverless? • • • • • Responding to web hooks Additional features without extending current platform PWA/Static site contact form, et al. Variable traffic levels When you want your costs to scale with traffic Rob Allen ~ @akrabat

Slide 38

Slide 38

Serverless platforms Rob Allen ~ @akrabat

Slide 39

Slide 39

Hello World AWS Lambda: def my_handler(event, context): name = event.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘message’: message} Rob Allen ~ @akrabat

Slide 40

Slide 40

Hello World Apache OpenWhisk: def main(args): name = args.get(“name”, “World”) message = ‘Hello {}!’.format(name) return {‘message’: message} Rob Allen ~ @akrabat

Slide 41

Slide 41

Hello World Google Cloud Functions def hello_http(request): name = request.json.get(“name”, “World”) message = ‘Hello {}!’.format(name) return message Rob Allen ~ @akrabat

Slide 42

Slide 42

Hello World Azure Cloud Functions import azure.functions as func def main(req: func.HttpRequest, msg: func.Out[func.QueueMessage]) -> str: name = req.params.json.get(“name”, “World”) message = ‘Hello {}!’.format(name) msg.set(message) return message Rob Allen ~ @akrabat

Slide 43

Slide 43

Rob Allen ~ @akrabat

Slide 44

Slide 44

Rob Allen ~ @akrabat

Slide 45

Slide 45

The anatomy of an action def main(args): # Marshall inputs from event parameters name = args.get(“name”, “World”) # Do the work message = ‘Hello {}!’.format(name) # return result return {‘body’: message} Rob Allen ~ @akrabat

Slide 46

Slide 46

Hello World def main(args): # Marshall inputs from event parameters name = args.get(“name”, “World”) # Do the work message = ‘Hello {}!’.format(name) # return result return {‘body’: message} Rob Allen ~ @akrabat

Slide 47

Slide 47

Hello World def main(args): # Marshall inputs from event parameters name = args.get(“name”, “World”) # Do the work message = ‘Hello {}!’.format(name) # return result return {‘body’: message} Rob Allen ~ @akrabat

Slide 48

Slide 48

Hello World def main(args): # Marshall inputs from event parameters name = args.get(“name”, “World”) # Do the work message = ‘Hello {}!’.format(name) # return result return {‘body’: message} Rob Allen ~ @akrabat

Slide 49

Slide 49

Hello World def main(args): # Marshall inputs from event parameters name = args.get(“name”, “World”) # Do the work message = ‘Hello {}!’.format(name) # return result return {‘body’: message} Rob Allen ~ @akrabat

Slide 50

Slide 50

Deploy to OpenWhisk $ zip -q hello.zip hello.py Rob Allen ~ @akrabat

Slide 51

Slide 51

Deploy to OpenWhisk $ zip -q hello.zip hello.py $ wsk action update —kind python:3.7 hello hello.zip ok: updated action hello Rob Allen ~ @akrabat

Slide 52

Slide 52

Run it $ wsk action invoke hello —result —param name Rob Rob Allen ~ @akrabat

Slide 53

Slide 53

Run it $ wsk action invoke hello —result —param name Rob { “body”: “Hello Rob!” } Rob Allen ~ @akrabat

Slide 54

Slide 54

Under the hood Rob Allen ~ @akrabat

Slide 55

Slide 55

OpenWhisk’s architecture Rob Allen ~ @akrabat

Slide 56

Slide 56

Create an action Rob Allen ~ @akrabat

Slide 57

Slide 57

Invoke an action Rob Allen ~ @akrabat

Slide 58

Slide 58

Action container lifecycle • Hosts the user-written code • Controlled via two end points: /init & /run Rob Allen ~ @akrabat

Slide 59

Slide 59

Action container lifecycle • Hosts the user-written code • Controlled via two end points: /init & /run Rob Allen ~ @akrabat

Slide 60

Slide 60

Architecture Rob Allen ~ @akrabat

Slide 61

Slide 61

Monolith architecture Rob Allen ~ @akrabat

Slide 62

Slide 62

Serverless architecture Rob Allen ~ @akrabat

Slide 63

Slide 63

Serverless architecture pattern Rob Allen ~ @akrabat

Slide 64

Slide 64

Functions are key Rob Allen ~ @akrabat

Slide 65

Slide 65

Functions are the Unit of Deployment Rob Allen ~ @akrabat

Slide 66

Slide 66

Functions are the Unit of Scale Rob Allen ~ @akrabat

Slide 67

Slide 67

Functions are Stateless Rob Allen ~ @akrabat

Slide 68

Slide 68

Functions have Structure Rob Allen ~ @akrabat

Slide 69

Slide 69

Structure If it’s non-trivial, software engineering principles apply! • Use multiple methods Rob Allen ~ @akrabat

Slide 70

Slide 70

Structure If it’s non-trivial, software engineering principles apply! • Use multiple methods • Use multiple files Rob Allen ~ @akrabat

Slide 71

Slide 71

Structure If it’s non-trivial, software engineering principles apply! • Use multiple methods • Use multiple files • Integrate reusable dependencies Rob Allen ~ @akrabat

Slide 72

Slide 72

Serverless state machines Rob Allen ~ @akrabat

Slide 73

Slide 73

Serverless state machines Rob Allen ~ @akrabat

Slide 74

Slide 74

Rob Allen ~ @akrabat

Slide 75

Slide 75

Rob Allen ~ @akrabat

Slide 76

Slide 76

Case study Project 365 photo website Rob Allen ~ @akrabat

Slide 77

Slide 77

Project 365 Static website to display my photo-a-day picture for each day of the year. • Hosted on S3 • CloudFront CDN • Lambda/PHP function Rob Allen ~ @akrabat

Slide 78

Slide 78

Lambda/PHP function Rob Allen ~ @akrabat

Slide 79

Slide 79

Infrastructure as code functions: update: handler: src/actions/update.main layers: - {Ref: PhpLambdaLayer} environment: FLICKR_API_KEY: ${self:custom.FLICKR_API_KEY} FLICKR_USER_ID: ${self:custom.FLICKR_USER_ID} events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

Slide 80

Slide 80

Infrastructure as code functions: update: handler: src/actions/update.main environment: FLICKR_API_KEY: ${self:custom.FLICKR_API_KEY} FLICKR_USER_ID: ${self:custom.FLICKR_USER_ID} events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

Slide 81

Slide 81

Infrastructure as code functions: update: handler: src/actions/update.main environment: FLICKR_API_KEY: ${self:custom.FLICKR_API_KEY} FLICKR_USER_ID: ${self:custom.FLICKR_USER_ID} events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

Slide 82

Slide 82

Infrastructure as code functions: update: handler: src/actions/update.main environment: FLICKR_API_KEY: ${self:custom.FLICKR_API_KEY} FLICKR_USER_ID: ${self:custom.FLICKR_USER_ID} events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

Slide 83

Slide 83

Process 1. 2. 3. 4. 5. Gather credentials from environment Download photos from Flickr API Create HTML page Upload to S3 Invalidate CloudFront cache Rob Allen ~ @akrabat

Slide 84

Slide 84

main() function main(array $eventData) : array { $apiKey = getEnvVar(‘P365_FLICKR_API_KEY’); $userId = getEnvVar(‘P365_FLICKR_USER_ID’); $year = $eventData[‘year’] ?? date(‘Y’); $pageCreator = new PhotoPageCreator($apiKey); $html = $pageCreator->update($year, $userId); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($filename, $html, $s3Bucket); $uploader->invalidate([‘/’.$filename]); } Rob Allen ~ @akrabat

Slide 85

Slide 85

main() function main(array $eventData) : array { $apiKey = getEnvVar(‘P365_FLICKR_API_KEY’); $userId = getEnvVar(‘P365_FLICKR_USER_ID’); $year = $eventData[‘year’] ?? date(‘Y’); $pageCreator = new PhotoPageCreator($apiKey); $html = $pageCreator->update($year, $userId); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($filename, $html, $s3Bucket); $uploader->invalidate([‘/’.$filename]); } Rob Allen ~ @akrabat

Slide 86

Slide 86

main() function main(array $eventData) : array { $apiKey = getEnvVar(‘P365_FLICKR_API_KEY’); $userId = getEnvVar(‘P365_FLICKR_USER_ID’); $year = $eventData[‘year’] ?? date(‘Y’); $pageCreator = new PhotoPageCreator($apiKey); $html = $pageCreator->update($year, $userId); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($filename, $html, $s3Bucket); $uploader->invalidate([‘/’.$filename]); } Rob Allen ~ @akrabat

Slide 87

Slide 87

main() function main(array $eventData) : array { $apiKey = getEnvVar(‘P365_FLICKR_API_KEY’); $userId = getEnvVar(‘P365_FLICKR_USER_ID’); $year = $eventData[‘year’] ?? date(‘Y’); $pageCreator = new PhotoPageCreator($apiKey); $html = $pageCreator->update($year, $userId); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($filename, $html, $s3Bucket); $uploader->invalidate([‘/’.$filename]); } Rob Allen ~ @akrabat

Slide 88

Slide 88

The finished website Rob Allen ~ @akrabat

Slide 89

Slide 89

Demo Rob Allen ~ @akrabat

Slide 90

Slide 90

To sum up Rob Allen ~ @akrabat

Slide 91

Slide 91

Resources • https://akrabat.com • https://www.martinfowler.com/articles/serverless.html • https://github.com/akrabat/ow-php-todo-backend • https://github.com/akrabat/project365-photos-website • http://www.openwhisk.org • https://aws.amazon.com/lambda/ • https://bref.sh Rob Allen ~ @akrabat

Slide 92

Slide 92

Thank you! Rob Allen ~ @akrabat

Slide 93

Slide 93

Photo credits - Assembly line: https://www.flickr.com/photos/adiram/3886212918 - Under the hood: https://www.flickr.com/photos/atomichotlinks/7736849388 - Pantheon: https://www.flickr.com/photos/shawnstilwell/4335732627 - Watch mechanism: https://www.flickr.com/photos/shinythings/2168994732 - Holiday snaps: https://www.flickr.com/photos/kjgarbutt/5358075923 - Computer code: https://www.flickr.com/photos/n3wjack/3856456237 - Rocket launch: https://www.flickr.com/photos/gsfc/16495356966 - Stars: https://www.flickr.com/photos/gsfc/19125041621 Rob Allen ~ @akrabat