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

地方こそサーバーレス、その意義に迫るサーバーレスPHP / Serverless PHP: T...

地方こそサーバーレス、その意義に迫るサーバーレスPHP / Serverless PHP: The Rural Areas, and Why Serverless PHP Matters

PHPカンファレンス香川2024
https://phpcon.kagawa.jp/2024

shiro seike

May 11, 2024
Tweet

More Decks by shiro seike

Other Decks in Programming

Transcript

  1. ©Fusic Co., Ltd. 2 ਗ਼Ո ࢙࿠ @seike460 AWS Community Builders

    Serverless ݄ؒPHPΧϯϑΝϨϯε2024 - PHPΧϯϑΝϨϯε๺ւಓ2024 ొஃ (LT) - PHPΧϯϑΝϨϯεؔ੢2024 ొஃ (LT) - PHPerKaigi2024 ొஃ (LT) - PHPΧϯϑΝϨϯεখాݪ2024 ొஃ - PHPΧϯϑΝϨϯε߳઒2024 ొஃ - PHPΧϯϑΝϨϯε෱Ԭ2024 ొஃ༧ఆ ࣗݾ঺հ ͸͡Ίʹ גࣜձࣾFusic ϓϦϯγύϧΤϯδχΞ/ΤόϯδΣϦετ
  2. ©Fusic Co., Ltd. 3 CONTENTS ໨࣍ 1. αʔόʔϨεͱ͸ͳΜͳͷ͔ 2. αʔόʔϨεͷ੍໿ͱιϑτ΢ΣΞΞʔΩςΫνϟ

    3. ஍ํҊ݅ͱϑϨʔϜϫʔΫ 4. ٕज़ελοΫͷҰ؏ੑͱڞ༗஌ 5. ·ͱΊ
  3. ©Fusic Co., Ltd. 5 αʔόʔϨεͱ͸ͳΜͳͷ͔ 言 Cloud Native Computing Foundation

    CNCF Serverless Whitepaper v 1 . 0 行 https://github.com/cncf/wg-serverless/tree/master/whitepapers/serverless-overview
  4. ©Fusic Co., Ltd. 15 Ϋϥ΢υωΠςΟϒͰ͋Ζ͏ͱ͢Δ͜ͱ αʔόʔϨεΩϟογϡͰ༗໊ͳmomentoʹΑΔදݱ 1. ϓϩϏδϣϯෆཁɺ؅ཧෆཁ 2. ࠷௿ྉۚͷͳ͍࢖ͬͨ෼͚ͩͷࢧ෷

    3. 1 ͭͷ API ݺͼग़͠Ͱར༻Մೳ 4. ܭըఀࢭ͕ͳ͍ 5. Πϯελϯε͕ແ͍ αʔόʔϨεͷಛ௃ଊ͓͑ͯΓඇৗʹαʔόʔϨεΒ͍͠ ݸਓతͳҙࣝͱͯ͠͸ʮ౴͑ͩͱߟ͑Δඞཁ͸ͳ͍ʯ αʔόʔϨεΒ͍͠αʔϏε΋Ξοϓσʔτ͞Ε͍ͯ͘͠ɺ ৽͍֓͠೦ͷ௥ՃʹΑΓαʔόϨεΒ͠͞͸มΘ͍ͬͯ͘
  5. ©Fusic Co., Ltd. 16 AWSʹ͓͚ΔαʔόʔϨεΒ͍͠αʔϏε ࠓճ͸AWSʹ͓͚ΔαʔόʔϨεΒ͍͠αʔϏεʹ஫໨ - AWSΛ୅දͱ͢ΔαʔόʔϨεαʔϏε - AWS

    Lambda: Πϕϯτൃੜ࣌ʹίʔυΛ࣮ߦ͠ɺ ίϯϐϡʔςΟϯάϦιʔεΛࣗಈతʹ؅ཧ͢ΔίϯϐϡʔςΟϯάαʔϏε ʮαʔόʔϨεͱ͍͑͹AWS Lambdaʯͱߟ͑Δํ΋ଟ͍ͱࢥ͍·͢
  6. ©Fusic Co., Ltd. 22 αʔόʔϨεͷ੍໿ΛৼΓฦΔ - ࣮ߦ੍࣌ؒݶ - ࣮ߦ͕࣌ؒ௕͍͜ͱ͕ت͹͍͠ͷ͔ -

    Ϧιʔε੍ݶ - ڊେͳCPU΍ϝϞϦΛར༻͢Δͷ͸ت͹͍͠ͷ͔ - ಉ࣮࣌ߦ੍ݶ - ಉ࣮࣌ߦ͢Δϓϩηε਺͕ଟେͳࣄ͸ت͹͍͠ͷ͔ - APIϨʔτ੍ݶ - APIͷίʔϧ਺͕ଟେͳࣄ͸ت͹͍͠ͷ͔
  7. ©Fusic Co., Ltd. 30 ஍ํ։ൃͷ՝୊ 方   : 人  

    : Laravel   用 :   : 用   : AWS
  8. ©Fusic Co., Ltd. 34 Bref AWS Lambda͸PHPΛެࣜαϙʔτ͍ͯ͠·ͤΜ ͜Ε͸PHPίϛϡχςΟʹͱͬͯͷେ͖ͳোนͰ͋Γɺ PHP։ൃऀ͕αʔόʔϨεΞʔΩςΫνϟΛ׆༻͢ΔͷΛ೉͍ͯͨ͘͠͠ Brefͱ͸PHPͷOSSϓϩδΣΫτͷҰͭ

    AWS Lambda্ͰPHPΞϓϦέʔγϣϯΛ؆୯ʹσϓϩΠͱ؅ཧΛՄೳʹ͢Δπʔϧ - PHPϥϯλΠϜ ɹAWS LambdaͷΧελϜϥϯλΠϜΛར༻ɺDeveloper Community ڞ༗ͷΧελϜϥϯλΠϜΛఏڙ - ϑϨʔϜϫʔΫରԠ ɹLaravelɺSymfonyͳͲͷਓؾͷ͋ΔPHPϑϨʔϜϫʔΫΛαϙʔτ - ίϯιʔϧίϚϯυ ɹίϚϯυϥΠϯΠϯλʔϑΣʔεʢCLIʣΛఏڙ
  9. ©Fusic Co., Ltd. 35 σϓϩΠ ·ͣԿΑΓ΋خ͍͠ͷ͕ɺ ΧελϜϥϯλΠϜΛ ʮDeveloper CommunityͰڞ༗ग़དྷΔࣄʯ AWS

    Lambda͸ެࣜͷϥϯλΠϜʹ PHP͕༻ҙ͞Ε͍ͯͳ͍ͷ͸PHPͷࣄ৘Λߟ͑Δͱ PHPer͸ཧղͰ͖·͕͢ɺগ͠ਏ͕͋͞Γ·͢ ͦ͜ΛBref͸ղܾͯ͘͠Ε͓ͯΓɺ ҎԼίϚϯυʹͯࠨͷServerless Frameworkͷ ςϯϓϨʔτ͕ग़ྗ͞Ε·͢ (region౳͸ߋ৽ࡁ) $ composer require bref/bref $ vendor/bin/bref init $ serverless deploy
  10. ©Fusic Co., Ltd. 36 serverless framework Serverless Framework͸αʔόʔϨεΞʔΩςΫνϟͷΞϓϦέʔγϣϯΛ ؆୯ʹσϓϩΠ͠؅ཧ͢ΔͨΊͷOSS AWS

    LambdaɺGoogle Cloud FunctionsɺAzure FunctionsͳͲɺ ͞·͟·ͳΫϥ΢υϓϩόΠμʔΛαϙʔτ͍ͯ͠·͢ ։ൃऀ͸ɺಛఆͷΫϥ΢υϓϩόΠμʔʹґଘ͢Δ͜ͱͳ͘ɺ ౷Ұ͞ΕͨΠϯλʔϑΣʔεʢYamlʣΛ௨ͯ͡αʔόʔϨεΞϓϦέʔγϣϯΛߏஙՄೳ
  11. ©Fusic Co., Ltd. 37 ࣮ࡍͷಈ࡞ Bref͸AWS LambdaͱAPI GatewayͱͷؒͰڮ౉͠Λߦ͏ Bref͕API Gateway͔ΒϦΫΤετΛड͚औΓɺAWS

    Lambda͕ཧղͰ͖Δܗࣜʹม׵ ·ͨBref Laravel Bridgeͱ͍͏ϥΠϒϥϦΛ௨͠ɺLaravel͕ѻ͑Δܗࣜʹม׵ ͦͷϦΫΤετ৘ใ͕BrefʹΑͬͯLambdaؔ਺ʹ౉͞ΕΔ $ composer require bref/bref bref/laravel-bridge --update-with-dependencies $ serverless deploy
  12. ©Fusic Co., Ltd. 38 LaravelͱBref ͜ͷLaravelͱBrefͷPHPͷϥΠϒϥϦΛ༻͍Δ͜ͱͰ AWS Lambdaͷ্Ͱ؆୯ʹPHPΛಈ࡞ͤ͞Δࣄ͕Մೳʹ ΋ͪΖΜAWS LambdaͷΞϓϦέʔγϣϯͳͷͰɺ

    αʔόʔϨεͷجຊΛҙࣝ͠ͳ͍ͱ͍͚·ͤΜ͕ɺ ͦΕͰ΋AWS Lambda্ͰLaravel Λಈ͔͢͜ͱ͕ग़དྷΔ ͜Ε͸PHPerʹͱͬͯେ͖ͳਐาͰͨ͠
  13. ©Fusic Co., Ltd. 39 SQLΛ࢖͍͍ͨ৔߹͸αʔυύʔςΟΛར༻Մೳ - Supabase - Supabase͸ΦʔϓϯιʔεͷFirebase୅ସͱͯ͠ઃܭ͞Εͨ BaaSϓϥοτϑΥʔϜ

    PostgreSQLΛج൫ͱͨ͠ߴੑೳͳϦϨʔγϣφϧσʔλϕʔεΛఏڙ - TiDB Serverless - TiDB Serverless͸PingCAPʹΑͬͯ։ൃ͞Εͨ Φʔϓϯιʔεͷ෼ࢄܕSQLσʔλϕʔεͰ͋ΔTiDBͷServerlessόʔδϣϯ MySQLͱޓ׵ੑͷ͋ΔϦϨʔγϣφϧσʔλϕʔεΛఏڙ
  14. ©Fusic Co., Ltd. 40 جຊతʹ͸ઃఆ஋Λઃఆ͢Δ͚ͩ - Supabase - DB_CONNECTIONͷ஋ͷઃఆͷ࢓ํ͸ PHPΧϯϑΝϨϯε๺ւಓ2024ͷࢿྉࢀর

    - TiDB Serverless - MYSQL_ATTR_SSL_CAͷ஋Λ๨Εͣʹ AWS LambdaͰར༻͢Δ৔߹͸ҎԼͷ஋ - TLS Connections to TiDB Serverlessɿࢀর - https://docs.pingcap.com/tidbcloud/secure-connections-to-serverless-clusters MYSQL_ATTR_SSL_CA=/etc/pki/tls/certs/ca-bundle.crt
  15. ©Fusic Co., Ltd. 43 ΤϛϡϨʔτͯ͠։ൃ͢Δ BrefʹͯAWS LambdaΛΤϛϡϨʔτ LocalStackΛར༻ͯ͠ɺDynamoDBɺS3ɺSQS ͳͲΛϩʔΧϧΤϛϡϨʔτ -

    ΋ͪΖΜ௨ৗͷಈ࡞֬ೝ΋Մೳ - ϓϩάϥϜͷUnitςετ΋Մೳ - Integrationςετ΋࠶ݱՄೳ LocalStack͕ެ։͍ͯ͠ΔSample΋͋Γ·͢ https://github.com/localstack-samples/bref-localstack-sample
  16. ©Fusic Co., Ltd. 48 LambdaͷϞϊϦε Amazon Web Services ϒϩάͷʮOperating Lambda:

    ΠϕϯτۦಈܕΞʔΩςΫνϟʹ͓͚ΔΞϯνύλʔϯ – Part 3ʯ ҎԼͷΑ͏ʹ৮ΕΒΕ͍ͯ·͢ɻ -LambdaͷϞϊϦε -ύοέʔδαΠζ -࠷খݖݶΛద༻͢Δͷ͕೉͍͠ -ΞοϓάϨʔυ͕ࠔ೉ -อक͕ࠔ೉ -ҎԼ͸֘౰͠ͳ͍ -ίʔυͷ࠶ར༻͕೉͍͠ɿLaravelಘҙ -ςετ͕೉͍͠ɿLaravelಘҙ ࢀߟURL:https://aws.amazon.com/jp/blogs/news/compute-operating-lambda-anti-patterns-in-event-driven-architectures-part-3/ Operating Lambda: ΠϕϯτۦಈܕΞʔΩςΫνϟʹ͓͚ΔΞϯνύλʔϯ – Part 3
  17. ©Fusic Co., Ltd. 51 PythonʹΑΔαʔόʔϨε 入 API EC 2 API

    手 心 API Gateway -> Lambda -> S 3 Json EC 2 cron Python Python -> Go
  18. ©Fusic Co., Ltd. 55 ٕज़ελοΫͷҰ؏ੑͱڞ༗஌ νʔϜͷεΩϧηοτ ΤϯδχΞ͕LaravelΛར༻ͨ͠ΞϓϦέʔγϣϯʹ׳Ε͍ͯΔ ৽͍͠ݴޠ΍αʔόʔϨεΞʔΩςΫνϟʹ׬શʹ੾Γସ͑Δ͜ͱ͸࣌ؒͱ࿑ྗ͕͔͔Δ ٕज़తͳ౷Ұੑͱ஌ࣝͷڞ༗ LaravelͷΑ͏ͳ੒ख़ͨ͠ϑϨʔϜϫʔΫ͸υΩϡϝϯτ΋ॆ࣮͓ͯ͠ΓɺνʔϜશମͰͷٕज़త౷Ұੑ΍஌ࣝͷڞ༗Λαϙʔτ

    ૊৫ͷܧଓੑͱੜ࢈ੑͷҡ࣋ ٸͳٕज़ελοΫͷมߋ͸ϓϩδΣΫτͷਐߦΛ஗Βͤɺੜ࢈ੑΛ௿Լͤ͞ΔՄೳੑ͕͋Γ·͢ αʔόʔϨεͷՁ஋ʹઆಘྗΛ࣋ͨͤͯ౤ࢿΛճऩ͢Δ͔ɺੜ࢈ੑΛ֬อͨ͠ঢ়ଶͰ૊৫Λม͑Δ೉қ౓ͷߴ͞ จԽ΍ϚΠϯυηοτͷมԽ νʔϜͷจԽ΍ϚΠϯυηοτ͕LaravelΛ࢖ͬͨWebΞϓϦέʔγϣϯਂ͍ࠜͯ͘͟͠Δͷ΋ײ͡·ͨ͠ ϓϩάϥϜݴޠΛࣺͯΔɺจԽΛࣺͯΔͷ͸ඇৗʹ೉͘͠ɺจԽతͳมԽ͸ٕज़తͳมԽΑΓ΋஗͍͜ͱΛ࣮ײ͠·ͨ͠
  19. ©Fusic Co., Ltd. 57 PHPΛAWS Lambda্Ͱ࢖͏͜ͱΛݕ౼ AWS Dev Day 2022ʹͯϑϧαʔόʔϨεʹͯఏڙ͍ͯ͠ΔαΠτͷ࿩Λ͠·ͨ͠

    ʮAmazon S3 Select で ࣮ݱ͢Δαʔ バ ʔϨεߴෛՙରԠαΠτʯ https://speakerdeck.com/seike460/serverless-high-load-site-realized-with-amazon-s3-select
  20. ©Fusic Co., Ltd. 58 PHPΛAWS Lambda্Ͱ࢖͏͜ͱΛݕ౼ ಡΈࠐΈઐ༻ͷେྔͷΞΫηεΛͲ͏͞͹͔͘ɺ ߋʹ୹ೲظͰߦ͏ඞཁ͕͋Γ·ͨ͠ - AWS

    Lambda - AWS S3 Select - S3ʹ഑ஔ͞ΕͨJSONΛSQLͷϑΟϧλϦϯά ্هΛར༻ͨ͠ΞʔΩςΫνϟΛಈ࡞ͤ͞ͳ͕Βɺ େྔͷΞΫηεΛࡹ͘ࣄͰ͏·͘ཁ݅ʹରԠΛߦ͍·ͨ͠ ·ͨPHPͷ AWS LambdaΛઌߦͯ͠ࢼ͍ͯͨ͠ϝϯόʔͱҰॹʹ ։ൃΛଓ͚Δ͜ͱͰ·ͣ͸ຊ൪ʹಋೖ͢Δ͜ͱʹ੒ޭͨ͠ θϩεέʔϧ͢ΔLaravelΛνʔϜͰ׬੒ͤ͞Δ͜ͱ͕ग़དྷ·ͨ͠
  21. ©Fusic Co., Ltd. 67 νʔϜͰಋೖ͢Δ͜ͱ΋Ͱ͖ͨ ؆қͳWebϑΥʔϜ ͨͩ͠଴ػίετΛ௿ݮ͍ͤͨ͞ AWS LambdaͱPHPΛ૊Έ߹Θͤͯ एखͷϝϯόʔʹߏஙͯ͠΋Β͍ͳ͕Β

    ଴ػίετΛ΄΅0ͷঢ়ଶͰߏஙΛ׬ྃ Laravelͷ஌ࣝ͸͋ͬͨͷͰɺ LaravelͷߏஙΛߦ͏Α͏ʹɺ ࣗ෼Ҏ֎ͷϝϯόʔʹߏஙͤ͞Δ͜ͱ͕Ͱ͖ͨ Laravelར༻ʹΑΓηΩϡϦςΟରࡦ΋͠ͳ͕Βɺ AWSͱͷ౷߹΋೉ͳ࣮͘૷͍ͯ͠·ͨ͠
  22. ©Fusic Co., Ltd. 69 Monolith First Serverless Development ࢀߟURL :

    https://speakerdeck.com/_kensh/monolith-first-serverless-development?slide=55 @_kensh ʮMonolith First Serverless Developmentʯ
  23. ©Fusic Co., Ltd. 70 ΤϯυϙΠϯτΛ෼͚Δ͜ͱͰ֦ுʹ΋ରԠ 用 Role APIGateway 示 functions:

    web: handler: public/index.php runtime: php- 8 2 -fpm timeout: 28 events: - httpApi: '*' admin-web: handler: public/index.php runtime: php- 8 2 -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)
  24. ©Fusic Co., Ltd. 71 ஍ํͦ͜αʔόʔϨε • αʔόʔϨεΛಋೖ͠΍͘͢ίετϝϦοτ͕ग़΍͍͢ • ׬ᘳΛٻΊͣαʔόʔϨεʹPHPΛಋೖ͢Δ͜ͱΛબ୒ࢶʹ •

    αʔόʔϨεΛಋೖ͢Δ͜ͱͰιϑτ΢ΣΞΞʔΩςΫνϟͱͯ͠΋༏Εͨ΋ͷʹ Everything will be Serverless ͦΜͳະདྷΛ৴ͯ͡αʔόʔϨεPHPΛ΍ͬͯΈ·ͤΜ͔ʁ
  25. ©Fusic Co., Ltd. 73 ·ͱΊ αʔό؅ཧΛඞཁͱ͠ͳ͍ΞϓϦέʔγϣϯͷߏஙͱ࣮ߦͷʮ֓೦ʯ Point 01 αʔόʔϨε͸΋ͪΖΜσϝϦοτ΋͋Δ͕ɺ੍໿Λड͚ೖΕΔࣄ͸࣮͸ωΨςΟϒ͡Όͳ͍ Point

    02 LaravelͳͲͷڧྗͳϑϨʔϜϫʔΫΛར༻ͨ͠Monolith First Serverless Developmentͱ͍͏ߟ͑ํ Point 03 ஍ํҊ݅ʹͦ͜αʔόʔϨε͸ಋೖ͠΍͘͢ɺޮՌ΋ग़΍͍͢ɻબ୒ࢶͷҰͭʹͲ͏Ͱ͠ΐ͏͔ Point 04