Upgrade to Pro — share decks privately, control downloads, hide ads and more …

アンチパターンに踏み込む -作って理解するサーバーレスPHPフレームワーク / Stepping into anti-patterns -Serverless PHP frameworks to build and understand -.

アンチパターンに踏み込む -作って理解するサーバーレスPHPフレームワーク / Stepping into anti-patterns -Serverless PHP frameworks to build and understand -.

shiro seike
PRO

October 07, 2023
Tweet

More Decks by shiro seike

Other Decks in Programming

Transcript

  1. PHP Conference Japan
    2 0
    23
    2
    0
    23
    .
    10
    .
    8
    @seike
    4
    60
    1
    - PHP -

    View Slide

  2. 自己
    @seike
    46
    0
    -
    - @seike
    46
    0
    - AWS Community Builder Serverless
    - Fusic
    - /

    - /
    -
    - PHP
    - PHP
    - PHP
    -
    - Fukuoka.php
    - PHP 2023
    行 長
    2

    View Slide

  3. Agenda
    1
    .AWS Lambda
    2
    .Web AWS Lambda
    3
    .

    4
    .
    自 用
    5
    .
    小 方 用
    6
    .
    3

    View Slide

  4. 01
    AWS Lambda

    View Slide

  5. AWS Lambda
    AWS


    用 支
    5

    View Slide

  6. AWS Lambda

    Lambda

    行 生
    小 小
    行 小
    Lambda
    行 長 行
    VPC
    力 用
    ⾒ Lambda VPC

    VPC Lambda
    行 生

    Lambda SecretManager

    AWS CloudWatch Logs CloudWatch Metrics

    Lambda
    6

    View Slide

  7. 02
    Web AWS Lambda

    View Slide

  8. Bref
    Bref PHP OSS

    AWS Lambda PHP
    AWS Lambda PHP
    PHP

    PHP

    Bref PHP
    PHP
     
    AWS Lambda

    Developer Community
     
    Laravel Symfony

    PHP
     
    CLI
    8

    View Slide

  9. Developer Community
    AWS Lambda
    PHP

    PHP
    PHPer

    Bref
    Serverless Framework

    9
    service: app-php-serverless
    provider:
    name: aws
    region: ap-northeast-
    1
    plugins:
    - ./vendor/bref/bref
    functions:
    api:
    handler: index.php
    description: ''
    runtime: php-
    8
    2
    -fpm
    timeout:
    28
    events:
    - httpApi: '*'
    # Exclude files from deployment
    package:
    patterns:
    - '!node_modules/**'
    - '!tests/**'
    $ composer require bref/bref
    $ vendor/bin/bref init
    $ serverless deploy

    View Slide

  10. 10
    Bref AWS Lambda API Gateway

    Bref API Gateway AWS Lambda
    Bref Laravel Bridge Laravel
    Bref Lambda

    View Slide

  11. AWS Lambda PHP Bref by
    11

    View Slide

  12. Laravel Bref
    12
    Laravel Bref PHP

    AWS Lambda PHP

    AWS Lambda
    AWS Lambda Laravel
    PHPer

    View Slide

  13. View Slide

  14. AWS Lambda
    14 小
    Lambda

    行 生 大
    小 小   入
    行 小
    Lambda
    行 長 行
    PHP

    VPC
    力 用
    ⾒ Lambda VPC

    VPC Lambda
    行 生

    Lambda SecretManager

    AWS CloudWatch Logs CloudWatch Metrics

    Lambda

    View Slide

  15. Lambda
    Amazon Web Services Operating Lambda:

    Part
    3
    -Lambda
    -
    -
    小 用
    -
    -
    -
    -

    -
    15
    URL:https://aws.amazon.com/jp/blogs/news/compute-operating-lambda-anti-patterns-in-event-driven-architectures-part-
    3
    /
    Operating Lambda:

    Part
    3

    View Slide

  16. 自 自身

    View Slide

  17. Point

    View Slide

  18. 03

    View Slide

  19. Point
    目 入

    View Slide


  20. Laravel

    言 用 力
    生 行 生
    力 生 高

    Laravel

    文 文
    Laravel Web
    言 文 非 文
    20

    View Slide

  21. PHP AWS Lambda
    PHP

    2018
    PHP Conference
    201
    9
    AWS Lambda PHP
    21

    View Slide

  22. AWS Dev Day
    2 022
    AWS Dev Day
    2 02 2
    Amazon S
    3
    Select
    で バ 高
    https://speakerdeck.com/seike
    460
    /serverless-high-load-site-realized-with-amazon-s
    3
    -select
    22

    View Slide

  23. AWS Lambda x Bref x Laravel
    23 用 大

    - AWS Lambda
    - AWS S
    3
    Select
    - S
    3
    JSON SQL

    大 行
    PHP AWS Lambda
    行 一

    Laravel

    View Slide

  24. AWS Lambda x Bref x Laravel

    24
    Web
    AWS Lambda Bref Laravel ⾒

    0
    Laravel Laravel


    Laravel

    AWS ⾒

    View Slide

  25. Point
    AWS Lambda

    View Slide


  26.   用
    26

    View Slide

  27. 用 立

    27 用

    View Slide

  28. AWS ⾒
    AWS Lambda

    28

    View Slide


  29. 心 用
    非 大
    29

    View Slide

  30. Laravel
    30
    Web
    非 高
    AWS Lambda
      用
      文 入
    Laravel

    View Slide

  31. Web AWS Lambda
    一見 二
     
    AWS Lambda
     
     
     
      用 一
    31

    View Slide


  32. View Slide

  33. 04
    自 用

    View Slide

  34. ChatGPT

    ORM
    ・ ・ ・
    API

    RESTful API
    GraphQL
    HTTP
    34
    CI/CD ⾒
    API ⾒
    Webhook
    OAuth
    (RBAC)
    API
    (XSS)
    (CSRF)
    SQL
    (CSP)
    HTTPS/SSL
    (SSO)

    View Slide

  35. API

    35

    View Slide

  36. @o
    0
    h_
    36
    URL https://speakerdeck.com/o
    0
    h/phperkaigi-
    20 23

    View Slide

  37. PHP
    37

    View Slide

  38. Request GET POST URL
    Router URL
    Response
    DbManager DBRepository
    DbRepository
    Controller

    View

    Session
    Application
    ClassLoader

    PHP P.
    202
    38

    View Slide

  39. Request
    Router
    Response
    DbManager
    DbRepository
    Controller
    View
    Session
    Application
    ClassLoader
    PHP P.
    202
    39

    View Slide

  40. API Gateway
    40
    - Client Cognito ID
    - Client ID API Gateway Request
    - API Gateway Authorizer Cognito
    - API

    API Gateway
    CORS

    View Slide

  41. DB
    Request
    Router
    Response
    Controller
    Application
    ClassLoader
    41

    View Slide

  42. Model

    42
    URL https://speakerdeck.com/seike
    4 6
    0
    /aws-lambda-in-production-with-bref

    View Slide

  43. Model

    43
    URL https://speakerdeck.com/seike
    4 6
    0
    /aws-lambda-in-production-with-bref

    View Slide

  44. DynamoDB

    44
    URL https://github.com/baopham/laravel-dynamodb

    View Slide

  45. ThinApi

    View Slide

  46. 46
    ├ ─ ─
    ThinApiApplication.php
    ├ ─ ─
    bootstrap.php
    ├ ─ ─
    composer.json
    ├ ─ ─
    composer.lock
    ├ ─ ─
    controllers
    │ └ ─ ─
    ApiController.php
    ├ ─ ─
    core
    │ ├ ─ ─
    Application.php
    │ ├ ─ ─
    ClassLoader.php
    │ ├ ─ ─
    Controller.php
    │ ├ ─ ─
    HttpNotFoundException.php
    │ ├ ─ ─
    Request.php
    │ ├ ─ ─
    Response.php
    │ └ ─ ─
    Router.php
    ├ ─ ─
    serverless.yml
    └ ─ ─
    web
    └ ─ ─
    index.php
    API ThinApiApplication
    比 行

    非 小

    View Slide

  47. web/index.php
    47
    serverless.yml
    handler web/index.php

    View Slide

  48. bootstrap.php
    48
    bootstrap
    Model
    core registerDir
    ClassLoader

    View Slide

  49. core/ClassLoader.php
    49
    ClassLoader
    spl_autoload_register

    View Slide

  50. web/index.php
    50
    ThinApiApplication
    Application
    Core

    Application
    ThinApiApplication->run();

    View Slide

  51. ThinApiApplication.php
    51
    ThinApiApplication
    Routing
    Application run
    DB


    configure

    View Slide

  52. Application
    52
    run Router

    runAction

    response

    View Slide

  53. Application
    53
    run Router

    runAction

    response

    View Slide

  54. Response
    54
    Response
    Response send
    Client
    Core

    Application

    View Slide

  55. ApiController
    55
    ApiController

    API
    ThinApi

    View Slide

  56. deploy

    56
    Deploying app-bref-thin to stage dev (ap-northeast-1)
    ✔ Service deployed to stack app-bref-thin-dev (28s)
    endpoint: ANY - https://zzzzzzzzzz.execute-api.ap-northeast-1.amazonaws.com
    functions:
    api: app-bref-thin-dev-api (275 kB)
    Want a better experience than the AWS console? Try out https://dashboard.bref.sh
    28
    27 5
    kB

    View Slide

  57. deploy Laravel
    57
    Deploying bref-laravel to stage dev (ap-northeast-1)
    ✔ Service deployed to stack bref-laravel-dev (52s)
    endpoint: ANY - https://yyyyyyyyyy.execute-api.ap-northeast-1.amazonaws.com
    functions:
    web: bref-laravel-dev-web (32 MB)
    artisan: bref-laravel-dev-artisan (32 MB)
    Want a better experience than the AWS console? Try out https://dashboard.bref.sh
    28 -> 52
    275kB ->
    3 2
    M
    Laravel



    View Slide

  58. [#/sec]

    58
    Requests per second: 285.36 [#/sec] (mean)
    Requests per second: 347.26 [#/sec] (mean)
    Requests per second: 328.23 [#/sec] (mean)
    EC
    2
    Apache Bench
    347
    .
    26
    [#/sec]
    Response index.php 351.97[#/sec]
    3

    View Slide

  59. [#/sec] Laravel
    59
    $ ab -n 20000 -c 10 https://yyyyyyyyyy.execute-api.ap-northeast-1.amazonaws.com/
    ~
    Requests per second: 295.31 [#/sec] (mean)
    Requests per second: 303.91 [#/sec] (mean)
    Requests per second: 307.67 [#/sec] (mean)
    ~
    EC
    2
    Apache Bench
    347
    .
    26
    [#/sec] ->
    307
    .
    67
    [#/sec]
    Laravel

    3

    View Slide

  60. Web


     手
    Laravel
    Web
     
      用 一
     
    60

    View Slide

  61. Point
    Monolith First Serverless Development

    View Slide

  62. Monolith First Serverless Development
    62
    URL : https://speakerdeck.com/_kensh/monolith-first-serverless-development?slide=
    55
    @_kensh
    Monolith First Serverless Development

    View Slide


  63. Role
    APIGateway ⾒

    63
    functions:
    web:
    handler: public/index.php
    runtime: php-
    82
    -fpm
    timeout:
    28
    events:
    - httpApi: '*'
    admin-web:
    handler: public/index.php
    runtime: php-
    82
    -fpm
    timeout:
    28
    role: api-admin-role
    events:
    - httpApi:
    method: '*'
    path: /admin
    $ serverless deploy function --function admin-web
    Deploying function admin-web to stage dev (ap-northeast-
    1
    )
    ✔ Function code deployed (
    42
    s)
    Function configuration did not change, and the update was skipped. If you made changes to the service configuration and expected them to be
    deployed, it most likely means that they can only be applied with a full service deployment. (
    42
    s)

    View Slide

  64. Lambda Web Adapter
    Bref ⾒
    Lambda Web Adapter
    Parse
    HTTP
    HTTP
    64

    View Slide

  65. Lambda Web Adapter
    65

    View Slide

  66. Amazon Web Services - Labs OSS
    Amazon Web Services Labs
    OSS

    OSS
    自 用

    Runtime
    Bref

    66

    View Slide

  67. 05
    小 方 用

    View Slide



  68.  
    :

     
    :
      用
    :
     
    :

     
    : AWS
    68

    View Slide



  69.  
    :

     
    :
      用
    :
     
    :

     
    : AWS
    69

    View Slide



  70.  
    :

     
    :
      用
    :
     
    :

     
    : AWS
    70

    View Slide

  71. Web

    Laravel
    71

    View Slide

  72. 72 入 目 用
    Point
    1 力
    Point
    2
    Monolith First Serverless Development

    Laravel
    Point
    3
    方 小
    Laravel
    Point
    4

    View Slide

  73. Thank You
    We are Hiring !
    https://recruit.fusic.co.jp/

    View Slide