The Serverless PHP Application Rob Allen LaravelConf Taiwan 2020 - - PowerPoint PPT Presentation

the serverless php application
SMART_READER_LITE
LIVE PREVIEW

The Serverless PHP Application Rob Allen LaravelConf Taiwan 2020 - - PowerPoint PPT Presentation

The Serverless PHP Application Rob Allen LaravelConf Taiwan 2020 Serverless? Rob Allen ~ @akrabat Platform options Rob Allen ~ @akrabat Platform options Rob Allen ~ @akrabat Platform options Rob Allen ~ @akrabat Platform options Rob


slide-1
SLIDE 1

The Serverless PHP Application

Rob Allen

LaravelConf Taiwan 2020

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

It's about value

Rob Allen ~ @akrabat

slide-39
SLIDE 39

Serverless platforms

Rob Allen ~ @akrabat

slide-40
SLIDE 40

Serverless languages

Rob Allen ~ @akrabat

slide-41
SLIDE 41

Serverless platforms with PHP support

Rob Allen ~ @akrabat

slide-42
SLIDE 42

Hello World

AWS Lambda (Bref):

<?php require __DIR__ . '/vendor/autoload.php'; return function ($event) { $name = $event['name'] ?? 'world'; return 'Hello ' . $name; };

Rob Allen ~ @akrabat

slide-43
SLIDE 43

Hello World

Apache OpenWhisk:

<?php function main(array $args) : array { $name = $args['name'] ?? 'world'; return ["greeting" => 'Hello ' . $name]; }

Rob Allen ~ @akrabat

slide-44
SLIDE 44

Hello World

OpenFAAS

<?php class Handler { public function handle(string $data): void { $decoded = json_decode($data, true); $name = $decoded['name'] ?? 'world'; return 'Hello ' . $name; } }

Rob Allen ~ @akrabat

slide-45
SLIDE 45

Hello World

Google Cloud Functions (alpha)

<?php use Psr\Http\Message\ServerRequestInterface; function helloHttp(Request $request) { $name = $request->getQueryParams('name') ?? 'world'; return 'Hello ' . $name; }

Rob Allen ~ @akrabat

slide-46
SLIDE 46

Rob Allen ~ @akrabat

slide-47
SLIDE 47

The anatomy of an action

function main(array $args) : array { // Marshall inputs from event parameters $name = $args['name'] ?? 'world'; // Do the work $message = 'Hello ' . $name // Return result return ["body" => $message]; }

Rob Allen ~ @akrabat

slide-48
SLIDE 48

Hello World

function main(array $args) : array { // Marshall inputs from event parameters $name = $args['name'] ?? 'world'; // Do the work $message = 'Hello ' . $name // Return result return ["body" => $message]; }

Rob Allen ~ @akrabat

slide-49
SLIDE 49

Hello World

function main(array $args) : array { // Marshall inputs from event parameters $name = $args['name'] ?? 'world'; // Do the work $message = 'Hello ' . $name // Return result return ["body" => $message]; }

Rob Allen ~ @akrabat

slide-50
SLIDE 50

Hello World

function main(array $args) : array { // Marshall inputs from event parameters $name = $args['name'] ?? 'world'; // Do the work $message = 'Hello ' . $name // Return result return ["body" => $message]; }

Rob Allen ~ @akrabat

slide-51
SLIDE 51

Hello World

function main(array $args) : array { // Marshall inputs from event parameters $name = $args['name'] ?? 'world'; // Do the work $message = 'Hello ' . $name // Return result return ["body" => $message]; }

Rob Allen ~ @akrabat

slide-52
SLIDE 52

Deploy to OpenWhisk

$ zip -q hello.zip hello.php

Rob Allen ~ @akrabat

slide-53
SLIDE 53

Deploy to OpenWhisk

$ zip -q hello.zip hello.php $ wsk action update --kind php:7.3 hello hello.zip

  • k: updated action hello

Rob Allen ~ @akrabat

slide-54
SLIDE 54

Run it

$ wsk action invoke hello --result --param name Rob

Rob Allen ~ @akrabat

slide-55
SLIDE 55

Run it

$ wsk action invoke hello --result --param name Rob { "body": "Hello Rob!" }

Rob Allen ~ @akrabat

slide-56
SLIDE 56

Under the hood

Rob Allen ~ @akrabat

slide-57
SLIDE 57

OpenWhisk's architecture

Rob Allen ~ @akrabat

slide-58
SLIDE 58

Create an action

Rob Allen ~ @akrabat

slide-59
SLIDE 59

Invoke an action

Rob Allen ~ @akrabat

slide-60
SLIDE 60

Action container lifecycle

  • Hosts the user-written code
  • Controlled via two end points: /init & /run

Rob Allen ~ @akrabat

slide-61
SLIDE 61

Action container lifecycle

  • Hosts the user-written code
  • Controlled via two end points: /init & /run

Rob Allen ~ @akrabat

slide-62
SLIDE 62

Architecture

Rob Allen ~ @akrabat

slide-63
SLIDE 63

Monolith architecture

Rob Allen ~ @akrabat

slide-64
SLIDE 64

Serverless architecture

Rob Allen ~ @akrabat

slide-65
SLIDE 65

Serverless architecture pattern

Rob Allen ~ @akrabat

slide-66
SLIDE 66

Functions are key

Rob Allen ~ @akrabat

slide-67
SLIDE 67

Functions are the Unit of Deployment

Rob Allen ~ @akrabat

slide-68
SLIDE 68

Functions are the Unit of Scale

Rob Allen ~ @akrabat

slide-69
SLIDE 69

Functions are Stateless

Rob Allen ~ @akrabat

slide-70
SLIDE 70

Functions have Structure

Rob Allen ~ @akrabat

slide-71
SLIDE 71

Structure

If it's non-trivial, software engineering principles apply!

  • Use multiple methods

Rob Allen ~ @akrabat

slide-72
SLIDE 72

Structure

If it's non-trivial, software engineering principles apply!

  • Use multiple methods
  • Use multiple files

Rob Allen ~ @akrabat

slide-73
SLIDE 73

Structure

If it's non-trivial, software engineering principles apply!

  • Use multiple methods
  • Use multiple files
  • Integrate reusable dependencies

Rob Allen ~ @akrabat

slide-74
SLIDE 74

Serverless state machines

Rob Allen ~ @akrabat

slide-75
SLIDE 75

Serverless state machines

Rob Allen ~ @akrabat

slide-76
SLIDE 76

Rob Allen ~ @akrabat

slide-77
SLIDE 77

Rob Allen ~ @akrabat

slide-78
SLIDE 78

Case study Project 365 photo website

Rob Allen ~ @akrabat

slide-79
SLIDE 79

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-80
SLIDE 80

Lambda/PHP function

Rob Allen ~ @akrabat

slide-81
SLIDE 81

Infrastructure as code

functions: update: handler: index.php events:

  • schedule:

name: project365-build rate: cron(0 */2 * * ? *)

Rob Allen ~ @akrabat

slide-82
SLIDE 82

Infrastructure as code

functions: update: handler: index.php events:

  • schedule:

name: project365-build rate: cron(0 */2 * * ? *)

Rob Allen ~ @akrabat

slide-83
SLIDE 83

Infrastructure as code

functions: update: handler: index.php events:

  • schedule:

name: project365-build rate: cron(0 */2 * * ? *)

Rob Allen ~ @akrabat

slide-84
SLIDE 84

Process

  • 1. Gather credentials from environment
  • 2. Download photos from Flickr API
  • 3. Create HTML page
  • 4. Upload to S3
  • 5. Invalidate CloudFront cache

Rob Allen ~ @akrabat

slide-85
SLIDE 85

main()

function main(array $eventData) : array { $year = $eventData['year'] ?? date('Y'); $pageCreator = new PhotoPageCreator(); $html = $pageCreator->update($year); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($year, $html, $s3Bucket); $uploader->invalidateCache(['/'.$year]); }

Rob Allen ~ @akrabat

slide-86
SLIDE 86

main()

function main(array $eventData) : array { $year = $eventData['year'] ?? date('Y'); $pageCreator = new PhotoPageCreator(); $html = $pageCreator->update($year); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($year, $html, $s3Bucket); $uploader->invalidateCache(['/'.$year]); }

Rob Allen ~ @akrabat

slide-87
SLIDE 87

main()

function main(array $eventData) : array { $year = $eventData['year'] ?? date('Y'); $pageCreator = new PhotoPageCreator(); $html = $pageCreator->update($year); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($year, $html, $s3Bucket); $uploader->invalidateCache(['/'.$year]); }

Rob Allen ~ @akrabat

slide-88
SLIDE 88

main()

function main(array $eventData) : array { $year = $eventData['year'] ?? date('Y'); $pageCreator = new PhotoPageCreator(); $html = $pageCreator->update($year); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($year, $html, $s3Bucket); $uploader->invalidateCache(['/'.$year]); }

Rob Allen ~ @akrabat

slide-89
SLIDE 89

main()

function main(array $eventData) : array { $year = $eventData['year'] ?? date('Y'); $pageCreator = new PhotoPageCreator(); $html = $pageCreator->update($year); $uploader = new Uploader($cloudFrontId); $uploader->uploadOne($year, $html, $s3Bucket); $uploader->invalidateCache(['/'.$year]); }

Rob Allen ~ @akrabat

slide-90
SLIDE 90

The finished website

Rob Allen ~ @akrabat

slide-91
SLIDE 91

Thank you!

Rob Allen ~ @akrabat

slide-92
SLIDE 92

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
  • Stars: https://www.flickr.com/photos/gsfc/19125041621

Rob Allen ~ @akrabat