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*  8PSLFS #BUDI  Ϩεϙϯε͕খ͍͞  ϑΝΠϧอଘ͕ͳ͍ 

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

    4FSWFSMFTT'SBNFXPSL IUUQTTFSWFSMFTTDPN IUUQTBN[OUP$N3853 IUUQTBN[OUP6)4JGE
  3. 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ΧελϜϨΠϠʔ
  4. sam package --template-file template.yaml \ --output-template-file [ग़ྗϑΝΠϧ].yaml \ --s3-bucket [S3όέοτ໊]

    sam deploy --template-file [ग़ྗϑΝΠϧ].yaml \ --stack-name [ελοΫ໊] \
 --capabilities CAPABILITY_IAM
  5. ར༻Ͱ͖ΔϞδϡʔϧ 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
  6. ར༻Ͱ͖ΔϞδϡʔϧ 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
  7. 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
  8. 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ͷυΩϡϝϯτʹαϯϓϧ͋Γ
  9. 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ͷ͚࣌ͩͳͷͰɺଞ؀ڥ͸Өڹͳ͠
  10. 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
  11. /** * @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͔Βͷ஋΋ਖ਼͘͠ड͚औΕΔ
  12. <?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*͕Ͱ͖Δ
  13. 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͕࣮ߦՄೳ
  14. 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.
  15. w αʔόΛ࣋ͭΑΓ҆Ձɺָʹӡ༻Ͱ͖Δ w "1* #BUDIͳͲൺֱతॲཧ࣌ؒͷ୹͍΋ͷʹ޲͍ͯΔ w ϑΝΠϧͷॻ͖ࠐΈ͸ෆՄʢUNQΛআ͘ʣ w 3%4 71$ͱ͸૬ੑΠϚΠν

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