Serverless PHP @ PHPCon Poland 2019

Serverless PHP @ PHPCon Poland 2019

#serverless nie był do tej pory gorącym tematem w świecie PHP, ponieważ żaden z dostawców usług chmurowych nie wspiera natywnie PHP w środowisku tego typu. Pod koniec zeszłego roku AWS udostępniło dwie nowe usługi (Lambda Runtime API oraz Lambda Layers), które pozwalają na samodzielne dodanie PHP do środowiska typu #serveless. Podczas prezentacji omówię poszczególne elementy takiej architektury oraz pokażę kod PHP działąjacy na AWS Lambda.

9a328142924c93e5e148c75356ba6d42?s=128

Sebastian Grodzicki

November 16, 2019
Tweet

Transcript

  1. Serverless PHP Sebastian Grodzicki @ PHPCon Poland 2019 @sebgrodzicki

  2. 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

  3. 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019

  4. phpinfo(); Sebastian Grodzicki
 • Engineering Manager at • ex-CTO at

    SHOWROOM & GoldenLine • PHP developer for 15+ years • #serverless @sebgrodzicki
  5. Serverless • storage as a service (e.g. Amazon S3), •

    database as a service (e.g. Amazon Aurora), • message queue as a service (e.g. Amazon SQS), • etc.
  6. Serverless replaces the traditional approaches to running applications.

  7. Platform options Physical servers

  8. Platform options Physical servers Virtual machines

  9. Platform options Physical servers Virtual machines Containers

  10. Platform options Physical servers Virtual machines Containers Platform

  11. Platform options Physical servers Virtual machines Containers Platform Serverless

  12. Platform options Physical servers Virtual machines Containers Platform Serverless Abstraction

  13. Platform options Physical servers Virtual machines Containers Platform Serverless Abstraction

    Control
  14. Why serverless?

  15. Why serverless? • We don't manage, update, configure, provision servers

    or containers,
  16. Why serverless? • We don't manage, update, configure, provision servers

    or containers, • We don't reserve or scale servers or containers, instead they are scaled automatically and transparently for us,
  17. Why serverless? • We don't manage, update, configure, provision servers

    or containers, • We don't reserve or scale servers or containers, instead they are scaled automatically and transparently for us, • We don't pay for fixed resources, instead we pay for what we actually use (e.g. execution time).
  18. Serverless can provide more scalable, affordable and reliable architectures for

    less effort.
  19. Data center evolution DC

  20. Data center evolution DC

  21. Data center evolution DC

  22. Data center evolution DC VM VM VM VM VM VM

    VM VM VM VM VM VM
  23. Data center evolution DC VM C C C C C

    C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C VM C C C C C C
  24. Data center evolution 
 DNS 
 CDN

  25. Data center evolution PaaS IaaS SaaS

  26. Function as a Service (FaaS)

  27. FaaS AWS Lambda Microsoft Azure Functions Google Cloud Functions

  28. FaaS AWS Lambda

  29. Bref

  30. $ composer require bref/bref CLI

  31. $ composer require bref/bref $ vendor/bin/bref init CLI

  32. <?php
 
 echo 'Hello world!'; index.php

  33. service: app
 
 provider:
 name: aws
 region: us-east-1
 runtime: provided


    
 plugins:
 - ./vendor/bref/bref
 
 functions:
 api:
 handler: index.php
 description: ''
 timeout: 28 # in seconds (API Gateway has a timeout of 29 seconds)
 layers:
 - ${bref:layer.php-73-fpm}
 events:
 - http: 'ANY /'
 - http: 'ANY /{proxy+}' serverless.yml
  34. $ composer require bref/bref $ vendor/bin/bref init
 
 $ serverless

    deploy CLI
  35. None
  36. <?php
 
 phpinfo(); index.php

  37. None
  38. None
  39. None
  40. None
  41. $ serverless invoke -f function "Hello world" CLI

  42. $ serverless invoke -f function -l
 
 "Hello world"
 


    ------------------------------------------------------
 START RequestId: 71dd0550-46b4-4f13-8438-8f687f385d0c Version: $LATEST
 
 END RequestId: 71dd0550-46b4-4f13-8438-8f687f385d0c
 
 REPORT
 RequestId: 71dd0550-46b4-4f13-8438-8f687f385d0c Duration: 14.20 ms
 Billed Duration: 100 ms
 Memory Size: 1024 MB
 Max Memory Used: 67 MB CLI
  43. <?php declare(strict_types=1);
 
 require __DIR__.'/vendor/autoload.php';
 
 lambda(function (array $event) {


    return 'Hello ' . ($event['name'] ?? 'world');
 }); index.php
  44. $ serverless invoke \
 > --function function \
 > --data='{"name":"PHPCon

    Poland"}'
 
 "Hello PHPCon Poland" CLI
  45. None
  46. Benchmarks: Cold starts 128 512 1024 2048 PHP function 500

    ms 240 ms 230 ms 220 ms HTTP application 345 ms 250 ms 240 ms 240 ms Symfony 5.4 s 1.4 s 800 ms 500 ms
  47. Benchmarks: Average (warm) execution time 128 512 1024 2048 PHP

    function 175 ms 35 ms 16 ms 13 ms HTTP application 10 ms 1 ms 1 ms 1 ms Symfony 58 ms 4 ms 4 ms 4 ms
  48. Benchmarks: CPU performance 128 512 1024 2048 bench.php 5.7 s

    1.4 s 0.65 s 0.33 s
  49. Maturity matrix Simplicity Performances Reliability Jobs, Cron ⬤ ⬤ ⬤

    API ⬤ ⬤ ⬤ API with MySQL / PostgreSQL ⬤ ⬤ ⬤ Website ⬤ ⬤ ⬤ Website with MySQL / PostgreSQL ⬤ ⬤ ⬤ Legacy application ⬤ ⬤ ⬤
  50. Runtime

  51. Who uses Laravel? ȱȱȱȱȱȱȱȱȱȱ

  52. Laravel Vapor

  53. Laravel Vapor

  54. PrettyCI

  55. PrettyCI

  56. PrettyCI

  57. Questions?

  58. Thank you!