実践サーバレス + α / Serverless Practices

5cd1b99b9950b26e8ee005bd6cd1a860?s=47 Masashi Terui
November 17, 2018

実践サーバレス + α / Serverless Practices

We wanna Tech@青森
https://connpass.com/event/103101/

5cd1b99b9950b26e8ee005bd6cd1a860?s=128

Masashi Terui

November 17, 2018
Tweet

Transcript

  1. SERVERLESS PRACTICES + α MASASHI TERUI @ WE WANNA TECH

    AOMORI
  2. SERVERWORKS CO.,LTD. + FREELANCER • Serverless Oji-san • Serverlessconf Tokyo

    2016,2017,2018 speaker • Remote worker (in Sapporo) • @marcy_terui • https://willy.works MASASHI TERUI ARCHITECT / DEVELOPER
  3. AGENDA SERVERLESS ͬͯͳΜ͚ͩͬʁ 1 6 2 7 3 8 4

    9 5 10 SERVERLESS ͷ࣮ମΛଊ͑Δ SERVERLESS ͷઃܭ SERVERLESS ͷ࣮૷ SERVERLESS ͷσϓϩΠ SERVERLESS ͷӡ༻ SERVERLESS ͷݱ࣮ SERVERLESS ͷະདྷ SERVERLESS ͷ͓·͚ SERVERLESS ͷςετ
  4. SERVERLESSͬͯͳΜ͚ͩͬʁ WHAT IS

  5. CNCF SERVERLESS WHITEPAPER V1.0 • Serverless computing refers to the

    concept of building and running applications that do not require server management • A platform may provide one or both of the following: • Functions-as-a-Service (FaaS) • Backend-as-a-Service (BaaS) • Products or platforms deliver the following benefits to developers: • Zero Server Ops • No Compute Cost When Idle 
 https://github.com/cncf/wg-serverless/tree/master/whitepaper
  6. FaaS + Functional SaaS (BaaS) = Serverless https://www.slideshare.net/acloudguru/ant-stanley-being-serverless

  7. SERVERLESS USE CASES (FROM CNCF WP) • Asynchronous, concurrent, easy

    to parallelize into independent units of work • Infrequent or has sporadic demand, with large, unpredictable variance in scaling requirements • Stateless, ephemeral, without a major need for instantaneous cold start time • Highly dynamic in terms of changing business requirements that drive a need for accelerated developer velocity • Non-HTTP-centric and non-elastic scale workloads that weren’t good fits for an IaaS, PaaS, or CaaS solution (Event Driven workloads)
  8. SERVERLESSͷ࣮ମΛଊ͑Δ

  9. FaaSͬͯͲΜͳ΋ͷʁ

  10. SERVERLESS PROCESSING MODEL https://github.com/cncf/wg-serverless/tree/master/whitepaper#detail-view-serverless-processing-model ॲཧͷશମ૾Λ௫Ή Πϕϯτιʔε͔ΒͷΠϕϯτॲཧཁٻΛ ଟ਺ͷΠϯελϯε্ͷFunction͕෼ࢄॲཧ

  11. THE INTERNAL FLOW OF PROCESSING https://github.com/apache/incubator-openwhisk/blob/master/docs/about.md#the-internal-flow-of-processing Πϕϯτ͕ॲཧ͞ΕΔྲྀΕΛ௫Ή ετϦʔϜ΍ΩϡʔΛڬΜͰ෼ࢄॲཧ͢Δͷ͕جຊ ಉظͷ৔߹͸௚઀TCPͰ௨৴͢ΔΒ͍͠(Lambda͸)

  12. FaaS ≒ ίϯςφͰִ཭͞Εͨؔ਺

  13. BaaSͬͯͲΜͳ΋ͷʁ

  14. FROM OWNERSHIP TO USE SERVICES Ϋϥ΢υʹΑͬͯαʔό͕ॴ༗͔Βར༻΁ ͞Βʹϛυϧ΢ΣΞ΍ϥΠϒϥϦ΋ར༻΁ BaaSΛ࢖͏ͷ͸ͦ͏͍͏ࣗવͳྲྀΕ

  15. BaaS ≒ ϑϧϚωʔδυ͔ͭந৅Խ͞ΕͨMW

  16. SERVERLESS͸ؔ਺ͱMWͰͰ͖ͯΔ ߴ౓ʹந৅Խ͞ΕͯΔ͚ͩ

  17. ΍Δ΂͖͜ͱΛͪΌΜͱ΍Δ SERVERLESS͔ͩΒͬͯࣼʹߏ͑ͣʹ

  18. SERVERLESSͷઃܭ

  19. • ϝοηʔδϯά • ಉظ/ඇಉظ • Interactive/PubSub • σʔλετΞ • εέʔϦϯά

    • σʔλΞΫηε • ࣮ߦํ๏ • ฒྻ/௚ྻ • ܾఆత/ඇܾఆత ໨తΛՌͨͨ͢Ίͷ࠷దղ͸ҟͳΔ
  20. BE EVENT DRIVEN

  21. ͢΂ͯ͸EVENTͱHANDLERͷ૊Έ߹Θͤ

  22. • ಉظ • API Gateway • ඇಉظ • ௚ྻ •

    Kinesis Streams • ฒྻ • SNS • SQS • ͦͷଞ • Step Functions EVENTΛͲͷΑ͏ʹॲཧ͢΂͖͔ʁ
  23. ඇಉظɾ෼ࢄΛϕʔεʹߟ͑Δ Կ΋ͤͣʹ଴ͬͯΔͳΒࣗ෼(MONOLITH)Ͱ΍Ε͹ྑ͍ͷͰ͸ʁ

  24. • ͭ·ΔॴΤϥʔϋϯυϦϯά • ϦτϥΠՄೳͳΤϥʔ͸ϦτϥΠ͢Δ • ಉظͰ΍Ε͹ΫϥΠΞϯτ͕ϦτϥΠͯ͘͠ΕΔͱ͍͏ͷ͸ݬ૝ • ϦτϥΠͰ͖ͳ͍Τϥʔ΋ҰͭͷΠϕϯτͰ͋Δ • ผͷܦ࿏ͰવΔ΂͖ॴʹ఻͑Δͱ͍͏͜ͱΛߟ͑Δ

    • ͦͯ͠ϞχλϦϯάɾτϨʔγϯά • ඞཁͳ৘ใ͸ू໿͢Δ • ৄ͘͠͸͋ͱͰ ඇಉظɾ෼ࢄʹ͓͚Δෆ҆
  25. Event Source + Handler = Microservice ʓʓʹରͯ͠☓☓͢Δɺશͯ͸ͦͷ૊Έ߹Θͤ

  26. • ࣮ࡍʹEvent SourceͱHandlerຖʹ؅ཧ୯ҐΛ෼͚Δ͔͸։ൃઓུґଘ • গਓ਺Ͱશ෦࡞ͬͯΔͷʹσϓϩΠύΠϓϥΠϯ΍GitϦϙδτϦΛ
 ෼͚Δͷ͸ΦʔόʔϔουͰ͔͠ͳ͍ • Event SourceͱHandlerͷ૊Έ߹ΘͤͰॲཧ಺༰͕ࣗ໌͔ͭ؅ཧՄೳͳ
 ঢ়ଶΛอ͍ͬͯΕ͹ޙͰ෼͚Δͷ͸ۤ͡Όͳ͍

    • ೚ҙͷίϯςΩετͰEvent SourceΛ઀ଓ఺ʹ্ख͘੾Γग़ͤΔ • େࣄͳͷ͸Function͕૿͑Δ͜ͱΛԀΘͣઃܭͷෳࡶԽΛݏ͏͜ͱ Practices
  27. • ґଘؔ܎ͷ޲͖Λἧ͑Δ • ඇಉظΛॏࢹ • Event SourceΛू໿఺ɾ෼ذ఺ͱ͢Δ • ෳࡶԽͷݪҼ͸େ఍σʔλΞΫηεʹ͋Δ •

    ॻ͖ࠐΈͱࢀরؔ܎ͷෳࡶԽ • σʔλͷॴ༗ؔ܎ • CQRS, Materialized View ෳࡶԽͤ͞ͳ͍ͨΊʹ
  28. • ॻ͖ࠐΈͱಡΈࠐΈͰಉ͡σʔλΛѻ͏ඞཁ͸ͳ͍ • ॻ͖ࠐΈͷݪ୯ҐΛߟ͑ɺΞΠςϜʹ·ͱΊΔ͜ͱͰ੔߹ੑΛकΔ • ಡΈࠐΈ͸݁Ռ੔߹Λड͚ೖΕͯޮ཰ͷྑ͍σʔλΛੜ੒͍ͯ͘͠ • Ͱ͖ΔݶΓҰͭͷHandler͕ѻ͏΂͖σʔλΛҰͭͱ͢Δ • Write͸ൺֱత؆୯

    • Read͸CQRS, Materialized ViewΛ্ख͘࢖͏ Data Modeling
  29. ۩ମతʹ͸ “DynamoDB ދͷר” [ݕࡧ] http://marcy.hatenablog.com/entry/2018/07/31/213705

  30. SERVERLESSͷ࣮૷

  31. • EventΛҾ਺ʹ݁ՌΛฦ͢ • ͨͩͦΕ͚ͩͳͷͰผʹී௨ʹҰൠతʹྑ͍ͱ͞ΕΔίʔυॻ͚͹͓̺ • ͋Δͱ͢Ε͹ • FaaSͷੑ্࣭ύϑΥʔϚϯεʹؔΘΔTips • EventॲཧͷϙΠϯτ

    ݴ͏ͯͨͩͷؔ਺
  32. • Cold Start/Warn StartΛҙࣝ͢Δ • ίʔυ͕ಉ͡Ͱ͋Ε͹Ұ౓ىಈͨ͠ίϯςφ͸͋Δఔ౓࢒Δʢ਺ेඵ) • ಉ͡ίϯςφ͕࠶ར༻͞ΕΔ -> Warm

    Start • ৽͍͠ίϯςφ্ཱ͕͕ͪΔ -> Cold Start (Φʔόʔϔου͕͋Δ) • ෆཁͳΦϒδΣΫτΛ޿͍ScopeͰੜ੒͠ͳ͍ • Warm Start࣌ʹ͸Global Scope΍StaticͳΦϒδΣΫτ͸࢒Δ • ࢖͍ճͤΔΦϒδΣΫτ͸࢖͍ճ͢(Lazy Loading, Singleton) • ಛʹTCPίωΫγϣϯ࣋ͬͯΔΦϒδΣΫτͱ͔͸Φʔόϔου͕σΧΠ͔Βେࣄ FaaSͷੑ্࣭ύϑΥʔϚϯεʹؔΘΔTips
  33. • Event Sourceͷछผຖʹॲཧํ๏ΛܾΊͯ౷Ұ͢Δ • ϝοηʔδͷड͚౉͠ํ • JSON/ProtocolBuffers, Plain/GZip etc… •

    Trace ID(ޙड़)ͳͲͷMetadataͷ࢓ࠐΈํ • ྫ֎ॲཧ • ແࢹʢϩάʹ࢒͢ʣͯ͠ྑ͍΋ͷ • ൃੜͨ࣌͠఺Ͱผϧʔτʹྲྀ͢΂͖΋ͷ • Failͤͯ͞ϦτϥΠػߏʹ৐ͤΔ΋ͷ • PythonͩͱDecoratorΛ࢖͏ͱศར EventॲཧͷϙΠϯτ
  34. DECORATOR Trace IDΛϔομͰ΍ΓऔΓ EventͱResult͸ඞͣϩάʹ࢒͢ ͱ͍͏࠷௿ݶͳαϯϓϧ

  35. SERVERLESSͷςετ

  36. UNIT TESTΛॻ͘ •ී௨ʹؔ਺ͩΑʁ •↓ඞݟ • https://speakerdeck.com/twada/ test-driven-architecture-aws-dev- day-tokyo-2018

  37. • جຊతʹ࣮ࡍͷAWS αʔϏεΛ࢖ͬͯ΍Δ • ౷߹؅ཧͰ͖ΔFWΛબͿ(AWS SAM, Serverless etc…) • ͦͷ··ಉ͡؀ڥ͕σϓϩΠͰ͖Δ

    • ςετ͍ͨ͠ൣғΛܾΊɺऴ୺ͷEvent SourceͱHandlerΛஔ͖׵͑Δ • ؀ڥม਺ͰEvent SourceΛ੾Γସ͑ • VerifyՄೳͳܗͰOutputͤ͞Δ Integration Test
  38. PRODUCTION SSM Parameter Store΍CFn Expot/Import ValueͰ֎෦ࢀরՄೳͱ͓ͯ͘͠ ؀ڥม਺͔ΒEndpointΛ஫ೖ͢ΔΑ͏ʹ࣮૷

  39. INTEGRATION TEST ͨͩདྷͨEventΛPut͢Δ͚ͩˠͦΕΛVerify͢Δ ʢTrace IDͷ࢓૊Έ͕͋Δͱঘྑʣ

  40. SERVERLESSͷσϓϩΠ

  41. • ࢖͏αʔϏε͕౷߹؅ཧͰ͖ΔFWΛબͿ(AWS SAM, Serverless etc…) • ઌड़ͷIntegration Testͷ࢓૊Έ͸CFnͷػೳʹڧ͘ґଘ͢ΔͷͰ
 CFnϕʔεͷFW͡Όͳ͍ͱ࢖͑ͳ͍ςΫχοΫ •

    ൣғ͕ݶఆతͳ΋ͷ͸ͦͷൣғͰऩ·Δ৔߹ʹͷΈ(Chalice, Apex etc…) • ͦͷൣғͰऩ·ΔͳΒศརͳͷͰੵۃతʹ࢖ͬͯྑ͍ Framework͸Կ࢖͏ʁ
  42. • ౷߹؅ཧͰ͖͍ͯΕ͹جຊతʹίϚϯυୟ͚ͩ͘ͰσϓϩΠͰ͖Δ͸ͣ • ී௨ʹύΠϓϥΠϯ૊ΊΔ • ػີ৘ใ΍σϓϩΠ࣌ʹల։͍ͨ͠৘ใ͸؀ڥม਺͔Β஫ೖ • FWͷεςʔδػೳ΍CFnͷ࢓૊ΈΛར༻ͯ͠؀ڥຖʹϦιʔε͕෼͚Δ • ໋໊نଇେࣄ

    • CFnͷࣗಈ໋໊ʹ೚ͤͯ࿦ཧతͳࢀর͚ͩΛ࢖༻͢Δͱָ(SAMͷ৔߹) • !Ref, !GetAtt, !Import/ExportValue CI/CD
  43. SERVERLESSͷӡ༻

  44. • ແ͍ͷ͸ʮαʔόͷӡ༻ʯ • EC2తͳαʔό • αʔόʔϓϩηεతͳαʔό • γεςϜࣗମͷӡ༻͸΋ͪΖΜ͋Δ • ΑΓຊ࣭తͳӡ༻ʹϑΥʔΧεͰ͖Δͷ͸͔֬

    ӡ༻͕ແ͍ͷ͕Sereverless??
  45. • ࣮ࡍʹ໰୊͕ى͖ͨ͜ͱΛ؂ࢹ͢Δ • Τϥʔͷൃੜ΍஗ԆΛ؂ࢹ͢Δ • API Gateway: Latency, 5XX Response

    • Lambda: Duration, Errors, Throttling, DeadLetterErrors • DynamoDB: Consumed Capacity, Throttling, Latency, Errors
 •Ұ൪େ੾ͳͷ͸ΞϓϦέʔγϣϯͷϩά ؂ࢹ
  46. • DeadLetterErrors • ͿͬͪΌ͚LambdaͷىಈError͸ى͖Δ࣌͸ͪΐ͍ͪΐ͍ى͖Δ • ࠶࣮ߦͰ্ख͘ݴͬͯΔέʔε͕΄ͱΜͲ • ࠶࣮ߦ΋ࣦഊͯ͠औΓ͜΅ͨ͠Error͸͜ΕΛݟͳ͍ͱ෼͔Βͳ͍(ཁઃఆ) • Throttling

    • SDK͸Throttlingͯ͠΋಺෦Ͱউखʹ࠶࣮ߦ͢Δ • Τϥʔ͕ى͖ͯͳ͍͔ΒThrottling͕ى͖ͯͳ͍ͱ͸ݶΒͳ͍ • ͜ΕΛݟΔͱൃੜঢ়گ͕෼͔ΔˠCapacity௥Ճ ཁ஫ҙͳ؂ࢹ߲໨
  47. • ෼ࢄͨ͠FaaS؀ڥͰ͸͍͔ʹू໿ͯ͠ޮ཰Α͘ݟΒΕΔΑ͏ʹ͢Δ͔͕େࣄ • ·ͣू໿͢Δͷ͸େલఏ • CloudWatch Logs͸ͨͩͷόοΫΞοϓ • Datadog LogsΦεεϝ

    • https://github.com/marcy-terui/ddlogs • Trace IDͷड͚౉͠ํΛܾΊͯԣ۲ͰݕࡧͰ͖ΔΑ͏ʹ͢Δ ϩά
  48. LOGS WITH TRACE ID Datadog Logsͷ࣮ࡍͷը໘Ͱ͢

  49. SERVERLESSͷݱ࣮

  50. • REST API + SPA/MobileAppͳΞϓϦέʔγϣϯ • ผʹѱ͘͸ͳ͍ • αʔόӡ༻͠ͳͯ͘ྑ͍Ҏ্ͷϝϦοτ͕ͳ͍ •

    ͦΕɺCaaS or PaaSͰΑ͘Ͷʁʁ • ී௨ʹRDB࢖͑ΔΑʁʁ • όοΫΤϯυॲཧΛ্ख͘෼ղͰ͖ͳ͍ͳΒແཧʹServerlessԽ͢Δඞཁ͸ͳ͍ • ࣮ࡍɺطଘWebΞϓϦͷServerlessԽ͸࡞Γ௚͢ؾ͕֓ͳ͚Ε͹
 ͍͍ͩͨίϯςφԽ, PaaSԽʹ଩੾Γ͢Δ͜ͱ͕΄ͱΜͲ WebΞϓϦέʔγϣϯʹผʹ޲͍ͯͳ͍
  51. SERVERLESSͷະདྷ

  52. • ͜͜Ͱݴ͏BaaS͸AppSync, FirebaseͷΑ͏ͳچདྷ͔ΒBaaSͱݺ͹Ε͍ͯͨ΋ͷ • Frontend͕΍ΓऔΓ͢Δͷ͸͋͘·ͰBaaS͚ͩ • όοΫΤϯυͰBaaSʹର͢ΔEvent DrivenͰ༷ʑͳॲཧ͕ߦΘΕΔ •ࣦ৬͢Δʘ(^o^)ʗśƂŦ •

    ͔΋͠Εͳ͍ʢΘΓͱߴ֬཰Ͱͦ͏ͳΔͱࢥͬͯΔʣ BaaSΛத৺ʹServerless͕ิ׬͢Δ
  53. SERVERLESSͷ͓·͚

  54. “CloudͰେن໛γεςϜ͸ͪΐͬͱɾɾɾ” THANKS!! “ServerlessͰେن໛γεςϜ͸ͪΐͬͱɾɾɾ” “݁ہશ෦Ͱ͖ΔΑ͏ʹͳͬͯΔ”