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

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

Masashi Terui
November 17, 2018

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

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

Masashi Terui

November 17, 2018
Tweet

More Decks by Masashi Terui

Other Decks in Technology

Transcript

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

    View full-size slide

  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

    View full-size slide

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

    View full-size slide

  4. SERVERLESSͬͯͳΜ͚ͩͬʁ
    WHAT IS

    View full-size slide

  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

    View full-size slide

  6. FaaS + Functional SaaS (BaaS) = Serverless
    https://www.slideshare.net/acloudguru/ant-stanley-being-serverless

    View full-size slide

  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)

    View full-size slide

  8. SERVERLESSͷ࣮ମΛଊ͑Δ

    View full-size slide

  9. FaaSͬͯͲΜͳ΋ͷʁ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  13. BaaSͬͯͲΜͳ΋ͷʁ

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  18. SERVERLESSͷઃܭ

    View full-size slide

  19. • ϝοηʔδϯά
    • ಉظ/ඇಉظ
    • Interactive/PubSub
    • σʔλετΞ
    • εέʔϦϯά
    • σʔλΞΫηε
    • ࣮ߦํ๏
    • ฒྻ/௚ྻ
    • ܾఆత/ඇܾఆత
    ໨తΛՌͨͨ͢Ίͷ࠷దղ͸ҟͳΔ

    View full-size slide

  20. BE EVENT DRIVEN

    View full-size slide

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

    View full-size slide

  22. • ಉظ
    • API Gateway
    • ඇಉظ
    • ௚ྻ
    • Kinesis Streams
    • ฒྻ
    • SNS
    • SQS
    • ͦͷଞ
    • Step Functions
    EVENTΛͲͷΑ͏ʹॲཧ͢΂͖͔ʁ

    View full-size slide

  23. ඇಉظɾ෼ࢄΛϕʔεʹߟ͑Δ
    Կ΋ͤͣʹ଴ͬͯΔͳΒࣗ෼(MONOLITH)Ͱ΍Ε͹ྑ͍ͷͰ͸ʁ

    View full-size slide

  24. • ͭ·ΔॴΤϥʔϋϯυϦϯά
    • ϦτϥΠՄೳͳΤϥʔ͸ϦτϥΠ͢Δ
    • ಉظͰ΍Ε͹ΫϥΠΞϯτ͕ϦτϥΠͯ͘͠ΕΔͱ͍͏ͷ͸ݬ૝
    • ϦτϥΠͰ͖ͳ͍Τϥʔ΋ҰͭͷΠϕϯτͰ͋Δ
    • ผͷܦ࿏ͰવΔ΂͖ॴʹ఻͑Δͱ͍͏͜ͱΛߟ͑Δ
    • ͦͯ͠ϞχλϦϯάɾτϨʔγϯά
    • ඞཁͳ৘ใ͸ू໿͢Δ
    • ৄ͘͠͸͋ͱͰ
    ඇಉظɾ෼ࢄʹ͓͚Δෆ҆

    View full-size slide

  25. Event Source + Handler = Microservice
    ʓʓʹରͯ͠☓☓͢Δɺશͯ͸ͦͷ૊Έ߹Θͤ

    View full-size slide

  26. • ࣮ࡍʹEvent SourceͱHandlerຖʹ؅ཧ୯ҐΛ෼͚Δ͔͸։ൃઓུґଘ
    • গਓ਺Ͱશ෦࡞ͬͯΔͷʹσϓϩΠύΠϓϥΠϯ΍GitϦϙδτϦΛ

    ෼͚Δͷ͸ΦʔόʔϔουͰ͔͠ͳ͍
    • Event SourceͱHandlerͷ૊Έ߹ΘͤͰॲཧ಺༰͕ࣗ໌͔ͭ؅ཧՄೳͳ

    ঢ়ଶΛอ͍ͬͯΕ͹ޙͰ෼͚Δͷ͸ۤ͡Όͳ͍
    • ೚ҙͷίϯςΩετͰEvent SourceΛ઀ଓ఺ʹ্ख͘੾Γग़ͤΔ
    • େࣄͳͷ͸Function͕૿͑Δ͜ͱΛԀΘͣઃܭͷෳࡶԽΛݏ͏͜ͱ
    Practices

    View full-size slide

  27. • ґଘؔ܎ͷ޲͖Λἧ͑Δ
    • ඇಉظΛॏࢹ
    • Event SourceΛू໿఺ɾ෼ذ఺ͱ͢Δ
    • ෳࡶԽͷݪҼ͸େ఍σʔλΞΫηεʹ͋Δ
    • ॻ͖ࠐΈͱࢀরؔ܎ͷෳࡶԽ
    • σʔλͷॴ༗ؔ܎
    • CQRS, Materialized View
    ෳࡶԽͤ͞ͳ͍ͨΊʹ

    View full-size slide

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

    View full-size slide

  29. ۩ମతʹ͸ “DynamoDB ދͷר” [ݕࡧ]
    http://marcy.hatenablog.com/entry/2018/07/31/213705

    View full-size slide

  30. SERVERLESSͷ࣮૷

    View full-size slide

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

    View full-size slide

  32. • Cold Start/Warn StartΛҙࣝ͢Δ
    • ίʔυ͕ಉ͡Ͱ͋Ε͹Ұ౓ىಈͨ͠ίϯςφ͸͋Δఔ౓࢒Δʢ਺ेඵ)
    • ಉ͡ίϯςφ͕࠶ར༻͞ΕΔ -> Warm Start
    • ৽͍͠ίϯςφ্ཱ͕͕ͪΔ -> Cold Start (Φʔόʔϔου͕͋Δ)
    • ෆཁͳΦϒδΣΫτΛ޿͍ScopeͰੜ੒͠ͳ͍
    • Warm Start࣌ʹ͸Global Scope΍StaticͳΦϒδΣΫτ͸࢒Δ
    • ࢖͍ճͤΔΦϒδΣΫτ͸࢖͍ճ͢(Lazy Loading, Singleton)
    • ಛʹTCPίωΫγϣϯ࣋ͬͯΔΦϒδΣΫτͱ͔͸Φʔόϔου͕σΧΠ͔Βେࣄ
    FaaSͷੑ্࣭ύϑΥʔϚϯεʹؔΘΔTips

    View full-size slide

  33. • Event Sourceͷछผຖʹॲཧํ๏ΛܾΊͯ౷Ұ͢Δ
    • ϝοηʔδͷड͚౉͠ํ
    • JSON/ProtocolBuffers, Plain/GZip etc…
    • Trace ID(ޙड़)ͳͲͷMetadataͷ࢓ࠐΈํ
    • ྫ֎ॲཧ
    • ແࢹʢϩάʹ࢒͢ʣͯ͠ྑ͍΋ͷ
    • ൃੜͨ࣌͠఺Ͱผϧʔτʹྲྀ͢΂͖΋ͷ
    • Failͤͯ͞ϦτϥΠػߏʹ৐ͤΔ΋ͷ
    • PythonͩͱDecoratorΛ࢖͏ͱศར
    EventॲཧͷϙΠϯτ

    View full-size slide

  34. DECORATOR
    Trace IDΛϔομͰ΍ΓऔΓ
    EventͱResult͸ඞͣϩάʹ࢒͢
    ͱ͍͏࠷௿ݶͳαϯϓϧ

    View full-size slide

  35. SERVERLESSͷςετ

    View full-size slide

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

    View full-size slide

  37. • جຊతʹ࣮ࡍͷAWS αʔϏεΛ࢖ͬͯ΍Δ
    • ౷߹؅ཧͰ͖ΔFWΛબͿ(AWS SAM, Serverless etc…)
    • ͦͷ··ಉ͡؀ڥ͕σϓϩΠͰ͖Δ
    • ςετ͍ͨ͠ൣғΛܾΊɺऴ୺ͷEvent SourceͱHandlerΛஔ͖׵͑Δ
    • ؀ڥม਺ͰEvent SourceΛ੾Γସ͑
    • VerifyՄೳͳܗͰOutputͤ͞Δ
    Integration Test

    View full-size slide

  38. PRODUCTION SSM Parameter Store΍CFn Expot/Import ValueͰ֎෦ࢀরՄೳͱ͓ͯ͘͠
    ؀ڥม਺͔ΒEndpointΛ஫ೖ͢ΔΑ͏ʹ࣮૷

    View full-size slide

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

    View full-size slide

  40. SERVERLESSͷσϓϩΠ

    View full-size slide

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

    CFnϕʔεͷFW͡Όͳ͍ͱ࢖͑ͳ͍ςΫχοΫ
    • ൣғ͕ݶఆతͳ΋ͷ͸ͦͷൣғͰऩ·Δ৔߹ʹͷΈ(Chalice, Apex etc…)
    • ͦͷൣғͰऩ·ΔͳΒศརͳͷͰੵۃతʹ࢖ͬͯྑ͍
    Framework͸Կ࢖͏ʁ

    View full-size slide

  42. • ౷߹؅ཧͰ͖͍ͯΕ͹جຊతʹίϚϯυୟ͚ͩ͘ͰσϓϩΠͰ͖Δ͸ͣ
    • ී௨ʹύΠϓϥΠϯ૊ΊΔ
    • ػີ৘ใ΍σϓϩΠ࣌ʹల։͍ͨ͠৘ใ͸؀ڥม਺͔Β஫ೖ
    • FWͷεςʔδػೳ΍CFnͷ࢓૊ΈΛར༻ͯ͠؀ڥຖʹϦιʔε͕෼͚Δ
    • ໋໊نଇେࣄ
    • CFnͷࣗಈ໋໊ʹ೚ͤͯ࿦ཧతͳࢀর͚ͩΛ࢖༻͢Δͱָ(SAMͷ৔߹)
    • !Ref, !GetAtt, !Import/ExportValue
    CI/CD

    View full-size slide

  43. SERVERLESSͷӡ༻

    View full-size slide

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

    View full-size slide

  45. • ࣮ࡍʹ໰୊͕ى͖ͨ͜ͱΛ؂ࢹ͢Δ
    • Τϥʔͷൃੜ΍஗ԆΛ؂ࢹ͢Δ
    • API Gateway: Latency, 5XX Response
    • Lambda: Duration, Errors, Throttling, DeadLetterErrors
    • DynamoDB: Consumed Capacity, Throttling, Latency, Errors

    •Ұ൪େ੾ͳͷ͸ΞϓϦέʔγϣϯͷϩά
    ؂ࢹ

    View full-size slide

  46. • DeadLetterErrors
    • ͿͬͪΌ͚LambdaͷىಈError͸ى͖Δ࣌͸ͪΐ͍ͪΐ͍ى͖Δ
    • ࠶࣮ߦͰ্ख͘ݴͬͯΔέʔε͕΄ͱΜͲ
    • ࠶࣮ߦ΋ࣦഊͯ͠औΓ͜΅ͨ͠Error͸͜ΕΛݟͳ͍ͱ෼͔Βͳ͍(ཁઃఆ)
    • Throttling
    • SDK͸Throttlingͯ͠΋಺෦Ͱউखʹ࠶࣮ߦ͢Δ
    • Τϥʔ͕ى͖ͯͳ͍͔ΒThrottling͕ى͖ͯͳ͍ͱ͸ݶΒͳ͍
    • ͜ΕΛݟΔͱൃੜঢ়گ͕෼͔ΔˠCapacity௥Ճ
    ཁ஫ҙͳ؂ࢹ߲໨

    View full-size slide

  47. • ෼ࢄͨ͠FaaS؀ڥͰ͸͍͔ʹू໿ͯ͠ޮ཰Α͘ݟΒΕΔΑ͏ʹ͢Δ͔͕େࣄ
    • ·ͣू໿͢Δͷ͸େલఏ
    • CloudWatch Logs͸ͨͩͷόοΫΞοϓ
    • Datadog LogsΦεεϝ
    • https://github.com/marcy-terui/ddlogs
    • Trace IDͷड͚౉͠ํΛܾΊͯԣ۲ͰݕࡧͰ͖ΔΑ͏ʹ͢Δ
    ϩά

    View full-size slide

  48. LOGS WITH TRACE ID Datadog Logsͷ࣮ࡍͷը໘Ͱ͢

    View full-size slide

  49. SERVERLESSͷݱ࣮

    View full-size slide

  50. • REST API + SPA/MobileAppͳΞϓϦέʔγϣϯ
    • ผʹѱ͘͸ͳ͍
    • αʔόӡ༻͠ͳͯ͘ྑ͍Ҏ্ͷϝϦοτ͕ͳ͍
    • ͦΕɺCaaS or PaaSͰΑ͘Ͷʁʁ
    • ී௨ʹRDB࢖͑ΔΑʁʁ
    • όοΫΤϯυॲཧΛ্ख͘෼ղͰ͖ͳ͍ͳΒແཧʹServerlessԽ͢Δඞཁ͸ͳ͍
    • ࣮ࡍɺطଘWebΞϓϦͷServerlessԽ͸࡞Γ௚͢ؾ͕֓ͳ͚Ε͹

    ͍͍ͩͨίϯςφԽ, PaaSԽʹ଩੾Γ͢Δ͜ͱ͕΄ͱΜͲ
    WebΞϓϦέʔγϣϯʹผʹ޲͍ͯͳ͍

    View full-size slide

  51. SERVERLESSͷະདྷ

    View full-size slide

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

    View full-size slide

  53. SERVERLESSͷ͓·͚

    View full-size slide

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

    View full-size slide