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

サーバーレスPHP

 サーバーレスPHP

PHPerKaigi 2019でお話ししたスライドです。 #phperkaigi #b

Ippei Sumida

March 30, 2019
Tweet

More Decks by Ippei Sumida

Other Decks in Programming

Transcript

  1. αʔόʔϨε1)1
    1)1FS,BJHJ
    QIQFSLBJHJC

    View full-size slide

  2. XIPBNJ
    ֯ాɹҰฏʢ͢Έͩɹ͍ͬ΃͍ʣ
    ϑϦʔϥϯε8FCϓϩάϥϚʔ
    $PEFS%PKPຕํΦʔΨφΠβʔ
    ,BOTBJ8PSE1SFTT.FFUVQຕํ

    ΦʔΨφΠβʔ

    View full-size slide

  3. BHFOEB
    w αʔόʔϨεʹ͍ͭͯ
    w "84ͰαʔόʔϨε1)1͜ͱ͸͡Ί
    w 4ZNGPOZΛಥͬࠐΜͰΈΑ͏

    View full-size slide

  4. αʔόʔϨεͱ͸

    View full-size slide

  5. αʔόʔϨε
    Ұఆͷαʔόʔ༰ྔʹґଘ͢Δ͜ͱͳ͘ɺՔಇʹඞཁͳ
    ϦιʔεΛಈతʹઃఆ͠ɺϚωʔδυαʔϏεΛར༻͢
    ΔࣄͰγεςϜߏஙΛ͢Δӡ༻ํ๏ɻ

    View full-size slide

  6. Ϣʔβ 8FC4FSWFS %#
    Α͋͘Δαʔόߏ੒ྫ

    View full-size slide

  7. Ϣʔβ ֎෦αʔϏε
    "1*(BUFXBZ -BNCEB
    ɹ"844FSWFSMFTT"QQMJDBUJPOྫ

    View full-size slide

  8. Ϣʔβ -BNCEB ֎෦αʔϏε
    ɹ"844FSWFSMFTT"QQMJDBUJPOྫ

    View full-size slide

  9. Ϣʔβ "1*(BUFXBZ -BNCEB
    8FC4FSWFS %#
    /FUMJGZ
    ؅ཧऀ
    αʔόʔϨε
    ΦϯϓϨɾΫϥ΢υ

    View full-size slide

  10. αʔόʔϨεͷϝϦοτ
    w αʔόͷ؅ཧ΍ઃఆɺߋ৽Λ͠ͳͯ͘ྑ͍
    w ࣄલʹαʔό΍ίϯςφΛ༻ҙ͠ͳͯ͘ྑ͍
    w ΦϯϓϨɺΫϥ΢υͱൺ΂ͯ҆Ձ
    w "1*(BUFXBZ-BNCEBສϦΫΤετ·Ͱແྉ

    ʢҎ߱64%ສϦΫΤετʣ

    View full-size slide

  11. IUUQTBN[OUP"ZMSL

    View full-size slide

  12. $0#0-
    "84ۘ੡0443VOUJNF SE1BSUZۘ੡0443VOUJNF

    View full-size slide

  13. αʔόϨε1)1
    ͜ͱ͸͡Ί

    View full-size slide


  14. "1*
    8PSLFS #BUDI
    Ϩεϙϯε͕খ͍͞
    ϑΝΠϧอଘ͕ͳ͍
    ॲཧ͕୹͍
    XJUI%ZOBNP%#

    8FCαΠτ
    Ϩεϙϯε͕େ͖͍
    ϑΝΠϧอଘ͋Γ
    ॲཧ͕௕͍
    XJUI3%4
    XJUI71$

    View full-size slide

  15. ඞཁͳ΋ͷ
    w "84ΞΧ΢ϯτ
    w "84$-*
    w "844". 4FSWFSMFTT"QQMJDBUJPO.PEFM
    $-*
    w 4FSWFSMFTT'SBNFXPSL
    IUUQTTFSWFSMFTTDPN
    IUUQTBN[OUP$N3853
    IUUQTBN[OUP6)4JGE

    View full-size slide

  16. "84ܥͷઃఆ͸ׂѪ

    View full-size slide

  17. IUUQTHJUIVCDPNTUBDLFSZQIQMBNCEBMBZFS
    ར༻͢ΔϨΠϠʔ

    View full-size slide

  18. NZQSPKFDU
    ᵚᴸUFNQMBUFZBNM
    ᵎᴸTSD
    ᵎᴸQIQ
    ᵎᴸJOEFYQIQ
    σΟϨΫτϦߏ଄

    View full-size slide

  19. AWSTemplateFormatVersion: 2010-09-09
    Description: My PHP Application
    Transform: AWS::Serverless-2016-10-31
    Resources:
    phpserver:
    Type: AWS::Serverless::Function
    Properties:
    FunctionName: !Sub ${AWS::StackName}-phpserver
    Description: PHP Webserver
    CodeUri: src/php
    Runtime: provided
    Handler: index.php
    MemorySize: 3008
    Timeout: 30
    Tracing: Active
    Layers:
    - !Sub arn:aws:lambda:${AWS::Region}:887080169480:layer:php73:2
    Events:
    api:
    Type: Api
    Properties:
    Path: /{proxy+}
    Method: ANY
    UFNQMBUFZBNM
    -BNCEB'VODUJPO໊
    ϓϩάϥϜͷϧʔτσΟϨΫτϦ
    ࣮ߦ͞ΕΔϓϩάϥϜ
    ࢖༻͢Δ-BNCEBΧελϜϨΠϠʔ

    View full-size slide

  20. echo 'Hello world.’;
    TSDQIQJOEFYQIQ

    View full-size slide

  21. sam package --template-file template.yaml \
    --output-template-file [ग़ྗϑΝΠϧ].yaml \
    --s3-bucket [S3όέοτ໊]
    sam deploy --template-file [ग़ྗϑΝΠϧ].yaml \
    --stack-name [ελοΫ໊] \

    --capabilities CAPABILITY_IAM

    View full-size slide

  22. Ϣʔβ 4
    ϓϩάϥϜΛύοέʔδͯ͠
    Ξοϓϩʔυ
    TBNQBDLBHF
    $MPVE'PSNBUJPO༻

    ςϯϓϨʔτϑΝΠϧΛग़ྗ

    View full-size slide

  23. Ϣʔβ 4
    $MPVE'PSNBUJPO
    "1*(BUFXBZ -BNCEB
    4ͷύοέʔδΛςϯϓϨʔτϑΝΠϧΛجʹ

    -BNCEBʹσϓϩΠ-BNCEB "1*(BUFXBZΛઃఆ
    TBNEFQMPZ

    View full-size slide

  24. ݺͼग़͠63-
    ಠࣗυϝΠϯઃఆՄ

    View full-size slide

  25. 4ZNGPOZΛೖΕͯΈΔ

    View full-size slide

  26. IUUQTHJUIVCDPNTUBDLFSZQIQMBNCEBMBZFS

    View full-size slide

  27. ར༻Ͱ͖ΔϞδϡʔϧ
    C[TP
    DBMFOEBSTP
    DUZQFTP
    DVSMTP
    EPNTP
    FYJGTP
    pMFJOGPTP
    GUQTP
    HFUUFYUTP
    JDPOWTP
    KTPOTP
    QIBSTP
    QPTJYTP
    TINPQTP
    TJNQMFYNMTP
    TPDLFUTTP
    TZTWNTHTP
    TZTWTFNTP
    TZTWTINTP
    UPLFOJ[FSTP
    XEEYTP
    YNMTP
    YNMSFBEFSTP
    YNMXSJUFSTP
    YTMTP
    [JQTP

    View full-size slide

  28. extension=json.so
    TSDQIQQIQJOJ
    Ϟδϡʔϧར༻ʹ͸QIQJOJͷઃఆ͕ඞཁ

    View full-size slide

  29. ར༻Ͱ͖ΔϞδϡʔϧ
    C[TP
    DBMFOEBSTP
    DUZQFTP
    DVSMTP
    EPNTP
    FYJGTP
    pMFJOGPTP
    GUQTP
    HFUUFYUTP
    JDPOWTP
    KTPOTP
    QIBSTP
    QPTJYTP
    TINPQTP
    TJNQMFYNMTP
    TPDLFUTTP
    TZTWNTHTP
    TZTWTFNTP
    TZTWTINTP
    UPLFOJ[FSTP
    XEEYTP
    YNMTP
    YNMSFBEFSTP
    YNMXSJUFSTP
    YTMTP
    [JQTP

    View full-size slide

  30. %#ར༻ʹ͸ಠࣗϨΠϠʔ௥Ճ΋ඞཁ

    View full-size slide

  31. αʔόʔϨεͷϝϦοτ
    w αʔόͷ؅ཧ΍ઃఆɺߋ৽Λ͠ͳͯ͘ྑ͍
    w ࣄલʹαʔό΍ίϯςφΛ༻ҙ͠ͳͯ͘ྑ͍
    w ΦϯϓϨɺΫϥ΢υͱൺ΂ͯ҆Ձ
    ϝϦοτ͕୆ແ͠ʁ

    View full-size slide

  32. 4ZNGPOZಥͬࠐΊΔ͚Ͳ

    ͦͦ͜͜ΊΜͲ͍͘͞

    ੍࡞ɾஶ࡞
    ᴸᴸᴸᴸᴸ
    ⡲⡪⡲

    View full-size slide

  33. ͷ༧ఆͩͬͨɻ
    QIQMBNCEBMBZFSͰͷઃఆɿɹIUUQTVOQMBUJOGPCMPH

    View full-size slide

  34. IUUQTCSFGTI
    #SFG

    View full-size slide

  35. #SFG͕ఏڙ͢Δ΋ͷ
    1)1$VTUPN3VOUJNF-BZFS

    ˞QIQGQN

    ˞ඞཁϞδϡʔϧ༗ޮࡁΈ

    ˞QEP@NZTRM͸QIQJOJઃఆඞཁ
    $-*
    ֤छυΩϡϝϯτ

    View full-size slide

  36. composer create-project symfony/website-skeleton sample
    cd sample
    composer require mnapoli/bref

    View full-size slide

  37. AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Globals:
    Function:
    Environment:
    Variables:
    APP_ENV: prod
    Resources:
    Website:
    Type: AWS::Serverless::Function
    Properties:
    FunctionName: 'bref-symfony-website'
    CodeUri: .
    Handler: public/index.php
    Timeout: 30 # in seconds (API Gateway has a timeout of 30 seconds)
    MemorySize: 1024
    Runtime: provided
    Layers:
    - 'arn:aws:lambda:ap-northeast-1:209497400698:layer:php-73-fpm:1'
    Events:
    HttpRoot:
    Type: Api
    Properties:
    Path: /
    Method: ANY
    HttpSubPaths:
    Type: Api
    Properties:
    Path: /{proxy+}
    Method: ANY
    UFNQMBUFZBNM

    View full-size slide

  38. Console:
    Type: AWS::Serverless::Function
    Properties:
    FunctionName: 'bref-symfony-console'
    CodeUri: .
    Handler: bin/console
    Timeout: 120 # in seconds
    Runtime: provided
    Layers:
    - 'arn:aws:lambda:ap-northeast-1:209497400698:layer:php-73:1' # PHP
    - 'arn:aws:lambda:ap-northeast-1:209497400698:layer:console:1' # The "console" layer
    Outputs:
    DemoApi:
    Description: 'URL of our function in the *Prod* environment'
    Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'
    UFNQMBUFZBNM
    #SFGͷυΩϡϝϯτʹαϯϓϧ͋Γ

    View full-size slide

  39. public function getLogDir()
    {
    // When on the lambda only /tmp is writeable
    if (getenv('LAMBDA_TASK_ROOT') !== false) {
    return '/tmp/log/';
    }
    return $this->getProjectDir() . '/var/log';
    }
    public function getCacheDir()
    {
    // When on the lambda only /tmp is writeable
    if (getenv('LAMBDA_TASK_ROOT') !== false) {
    return '/tmp/cache/' . $this->environment;
    }
    return $this->getProjectDir() . '/var/cache/' . $this->environment;
    }
    TSD,FSOFMQIQ
    MPH DBDIFͷอଘઌมߋ

    UNQͷΈॻ͖ࠐΈ͕Մೳ

    ,FSOFMQIQΛͪΐͬͱ͚ͩΧελϚΠζ
    -BNCEBͷ͚࣌ͩͳͷͰɺଞ؀ڥ͸Өڹͳ͠

    View full-size slide

  40. composer install --optimize-autoloader --no-dev
    sam package --template-file template.yaml \
    --output-template-file serverless-output.yaml \
    --s3-bucket [S3όέοτ໊]
    sam deploy --template-file serverless-output.yaml \
    --stack-name [ελοΫ໊] \

    --capabilities CAPABILITY_IAM

    View full-size slide

  41. /**
    * @Route("/", name="top")
    * @param Request $request
    * @return \Symfony\Component\HttpFoundation\JsonResponse
    */
    public function index(Request $request)
    {
    return $this->json([
    'message' => 'Welcome to your new controller!',
    'path' => 'src/Controller/TopController.php',
    'name' => $request->query->get('name'),
    ]);
    }
    TSD$POUSPMMFS5PQ$POUSPMMFSQIQ
    "OOPUBUJPOͰͷϧʔςΟϯά͕ਖ਼ৗʹಈ࡞
    3FRVFTU͔Βͷ஋΋ਖ਼͘͠ड͚औΕΔ

    View full-size slide

  42. ॲཧɿ2JJUB͔ΒهࣄҰཡऔಘ

    ग़ྗɿ+40/

    ଎౓ɿNTʙT


    View full-size slide

  43. IUUQTBQJQMBUGPSNDPN
    "1*1MBUGPSN

    View full-size slide

  44. composer require api

    View full-size slide

  45. namespace App\Entity;
    use ApiPlatform\Core\Annotation\ApiResource;
    use Doctrine\ORM\Mapping as ORM;
    /**
    * @ApiResource()
    * @ORM\Entity(repositoryClass="App\Repository\ProductRepository")
    */
    class Product
    {
    TSD$POUSPMMFS5PQ$POUSPMMFSQIQ
    ͜ͷ"OOPUBUJPOΛ͍ΕΔͱzBQJQSPEVDUTzͱ͍͏3&45"1*͕Ͱ͖Δ

    View full-size slide

  46. ॲཧɿ%#͔Βσʔλऔಘ

    ̙̗ɿ֎෦αʔό.Z42-

    ग़ྗɿ+40/

    ଎౓ɿNTʙT


    View full-size slide

  47. Console:
    Type: AWS::Serverless::Function
    Properties:
    FunctionName: 'bref-symfony-console'
    CodeUri: .
    Handler: bin/console
    Timeout: 120 # in seconds
    Runtime: provided
    Layers:
    - 'arn:aws:lambda:ap-northeast-1:209497400698:layer:php-73:1' # PHP
    - 'arn:aws:lambda:ap-northeast-1:209497400698:layer:console:1' # The "console" layer
    Outputs:
    DemoApi:
    Description: 'URL of our function in the *Prod* environment'
    Value: !Sub 'https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/'
    UFNQMBUFZBNM
    -BNCBͰ4ZNGPOZ$POTPMF͕࣮ߦՄೳ

    View full-size slide

  48. vendor/bin/bref cli --region [Ϧʔδϣϯ] [ϑΝϯΫγϣϯ໊] -—
    [ίϚϯυ໊]

    View full-size slide

  49. vendor/bin/bref cli --region ap-northeast-1 bref-symfony-console --
    app:bref-sample
    Array
    (
    [0] => App\Entity\Product Object
    (
    [id:App\Entity\Product:private] => 1
    [name:App\Entity\Product:private] => ঎඼1
    [price:App\Entity\Product:private] => 100
    [published:App\Entity\Product:private] => 1
    )
    [1] => App\Entity\Product Object
    (
    [id:App\Entity\Product:private] => 2
    [name:App\Entity\Product:private] => ঎඼2
    [price:App\Entity\Product:private] => 200
    [published:App\Entity\Product:private] => 1
    )
    )
    [OK] You have a new command! Now make it your own! Pass --help to see
    your
    options.

    View full-size slide

  50. w αʔόΛ࣋ͭΑΓ҆Ձɺָʹӡ༻Ͱ͖Δ
    w "1* #BUDIͳͲൺֱతॲཧ࣌ؒͷ୹͍΋ͷʹ޲͍ͯΔ
    w ϑΝΠϧͷॻ͖ࠐΈ͸ෆՄʢUNQΛআ͘ʣ
    w 3%4 71$ͱ͸૬ੑΠϚΠν
    w "VSPSB4FSWFSMFTT͕"1*ܦ༝Ͱ42-࣮ߦͰ͖ΔΑ͏ʹͳΔͱ૬ੑ͍͍ʢݱࡏЌ൛ʣ
    w ઃఆ͕൥ࡶʹͳΔ৔߹͸ɺ4FSWFSMFTTҎ֎ͷબ୒ࢶ΋
    w #SFGΛར༻͢Δ͜ͱͰɺ؆୯ʹ4FSWFSMFTT4ZNGPOZ
    w -BSBWFM΋Ͱ͖ΔΑ
    w αʔϏε࢝·ͬͯؒ΋ͳ͍ͷͰɺࣄྫগͳΊɻ
    DPODMVTJPO

    View full-size slide

  51. !JQQFZ@T
    JQQFJTVNJEB
    JQQFZT!HNBJMDPN
    IUUQTVOQMBUJOGP
    BTLNFBOZUIJOH

    View full-size slide