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

今こそはじめよう! 実践!サーバレスアーキテクチャ/serverless-sendai

今こそはじめよう! 実践!サーバレスアーキテクチャ/serverless-sendai

堀家隆宏

July 29, 2016
Tweet

More Decks by 堀家隆宏

Other Decks in Programming

Transcript

  1. ࠓͦ͜͸͡ΊΑ͏ʂ ࣮ફʂαʔόϨεΞʔΩςΫνϟ @Amimoto_Ami amimoto-ami.com

  2. ࣗݾ঺հ • ໊લɿງՈོ޺ • ॴଐɿAMIMOTO TEAM • Githubɿhttps://github.com/horike37 • Qiitaɿhttp://qiita.com/horike37

    • ಘҙͳ͜ͱɿAWS, JavaScript, WordPress
  3. ࠷ۙ։ൃͨ͠ αʔόϨεͳ OSSϓϩμΫτ

  4. https://github.com/horike37/simple-api-gateway Simple API Gateway • API GatewayͷઃఆΛ؆୯ʹ͢Δͱ͍ ͏ίϯηϓτ • ର࿩ίϚϯυʹΑΔઃఆͷखҾ

    • ϚοϐϯάςϯϓϨʔτ΍CORSͷઃ ఆΛҙࣝ͠ͳͯ͘ྑ͍
  5. https://github.com/horike37/serverless-command-line-event-args • Serverless FrameworkͷϓϥάΠϯ • Serverless FrameworkʹΑΔ։ൃͷޮ ཰Խ͕Մೳʹ • Lambdaʹ౉͢Ҿ਺ΛίϚϯυϥΠϯ

    Ͱ౉͢͜ͱ͕͖ͰΔ Serverless CommandLine Event Args
  6. ຊ୊

  7. αʔόϨε ΞʔΩςΫνϟ ͱ͸

  8. • Ϋϥ΢υͰఏڙ͞ΕΔαʔϏεར༻Λલఏʹߏங͢ΔαʔϏεٴͼΞϓϦέʔγϣϯ • Ծ૝ϚγϯΛ࢖ΘͣʹΞʔΩςΫνϟΛߏ੒ EC2

  9. Կ͕خ͍͠ͷ͔

  10. ຊདྷ΍Γ͔ͨͬͨίʔυΛॻ͘͜ͱʹ ूதग़དྷΔ؀ڥΛ࡞Δ

  11. • webαʔϏεΛ։ൃӡ༻ͯ͠ϏδωεΛ੒ޭͤ͞Δ͜ͱ • ͦͷͨΊʹΞϓϦέʔγϣϯͷίʔυΛॻ͘͜ͱ ຊདྷ΍Γ͔ͨͬͨ͜ͱ ͔͠͠ݱ࣮ʹ͸ίʔυΛॻ͚ͩ͘Ͱ͸ແ͍

  12. • OS΍ωοτϫʔΫͷઃఆɾ؅ཧ • αʔόͷߏஙɾϥϯλΠϜ؀ڥͷηοτΞοϓ • ϛυϧ΢ΣΞ΍ΞϓϦέʔγϣϯͷηΩϡϦςΟΞοϓσʔτ • Πϯϑϥͷϝϯςφϯε • ΞΫηε਺΍Ϣʔβ਺ͷ৳ͼʹର͢Δαʔόߏ੒ͷมߋɾΩϟύγςΟ

    ؅ཧ • ݸਓ৘ใͷ؅ཧ αʔό͕ଘࡏ͢Δ৔߹ʹൃੜ͢ΔλεΫ
  13. αʔό͕ଘࡏ͢ΔwebαʔϏεͷ্ཱͪ͛౰ॳ ίʔυͷ࣮૷ αʔόϝϯςφϯε αʔόͷߏங ΩϟύγςΟϓϥϯχϯά අ΍ͤΔ࣌ؒͷׂ߹ • ଟ͘ͷ࣌ؒΛίʔυͷ࣮૷ʹඅ΍ͤΔ

  14. αʔό͕ଘࡏ͢ΔwebαʔϏε͕੒௕͢Δͱ αʔόͷ؂ࢹ αʔόϝϯςφϯε ίʔυͷ࣮૷ ΩϟύγςΟ ϓϥϯχϯά අ΍ͤΔ࣌ؒͷׂ߹ • ૿͑ߦ͘ΞΫηε΍Ϣʔβ਺ʹର͢Δ αʔόͷߏ੒มߋɾΩϟύγςΟϓϥ

    ϯχϯά • ϞϊϦγοΫͳγεςϜʹର͢Δϝϯ ςφϯείετ • γϏΞͳݸਓ৘ใͷ؅ཧ • αʔόͷ؂ࢹ΍ͦΕʹର͢Δ24/365ͷ ରԠͷඞཁੑ ηΩϡϦςΟΞλοΫ ΁ͷରԠ
  15. ͍ͭͷؒʹ͔ଟ͘ͷ࣌ؒΛ෇ՃՁ஋ͷੜ·ͳ͍ ॏ࿑ಇʹඅ΍͍ͯ͠Δ

  16. Ͳ͏ղܾ͢Δͷ͔

  17. αʔό͕ଘࡏ͢ΔwebαʔϏεͷ੒௕ظ αʔόͷ؂ࢹ αʔόϝϯςφϯε ίʔυͷ࣮૷ ΩϟύγςΟ ϓϥϯχϯά අ΍ͤΔ࣌ؒͷׂ߹ • ૿͑ߦ͘ΞΫηε΍Ϣʔβ਺ʹର͢Δ αʔόͷߏ੒มߋɾΩϟύγςΟϓϥ

    ϯχϯά • ϞϊϦγοΫͳγεςϜʹର͢Δϝϯ ςφϯείετ • γϏΞͳݸਓ৘ใͷ؅ཧ • αʔόͷ؂ࢹ΍ͦΕʹର͢Δ24/365ͷ ରԠ໺ඞཁੑ %%PT߈ܸͷରԠ ͢΂ͯΛΫϥ΢υαʔϏε΁ Ξ΢τιʔε
  18. ݁Ռ΋ͨΒ͞ΕΔϝϦοτ Ϗδωε΁ͷ෇ՃՁ஋Λ͚ͭΔίʔυͷ࣮૷ʹ ଟ͘ͷ࣌ؒΛඅ΍ͤΔΑ͏ʹͳΔ

  19. ࣮ફฤ

  20. Ұൠతͳϒϥ΢βΞϓϦέʔγϣϯΛαʔόϨεΞʔΩςΫνϟͰ ࣮૷ͨ͠৔߹ͷߏ੒Λݟ͍͖ͯ·͠ΐ͏

  21. ϒϥ΢βΞϓϦέʔγϣϯͷߏ੒ਤ

  22. ϒϥ΢βΞϓϦέʔγϣϯͷߏ੒ਤ CognitoʹΑΔ ೝূɾೝՄ

  23. ϒϥ΢βΞϓϦέʔγϣϯͷߏ੒ਤ ಈతॲཧ͸API Gateway Λ௨ͯ͠LambdaͰॲཧ

  24. ϒϥ΢βΞϓϦέʔγϣϯͷߏ੒ਤ HTML,CSSͳͲ ੩తϦιʔε͸S3ʹઃஔ

  25. Cognito 
 User Pools ೝূɾೝՄ

  26. Cognito User Pools • ಠࣗͷϢʔβσΟϨΫτϦΛ࡞੒Ͱ͖ΔϑϧϚωʔδυαʔ Ϗε • ਺ԯϢʔβ·Ͱεέʔϧ • ೝূɾೝՄγεςϜΛεέʔϧͤ͞ɺηΩϡΞʹ͢Δͱ͍͏

    ॏ࿑ಇΛ͢΂ͯ΍ͬͯ͘ΕΔαʔϏε
  27. Cognito User Pools • جຊతʹ͸1ͭͷΞϓϦέʔγϣϯʹ͖ͭ1ͭͷUser Pool Λ࡞Δ • αΠϯΞοϓͷࡍͷύεϫʔυͷϙϦγʔ΍ೋஈ֊ೝূͳ ͲηΩϡϦςΟʹؔ͢ΔઃఆΛ֤User

    Poolຖʹઃఆ͢Δ • αΠϯΞοϓࡁΈͷϢʔβͷҰཡ͸Ϛωδϝϯτίϯιʔ ϧ͔Β֬ೝͰ͖Δ
  28. Amazon Cognito Identity SDK for JavaScript https://github.com/aws/amazon-cognito-identity-js • User PoolͱwebαΠτͱͷ࿈ܞΛߦ͏ϥΠϒϥϦ

    • αΠϯΠϯɺαΠϯΞοϓɺύεϫʔυϦϚΠϯμʔ ͳͲɺϢʔβ؅ཧΛߦ͏্Ͱඞཁͳϝιου͕શ ͯ༻ҙ͞Ε͍ͯΔ • JavaScriptͰϝιουΛهड़͢Δ͚ͩͰྑ͍ɻෳ ࡶͳϏδωεϩδοΫΛ࣮૷͢Δඞཁ͸ͳ͍
  29. Customer Authorizer API 
 Gateway Cognito User Pools • API

    GatewayʹೝূೝՄͷ࢓૊ΈΛUser PoolsΛ࢖࣮ͬͯݱͰ͖Δ • ϩάΠϯࡁΈͰ͋Ε͹APIͷॲཧΛಈ࡞͞ ͤͯɺະϩάΠϯঢ়ଶͰ͋Ε͹ɺAPIͷॲ ཧΛ࣮ࢪͤ͞ͳ͍ͱ͍ͬͨ͜ͱ͕Մೳ
  30. ຊ೔ϕʔλ͕औΕͯਖ਼ࣜϦϦʔε

  31. DynamoDB ಈతॲཧ Lambda API 
 Gateway

  32. API Gateway • ΞϓϦέʔγϣϯͷݰؔͱͯ͠ৼΔ෣͏ APIΛ࡞੒͢Δ͜ͱ ͕Ͱ͖ΔϚωʔδυαʔϏε • APIόοΫΤϯυγεςϜʹLambdaΛઃఆͯ͠֎෦͔Βͷೖ ྗσʔλΛॲཧͤ͞Δ •

    ͲͷΑ͏ͳεέʔϧͰ͋ͬͯ΋ɺ؆୯ʹ API ͷ࡞੒ɺ഑෍ɺ อकɺ؂ࢹɺอޢ͕ߦ͑Δ
  33. Lambda • ΞϓϦέʔγϣϯίʔυΛ࣮ߦͰ͖ΔίϯϐϡʔταʔϏε • Ϣʔβ͸ΞϓϦέʔγϣϯίʔυͷΈΛAWSʹΞοϓϩʔ υ͢Ε͹ྑ͍ɻϢʔβ͕੹೚Λෛ͏ͷ͸ίʔυͷΈ • αʔόɺOSɺΩϟύγςΟʔͷϓϩϏδϣχϯά͓Αͼࣗ ಈεέʔϦϯάɺίʔυͷϞχλϦϯά͓Αͼϩάه࿥͸ AWS͕؅ཧٴͼ࣮ࢪ͢Δ

  34. DynamoDB • ϑϧϚωʔδυͳNo SQLσʔλϕʔεαʔϏε • SPOF͕ଘࡏ͠ͳ͍ɻߴՄ༻ੑ 3 x ϨϓϦέʔγϣϯ •

    ετϨʔδ༰ྔ੍ݶͳ͠ • ਫฏεέʔϦϯάʹΑΔෛՙ෼ࢄ
  35. • ͜ͷ3ͭͷαʔϏεΛ૊Έ߹ΘͤΔ͜ͱͰɺΞϓϦέʔγϣ ϯʹඞཁͳσʔλͷೖग़ྗɺॲཧɺӬଓԽ͕ՄೳʹͳΔ αʔόϨεΞʔΩςΫνϟࡾछͷਆث

  36. αʔόϨεΞʔΩςΫνϟͰͷ։ൃΛΑΓޮ཰తʹ

  37. ։ൃπʔϧ΍ϑϨʔϜϫʔΫ Serverless Framework API GatewayٴͼLambdaɺͦͷଞAWSϦιʔεΛ࢖ͬͨ ΞϓϦέʔγϣϯΛߏங͢ΔͨΊͷϑϨʔϜϫʔΫ APEX LambdaϑΝϯΫγϣϯΛϏϧυɺσϓϩΠɺ؅ཧ͢ΔͨΊ ͷπʔϧ chalice

    Serverless FrameworkʹࣅͨϑϨʔϜϫʔΫ Lamvery LambdaͷσϓϩΠ΍Functionͦͷ΋ͷΛؚΊͨपลػೳͷ ઃఆɾ؅ཧΛࢧԉ͢Δπʔϧ Simple API Gateway API GatewayͷઃఆٴͼσϓϩΠΛ؆୯ʹ͢Δπʔϧ
  38. ੩తϦιʔε S3 Bucket

  39. Simple Strage Service • Πϯλʔωοτ༻ͷετϨʔδαʔϏε • εέʔϥϒϧͰ৴པੑ͕ߴ͘ɺ͔ͭߴ଎Ͱ҆Ձͳσʔλετ ϨʔδΠϯϑϥετϥΫνϟΛར༻Ͱ͖Δ
 • ϑϩϯτ༻ͷHTML͸S3όέοτʹ഑ஔ͢Δ͜ͱͰແݶͷε

    έʔϥϏϦςΟΛ࣮ݱ͢Δ
  40. ϑϩϯτͷϒϥ΢βͱAPIΛޮ཰Α͘઀ଓͯ͘͠ΕΔ΋ͷ͕ඞཁ

  41. ϑϩϯτͷϒϥ΢βͱAPIΛޮ཰Α͘઀ଓͯ͘͠ΕΔ΋ͷ͕ඞཁ Single Page ApplicationʹΑΔ ϒϥ΢βΞϓϦέʔγϣϯͷ։ൃ

  42. • ୯ҰϖʔδʹΑΔWebΞϓϦέʔγϣϯ • ϖʔδ͸ϋογϡͷมߋ΍ DOM ͷૢ࡞ʹΑͬͯ੾ ΓସΘΔ • αʔόͱͷ௨৴͸ Ajax

    ΍ WebSocket ͳͲͰߦ͏ Single Page Applicationͱ͸
  43. • APIͱϖʔδ͕׬શʹ෼཭͍ͯ͠ΔͨΊɺAPI ϑΝʔετͷߟ͑ํͰࣗવʹΞϓϦέʔγϣϯ͕૊ ΊΔ • ΫϥΠΞϯτͱAPIͦΕͧΕʹ։ൃϦιʔεΛ෼཭ ׂͯ͠Γ౰ͯΔ͜ͱ͕༰қ Single Page ApplicationͷϝϦοτ

  44. ϓϦίϯύΠϧʹΑΔΫϥΠΞϯταΠυϨϯμϦϯά API Gateway͔Βͷσʔλऔಘ ίϯύΠϧͯ͠HTMLͷੜ੒ ϒϥ΢βͰͷඳը

  45. ·ͱΊ • ͜͜·Ͱʹ঺հͨ͠αʔϏε΍πʔϧΛ࢖͏͜ͱͰैདྷͷϒ ϥ΢βΞϓϦέʔγϣϯΛαʔόϨεΞʔΩςΫνϟͰ։ൃɺ ӡ༻͢Δ͜ͱ͕ՄೳʹͳΔ

  46. ·ͱΊ http://qiita.com/horike37 ࠓ೔঺հͨ͠ΞʔΩςΫνϟͷৄࡉ͸QiitaͷํʹιʔείʔυؚΊͯॻ͍͍ͯ·͢ ڵຯ͋Δํ͸ੋඇͷ͍ͧͯΈ͍ͯͩ͘͞

  47. αʔόϨεΞʔΩςΫνϟͰ࠷΋େࣄͳ͜ͱ ࠷ޙʹ

  48. αʔόϨεΞʔΩςΫνϟͰ΍͍ͬͯ͘ؾ࣋ͪʂ

  49. ࣗ෼ͨͪͰ։୓͠ɺࣄྫΛ࡞͍ͬͯ͘ؾ࣋ͪʂ

  50. ͦΜͳ೤͍ؾ࣋ͪΛ͞Βʹ੝Γ্͛ͯ͘ΕΔΠϕϯτ͕͋Γ·͢ʂ

  51. Serverless Conf Tokyo͕։࠵͞Ε·͢ʂ http://tokyo.serverlessconf.io/

  52. @Amimoto_Ami amimoto-ami.com THANK YOU! ࠓͦ͜͸͡ΊΑ͏ʂ ࣮ફʂαʔόϨεΞʔΩςΫνϟ