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

サーバーレスピタゴラスイッチ

F0e950c6bf20815f3d169f063d3c5d4f?s=47 Aipa
May 29, 2020

 サーバーレスピタゴラスイッチ

F0e950c6bf20815f3d169f063d3c5d4f?s=128

Aipa

May 29, 2020
Tweet

More Decks by Aipa

Other Decks in Technology

Transcript

  1. αʔόʔϨε ϐλΰϥεΠον ΞΠύʔୂ௕ˏϥϯνษڧձ

  2. None
  3. ͏͚͝ʔʂ 63-͘Εʔ

  4. -BNCEB%FTUJOBUJPOT 63-ˠ)5.-อଘ

  5. -BNCEB5SJHHFS )5.-͔Β4DSBQ KTPOϑΝΠϧอଘ

  6. -BNCEB5SJHHFS KTPOϑΝΠϧ͔Β NQMJOLऔಘ ˠμ΢ϯϩʔυ NQϑΝΠϧอଘ

  7. -BNCEB5SJHHFS ಛ௃ྔ࡞੒ KTPOϑΝΠϧอଘ

  8. -BNCEB5SJHHFS σʔλอଘ

  9. -BNCEB5SJHHFS )5.-ςϯϓϨʔτऔಘ )5.-࡞੒ )5.-อଘ

  10. 4TUBUJDXFCTJUFIPTUJOH

  11. αʔόʔϨε ϐλΰϥεΠον

  12. ࠓ೔ͷ࿩ͷछ ࣗ෼ͷษڧํ๏ • ຊΛಡΈͳ͕Β࣮૷ • αʔϏεΛ࡞Δʢ։ൃ͢Δʣ

  13. AWSษڧ͢ΔͨΊʹɺ˓ϩαΠτ࡞Ζ͏ͱࢥͬͯ ࠓྲྀߦΓ͸Կ͔ͳʁͬͯௐ΂ͨ݁Ռ IUUQTUXJUUFSDPNBTBHJSJKPFTUBUVT

  14. ٹ͏ͧ

  15. Ϟνϕʔγϣϯ

  16. Ϟνϕʔγϣϯ • AWSͷࢼݧʹ߹֨͢ΔͨΊʹษڧ͍ͨ͠ • AWSΛษڧ͢ΔͨΊʹɺͰ͖Δ͚ͩ։ൃͤͣ AWSͷαʔϏεΛར༻ͯ͠αʔϏεల։Ͱ͖ ΔΑ͏ʹ͢Δ • AVۀքΛٹ͏

  17. 2019/12/21 AWS ιϦϡʔγϣϯ ΞʔΩςΫτ ΞιγΤΠτɹ߹֨

  18. Ϟνϕʔγϣϯ͕ݮΔ • AWSͷࢼݧʹ߹֨͢ΔͨΊʹษڧ͍ͨ͠ • AWSΛษڧ͢ΔͨΊʹɺͰ͖Δ͚ͩ։ൃͤͣ AWSͷαʔϏεΛར༻ͯ͠αʔϏεల։Ͱ͖ ΔΑ͏ʹ͢Δ • AVۀքΛٹ͏

  19. ٹ͏ͧ ʁʁʁ

  20. ຊฤ

  21. ࿩͢͜ͱ • ࡞ͬͨͷͰͦΕͰֶΜͩ͜ͱΛڞ༗͠·͢ • ϝΠϯ͸Lambda

  22. Lambdaͷ࿩

  23. Lambdaͱ͸ʁ • αʔόͷ༻ҙɾ؅ཧͤͣͱ΋ίʔυ͚ͩొ࿥͢Ε͹࣮ߦͰ͖Δ ίϯϐϡʔςΟϯάαʔϏε • APIͷΑ͏ʹୟ͖͍ͨͳΒAPI GatewayͱηοτͰར༻ɺόονॲཧͷ Α͏ʹͱ͋Δ࣌ؒʹ࣮ߦ͍ͨ͠ͱ͔ͳΒCloud Watch EventsͰ࣮ߦ

    etc… • ͍ͬͦ҆͘ʢ࣍ϖʔδʣ • 1,000,000݅/݄ͷϦΫΤετ͸ແྉ • 400,000GBඵ/݄·Ͱ͸ແྉʢ࣌ؒʹ͢Δͱ1೔ʣ
  24. Lambda ྉۚ • ؔ਺ʹ 512 MB ͷϝϞϦΛׂΓ౰ͯɺ3,000,000 ճ࣮ߦ͠ɺຖճͷ࣮ߦ͕࣌ؒ 1 ඵؒͩͬͨ

    ৔߹ • ߹ܭྉۚɿίϯϐϡʔςΟϯάྉۚ ʴ ϦΫΤετྉۚ → 18.34 USD ʴ 0.40 USD = 18.74 USD/݄ → ໿2000ԁ • ίϯϐϡʔςΟϯάྉۚɿ3,000,000 × 512 MB ÷ 1024 → 1,500,000 - 400,000 GB-ඵ (ແྉ࿮) → 1,100,000 GB-ඵ × 0.00001667 USD → 18.34 USD • ϦΫΤετྉۚɿ3,000,000 - 1,000,000(ແྉ࿮) → 2,000,000 × 0.2 USD ÷ 1,000,000 → 0.40 USD
  25. ͭ·Γʁʁʁ

  26. Lambda Functionઃఆը໘ ؔ਺ͷςετ -BNCEB5SJHHFS -BNCEB%FTUJOBUJPOT

  27. Lambda Functionઃఆը໘ ͍ΖΜͳݴޠαϙʔτ

  28. Lambda Functionઃఆը໘ ࠷େͰ໿(#ͷϝϞϦΛ ༩͑Δ͜ͱ͕Մೳ

  29. Lambda΁ͷؔ਺Ξοϓϩʔυ • ίʔυΛΠϯϥΠϯͰฤू • ։ൃͨ͠ίʔυΛίϐϖ͢Δ͔ɺLambda্Ͱ ։ൃ͢Δ

  30. Lambda΁ͷؔ਺Ξοϓϩʔυ • zipʹͯ͠Ξοϓϩʔυ • ॻ͍ͯ͋Δͱ͓Γ10MBͷ੍ݶ͕͋Δ Ҿ͔͔ͬΔͳΒS3Λར༻͢Ε͹Α͍

  31. Lambda΁ͷؔ਺Ξοϓϩʔυ • CIͰ΍ΔͳΒGitϦϙδτϦ͔ΒCodeBuild͠ ͯCloudformation΍CodeDeployΛ࢖ͬͯσ ϓϩΠํ๏΋͋Γ·͢ɻͥͻௐ΂ͯΈͯͶ

  32. Lambda΁ͷؔ਺Ξοϓϩʔυ

  33. Lambdaͷ੍ݶ • ؔ਺λΠϜΞ΢τ 15෼ʢ900ඵʣ • ࢖༻Ͱ͖ΔLambdaϨΠϠʔͷ਺ʢ5ʣ • σϓϩΠͰ͖ΔzipαΠζʢ50MBʣ • ղౚޙ͸ʁ

    ʢ250MBʣ
  34. Lambda Application • αʔόʔϨεΞϓϦέʔγϣϯΛ࡞੒Ͱ͖Δ • ۩ମతʹ͸Lambdaؔ਺ɺTriggerͱ͔ͷ૊ Έ߹Θͤɺ·ͨίʔυϏϧυɾσϓϩΠͳ Ͳ΋ͻͱ·ͱΊʹͨ͠΋ͷ • ςϯϓϨʔτ͕͍͔ͭ͘༻ҙ͞Ε͍ͯΔ

  35. Lambda Application

  36. ࡟আ͸ΊΜͲ͍ ൒खಈ࡟আ

  37. Lambda Layer • ෳ਺Lambdaؔ਺ؒͰϥΠϒϥϦΛڞ௨ԽͰ͖Δػೳ • Lambda1ɺLambda2ͷؔ਺Ͱ࢖༻͢ΔϥΠϒϥϦA ͕͋ͬͨ৔߹ɺ1ͱ2ͲͪΒʹ΋ύοέʔδϯάͯ͠σ ϓϩΠ͢Δඞཁ͕͋ͬͨ • ͭ·ΓΊΜͲ͍͘͞

    • ↑Λղܾ͢Δͷ͕Lambda Layer
  38. Lambda Layer

  39. Lambda Trigger • LambdaΛݺͼग़ͨ͢ΊͷԿ͔

  40. Lambda Trigger • ΊͬͪΌ͋Δ • ࠓճར༻ͨ͠ͷ͸S3ͱCloudWatch Events

  41. Lambda Trigger Cloud Watch Events • cronΈ͍ͨͳ͜ͱ͕Ͱ͖Δ • ϑΥʔϚοτ͸cronͱRateͱݺ͹ΕΔԿ͔͕͋Δ •

    ྫʣcronͷ৔߹: ຖ೔ޕલ 10:15 (UTC) → cron(15 10 * * ? *) • ଞʹ΋ϧʔϧλΠϓͰΠϕϯτύλʔϯ ʢEC2ͷεςʔλεมԽ౳ʣΛTriggerʹ͢Δ͜ͱ͕Ͱ͖Δ
  42. Lambda Trigger Cloud Watch Events

  43. Lambda Trigger S3 • S3όέοτʹର͠ɺΦϒδΣΫτ࡞੒΍࡟আͷΠϕϯτͰTriggerʹ͢Δ͜ͱ ͕Մೳ

  44. Lambda Trigger S3

  45. Lambda Trigger S3 (MBDJFSͷΠϕϯτ΋ 5SJHHFSͷର৅

  46. Lambda Destinations • Lambdaͷ࣮ߦ݁ՌʹΑͬͯɺ࣍ͷΞΫγϣϯΛࢦఆ͢Δػೳ • 2019/11/25͙Β͍ʹൃද • Lambdaͷ࣮ߦ͕੒ޭɾࣦഊͰॲཧઌΛมߋͰ͖Δ • Lambda,SNS,SQS,EventBridge(?)Λબ୒Ͱ͖Δ

    • ஫ҙ఺ͱͯ͠LambdaͷςετͰ͸ಈ͔ͳ͍ͷͰɺԿ͔ TriggerΛsetͯ͠ςετ͍ͯͩ͘͠͞ʢϋϚϦϙΠϯτʣ
  47. Lambda Destinations

  48. Lambda Destinations ૹ৴ઌͷ-BNCEBͷ "3/Λೖྗ

  49. LambdaͰΫϩʔϥʔ • μ΢ϯϩʔυॲཧͱεΫϨΠϓॲཧΛ෼཭ • εΫϨΠϓॲཧͰࣦഊͯ͠΋μ΢ϯϩʔυ ͠௚͞ͳͯ͘ྑ͍ʢෛՙΛ༩͑ͳ͍େࣄʣ XFC͔ΒIUNMΛμ΢ϯϩʔυ T͔ΒIUNMμ΢ϯϩʔυ IUNMΛอଘ

  50. S3 static website hosting • S3ΛWebαΠτͱͯ͠ެ։Ͱ͖Δػೳ • ੩తͳϦιʔεͳΒEC2ৗ࣌ىಈͰ͸ͳ͘ S3ͷ΄͏͕ίεύ࠷ߴ •

    ΋ͪΖΜRoute53Λ࢖ͬͯಠࣗυϝΠϯΛ ࢖༻͢Δ͜ͱ΋Մೳ
  51. S3 ϥΠϑαΠΫϧϙϦγʔ • Lambda͕ಈ࡞͢ΔதͰॲཧ͢Δதؒσʔλ͸ ࢒Γଓ͚ΔˠϦιʔε࢖͏ͷͰ՝ۚ͞ΕΔ • ΋͍ͬͨͳ͍͕૟আ͸ΊΜͲ͍ →ઃఆͰ͖Δ

  52. S3 ϥΠϑαΠΫϧϙϦγʔ

  53. S3 ϥΠϑαΠΫϧϙϦγʔ

  54. S3 ϥΠϑαΠΫϧϙϦγʔ

  55. ࡶײ

  56. ࡶײ • Lambdaϐλΰϥྑ͍ • αΠζ৺഑͚ͩͬͨͲOpenCVಈ͍͓ͨ • ࿈ܞ෦෼͸։ൃͤͣͱ΋ಈ͘ͷͰྑ͍

  57. ࠓޙ

  58. ࠓޙ • σϓϩΠ͕ͩΔ͍ • Lambdaͷ૊Έ߹ΘͤɺS3ͷΠϕϯτઃఆ౳ɺϒϥ΢β͔Βϙν ϙνͰ͖Δ͚ͲԿճ΋΍Δͷ͸ΊΜͲ͍ • CodePipelineͰࣗಈσϓϩΠʹ͍ͨ͠ • GitHub,

    CodeBuild, CloudFormationΛ૊Έ߹ΘͤΔ • ΤϥʔϋϯυϦϯά͍ͨ͠ • Ͳ͔͜ͷॲཧͰϛεͬͨΒSNSͰϝʔϧ or ॲཧΛϦτϥΠ