サーバーレスPHP

 サーバーレスPHP

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

A73ed5cefbbd84abd2c5e14a5eb4a339?s=128

Ippei Sumida

March 30, 2019
Tweet

Transcript

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

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

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

  4. αʔόʔϨεͱ͸

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

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

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

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

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

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


    ʢҎ߱64%ສϦΫΤετʣ
  11. IUUQTBN[OUP"ZMSL

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

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

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

    ॲཧ͕୹͍  XJUI%ZOBNP%#  8FCαΠτ  Ϩεϙϯε͕େ͖͍  ϑΝΠϧอଘ͋Γ  ॲཧ͕௕͍  XJUI3%4  XJUI71$
  15. ඞཁͳ΋ͷ w "84ΞΧ΢ϯτ w "84$-* w "844". 4FSWFSMFTT"QQMJDBUJPO.PEFM $-* w

    4FSWFSMFTT'SBNFXPSL IUUQTTFSWFSMFTTDPN IUUQTBN[OUP$N3853 IUUQTBN[OUP6)4JGE
  16. "84ܥͷઃఆ͸ׂѪ

  17. IUUQTHJUIVCDPNTUBDLFSZQIQMBNCEBMBZFS ར༻͢ΔϨΠϠʔ

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

  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ΧελϜϨΠϠʔ
  20. <?php echo 'Hello world.’; TSDQIQJOEFYQIQ

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

    sam deploy --template-file [ग़ྗϑΝΠϧ].yaml \ --stack-name [ελοΫ໊] \
 --capabilities CAPABILITY_IAM
  22. Ϣʔβ 4 ϓϩάϥϜΛύοέʔδͯ͠ Ξοϓϩʔυ TBNQBDLBHF $MPVE'PSNBUJPO༻
 ςϯϓϨʔτϑΝΠϧΛग़ྗ

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

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

  25. None
  26. 4ZNGPOZΛೖΕͯΈΔ

  27. IUUQTHJUIVCDPNTUBDLFSZQIQMBNCEBMBZFS

  28. ར༻Ͱ͖ΔϞδϡʔϧ 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
  29. extension=json.so TSDQIQQIQJOJ Ϟδϡʔϧར༻ʹ͸QIQJOJͷઃఆ͕ඞཁ

  30. ར༻Ͱ͖ΔϞδϡʔϧ 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
  31. %#ར༻ʹ͸ಠࣗϨΠϠʔ௥Ճ΋ඞཁ

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

  33. 4ZNGPOZಥͬࠐΊΔ͚Ͳ
 ͦͦ͜͜ΊΜͲ͍͘͞ ऴ ੍࡞ɾஶ࡞ ᴸᴸᴸᴸᴸ ⡲⡪⡲

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

  35. IUUQTCSFGTI #SFG

  36. #SFG͕ఏڙ͢Δ΋ͷ  1)1$VTUPN3VOUJNF-BZFS
 ˞QIQGQN
 ˞ඞཁϞδϡʔϧ༗ޮࡁΈ
 ˞QEP@NZTRM͸QIQJOJઃఆඞཁ  $-*  ֤छυΩϡϝϯτ

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

  38. 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
  39. 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ͷυΩϡϝϯτʹαϯϓϧ͋Γ
  40. 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ͷ͚࣌ͩͳͷͰɺଞ؀ڥ͸Өڹͳ͠
  41. 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
  42. /** * @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͔Βͷ஋΋ਖ਼͘͠ड͚औΕΔ
  43. ॲཧɿ2JJUB͔ΒهࣄҰཡऔಘ
 ग़ྗɿ+40/
 ଎౓ɿNTʙT


  44. IUUQTBQJQMBUGPSNDPN "1*1MBUGPSN

  45. composer require api

  46. <?php 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*͕Ͱ͖Δ
  47. ॲཧɿ%#͔Βσʔλऔಘ
 ̙̗ɿ֎෦αʔό.Z42-
 ग़ྗɿ+40/
 ଎౓ɿNTʙT


  48. 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͕࣮ߦՄೳ
  49. vendor/bin/bref cli --region [Ϧʔδϣϯ] [ϑΝϯΫγϣϯ໊] -— [ίϚϯυ໊]

  50. 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.
  51. w αʔόΛ࣋ͭΑΓ҆Ձɺָʹӡ༻Ͱ͖Δ w "1* #BUDIͳͲൺֱతॲཧ࣌ؒͷ୹͍΋ͷʹ޲͍ͯΔ w ϑΝΠϧͷॻ͖ࠐΈ͸ෆՄʢUNQΛআ͘ʣ w 3%4 71$ͱ͸૬ੑΠϚΠν

    w "VSPSB4FSWFSMFTT͕"1*ܦ༝Ͱ42-࣮ߦͰ͖ΔΑ͏ʹͳΔͱ૬ੑ͍͍ʢݱࡏЌ൛ʣ w ઃఆ͕൥ࡶʹͳΔ৔߹͸ɺ4FSWFSMFTTҎ֎ͷબ୒ࢶ΋ w #SFGΛར༻͢Δ͜ͱͰɺ؆୯ʹ4FSWFSMFTT4ZNGPOZ w -BSBWFM΋Ͱ͖ΔΑ w αʔϏε࢝·ͬͯؒ΋ͳ͍ͷͰɺࣄྫগͳΊɻ DPODMVTJPO
  52. !JQQFZ@T JQQFJTVNJEB JQQFZT!HNBJMDPN IUUQTVOQMBUJOGP BTLNFBOZUIJOH