Serverless PHP @ SymfonyLive Warszawa 2019

Serverless PHP @ SymfonyLive Warszawa 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

June 14, 2019
Tweet

Transcript

  1. Serverless PHP Sebastian Grodzicki @ SymfonyLive Warszawa 2019 !@sebgrodzicki

  2. phpinfo() Sebastian Grodzicki
 • Team Lead at • PHP developer

    for 15+ years • #serverless !@sebgrodzicki
  3. 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.
  4. Serverless replaces the traditional approaches to running applications.

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

    Control
  6. 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).
  7. Serverless can provide more scalable, affordable and reliable architectures for

    less effort.
  8. Data center evolution DC

  9. Data center evolution DC VM VM VM VM VM VM

    VM VM VM VM VM VM
  10. 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
  11. Data center evolution 
 DNS 
 CDN

  12. Data center evolution PaaS IaaS SaaS

  13. Function as a Service (FaaS)

  14. FaaS AWS Lambda Microsoft Azure Functions Google Cloud Functions

  15. Bref

  16. $ composer require bref/bref CLI

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

  18. <?php declare(strict_types=1);
 
 require __DIR__.'/vendor/autoload.php';
 
 lambda(function (array $event) {


    return 'Hello ' . ($event['name'] ?? 'world');
 }); index.php
  19. AWSTemplateFormatVersion: '2010-09-09'
 Transform: AWS::Serverless-2016-10-31
 Description: ''
 
 Resources:
 MyFunction:
 Type:

    AWS::Serverless::Function
 Properties:
 FunctionName: 'my-function'
 Description: ''
 CodeUri: .
 Handler: index.php
 Timeout: 10
 MemorySize: 1024
 Runtime: provided
 Layers:
 - 'arn:aws:lambda:eu- central-1:209497400698:layer:php-73:6' template.yaml
  20. None
  21. $ composer require bref/bref $ vendor/bin/bref init $ aws s3

    mb s3://symfonylive CLI
  22. $ composer require bref/bref $ vendor/bin/bref init $ aws s3

    mb s3://symfonylive $ sam package \
 --output-template-file .stack.yaml \
 --s3-bucket symfonylive CLI
  23. $ composer require bref/bref $ vendor/bin/bref init $ aws s3

    mb s3://symfonylive $ sam package \
 --output-template-file .stack.yaml \
 --s3-bucket symfonylive $ sam deploy \
 --template-file .stack.yaml \
 --capabilities CAPABILITY_IAM \
 --stack-name SymfonyLive CLI
  24. None
  25. None
  26. $ vendor/bin/bref invoke my-function
 
 START RequestId: 745a76df-fddc-4cb4-b826-e169d43c67d3 Version: $LATEST


    
 END RequestId: 745a76df-fddc-4cb4-b826-e169d43c67d3
 
 REPORT RequestId: 745a76df-fddc-4cb4-b826-e169d43c67d3 Duration: 13.65 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 63 MB 
 
 "Hello world" CLI
  27. <?php declare(strict_types=1);
 
 require __DIR__.'/vendor/autoload.php';
 
 lambda(function (array $event) {


    return 'Hello ' . ($event['name'] ?? 'world');
 }); index.php
  28. $ vendor/bin/bref invoke my-function --event '{"name":"SymfonyLive"}'
 
 START RequestId: e44bd988-16aa-4a36-abf1-3924acfb7d42

    Version: $LATEST
 
 END RequestId: e44bd988-16aa-4a36-abf1-3924acfb7d42
 
 REPORT RequestId: e44bd988-16aa-4a36-abf1-3924acfb7d42 Duration: 13.27 ms Billed Duration: 100 ms Memory Size: 1024 MB Max Memory Used: 63 MB 
 
 "Hello SymfonyLive" CLI
  29. None
  30. None
  31. Benchmarks: Cold starts 128 512 1024 2048 PHP function 600

    ms 280 ms 225 ms 215 ms HTTP application 500 ms 270 ms 270 ms 215 ms Symfony 5.6 s 1.7 s 850 ms 550 ms
  32. Benchmarks: Average (warm) execution time 128 512 1024 2048 PHP

    function 130 ms 32 ms 20 ms 17 ms HTTP application 13 ms 5 ms 3 ms 2 ms Symfony 56 ms 12 ms 6 ms 5 ms
  33. Benchmarks: CPU performance 128 512 1024 2048 bench.php 6 s

    1.8 s 0.8 s 0.4 s
  34. Simplicity Performances Reliability Jobs, Cron ⬤ ⬤ ⬤ API ⬤

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

  36. PrettyCI

  37. PrettyCI

  38. PrettyCI

  39. Questions?

  40. Thank you!