the serverless php application
play

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


  1. The Serverless PHP Application Rob Allen LaravelConf Taiwan 2020

  2. Serverless? Rob Allen ~ @akrabat

  3. Platform options Rob Allen ~ @akrabat

  4. Platform options Rob Allen ~ @akrabat

  5. Platform options Rob Allen ~ @akrabat

  6. Platform options Rob Allen ~ @akrabat

  7. Platform options Rob Allen ~ @akrabat

  8. Platform options Rob Allen ~ @akrabat

  9. Platform options Rob Allen ~ @akrabat

  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

  11. FaaS Your code Rob Allen ~ @akrabat

  12. FaaS Deployed to the cloud Rob Allen ~ @akrabat

  13. FaaS Runs when needed Rob Allen ~ @akrabat

  14. FaaS Scaled automatically Rob Allen ~ @akrabat

  15. FaaS Pay only for execution Rob Allen ~ @akrabat

  16. Where are the servers? Rob Allen ~ @akrabat

  17. Rob Allen ~ @akrabat

  18. Rob Allen ~ @akrabat

  19. Use-cases Rob Allen ~ @akrabat

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

  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

  22. Benefits Rob Allen ~ @akrabat

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

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

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

  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

  27. Challenges Rob Allen ~ @akrabat

  28. Challenges • Start up latency Rob Allen ~ @akrabat

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

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

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

  32. When should you use serverless? Rob Allen ~ @akrabat

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

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

  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

  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

  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

  38. It's about value Rob Allen ~ @akrabat

  39. Serverless platforms Rob Allen ~ @akrabat

  40. Serverless languages Rob Allen ~ @akrabat

  41. Serverless platforms with PHP support Rob Allen ~ @akrabat

  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

  43. Hello World Apache OpenWhisk: <?php function main(array $args) : array { $name = $args['name'] ?? 'world'; return ["greeting" => 'Hello ' . $name]; } Rob Allen ~ @akrabat

  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

  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

  46. Rob Allen ~ @akrabat

  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

  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

  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

  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

  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

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

  53. Deploy to OpenWhisk $ zip -q hello.zip hello.php $ wsk action update --kind php:7.3 hello hello.zip ok: updated action hello Rob Allen ~ @akrabat

  54. Run it $ wsk action invoke hello --result --param name Rob Rob Allen ~ @akrabat

  55. Run it $ wsk action invoke hello --result --param name Rob { "body": "Hello Rob!" } Rob Allen ~ @akrabat

  56. Under the hood Rob Allen ~ @akrabat

  57. OpenWhisk's architecture Rob Allen ~ @akrabat

  58. Create an action Rob Allen ~ @akrabat

  59. Invoke an action Rob Allen ~ @akrabat

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

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

  62. Architecture Rob Allen ~ @akrabat

  63. Monolith architecture Rob Allen ~ @akrabat

  64. Serverless architecture Rob Allen ~ @akrabat

  65. Serverless architecture pattern Rob Allen ~ @akrabat

  66. Functions are key Rob Allen ~ @akrabat

  67. Functions are the Unit of Deployment Rob Allen ~ @akrabat

  68. Functions are the Unit of Scale Rob Allen ~ @akrabat

  69. Functions are Stateless Rob Allen ~ @akrabat

  70. Functions have Structure Rob Allen ~ @akrabat

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

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

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

  74. Serverless state machines Rob Allen ~ @akrabat

  75. Serverless state machines Rob Allen ~ @akrabat

  76. Rob Allen ~ @akrabat

  77. Rob Allen ~ @akrabat

  78. Case study Project 365 photo website Rob Allen ~ @akrabat

  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

  80. Lambda/PHP function Rob Allen ~ @akrabat

  81. Infrastructure as code functions: update: handler: index.php events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

  82. Infrastructure as code functions: update: handler: index.php events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

  83. Infrastructure as code functions: update: handler: index.php events: - schedule: name: project365-build rate: cron(0 */2 * * ? *) Rob Allen ~ @akrabat

  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

Recommend


More recommend