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

AWSで始めるServerlessの勘所と始め方

ryurock
December 20, 2019

 AWSで始めるServerlessの勘所と始め方

Serverless化を初めて最初の一歩でこれ知っておけば良かった。
ってのをまとめてみました。

ryurock

December 20, 2019
Tweet

More Decks by ryurock

Other Decks in Technology

Transcript

  1. ΞδΣϯμ  4FSWFSMFTTͱ͸ʁ  4FSWFSMFTTͷྫ  4FSWFSMFTTΛ͢Δʹ͋ͨͬͯߟ͑ͳͯ͘͸͍͚ͳ͍ϨΠϠʔ  4FSWFSMFTTͷཧ૝ͱݱ࣮ 

    ॳΊͯͷ4FSWFSMFTTͷϙΠϯτ  4FSWFSMFTTͷ࠷ॳͷҰา-BNCEB'VODUJPOT  -BNCEB'VODUJPOT  8IZ-BNCEB'VODUJPOT   -BNCEB'VODUJPOTΛ࢖͏લʹ஌͓ͬͯ͘΂͖ࣄ -BNCEB'VODUJPOTߏ੒ύλʔϯ -BNCEB'VODUJPOTΛ࢖͏ʹ͋ͨͬͯͷਆπʔϧ 4FSWFSMFTTͷ࠷ॳͷೋาΊ&$4'BSHBUF &$4'BSHBUF 'BSHBUFͰ͓֮͑ͯ͘ࣄ 'BSHBUFͷ஫ҙࣄ߲ 'BSHBUFͷ࠷ॳͷҰา 4FSWFSMFTTͷར఺ %PDLFS͸ࢮͳͳ͔ͬͨ ·ͱΊ
  2. ServerlessΛ͢Δʹ͋ͨͬͯߟ͑ͳͯ͘͸͍͚ͳ͍ϨΠϠʔ લड़ͨ͠Storage୯ମͰద༻ͷ৔߹ڞʹεϧʔϓοτڐ༰͕௿͍ͷ ͰWEBαʔϏε΍େ༰ྔόονͷ৔߹͸୯ମͰ͸࢖༻ͮ͠Β͍ͷ ͰσʔλΛసૹ͢ΔαʔϏεΛதؒʹஔ͘ඞཁ͕͋Δ Transform Layer • Kinesis FireHose •

    ϦΞϧλΠϜ޲͚ϚωʔδυFluentdͱ͓΅͓͍͑ͯͯେମେৎ෉ • Firelens • ίϯςφͷσʔλΛసૹ͢ΔͨΊͷSideCar Container • Re:event2019ͰFargateͷσϑΥϧτϩάʹ௥Ճ͞Εͨ • AWS͕ఏڙ͢ΔAWS޲͚ʹ࠷దԽͨ͠Fluentd
  3. ServerlessΛ͢Δʹ͋ͨͬͯߟ͑ͳͯ͘͸͍͚ͳ͍ϨΠϠʔ େ͖͘෼͚ͯ * Batch౳Ͱ࣮ߦ͢Δpullܗࣜ * σʔλ͕อଘ͞ΕͨλΠϛϯάͰىಈͤ͞ΔEventυϦϒϯܗࣜ * Ұൠతͳઆ໌Ͱ͸ͳ͍ͱࢥ͏͚Ͳ΋ELBͷHTTP Request͔ΒͷEvent͕౉͍ͬͯΔͱߟ͑ͯྑ͍ Event

    Layer • CloudWatch Events • ϦΞϧλΠϜ޲͚ϚωʔδυFluentdͱ͓΅͓͍͑ͯͯେମେৎ෉ • ELB • HTTP Request͔ΒLambda΍ίϯςφʹΠϕϯτΛ౉͢ • SNS • TopicsΛൃߦ͢Δ͜ͱʹΑͬͯLambda΍SQSΛൃߦͰ͖Δ
  4. Why Lambda Functions? • Ұ൪ϑϧϚωʔδυServerless • ࿈ܞॲཧ(S3,CloudWatch Logs,SQS౳)͕ ஈʑͱ૿͍͑ͯͯ࢖͍উख͕Α͍ •

    খ࢝͘͞ΊΔࣄ͕Ͱ͖Δ • eg. S3ʹImage͕Ξοϓϩʔυ͞ΕͨΒ τϦϛϯάͱ͔ • Lambda Layerͷग़ݱʹΑͬͯNative module΋࢖͍΍͘͢ͳͬͨ
  5. Lambda Functionsߏ੒ύλʔϯ(ྫ) • APIGateway + Lambda • ͜ͷ৔߹͸VPC on Lambdaʹ͓ͯ͘͠ͱRDSͱૄ

    ௨Ͱ͖Δ(HTTP͸API Gateway͕ݞ୅ΘΓͯ͘͠ ΕΔͷͰ) • ELB + Lambda • ࠷ۙͰ͖ΔΑ͏ʹͳͬͨ(API Gatewayͱಉ͡ߟ͑ ํͰOK) • API ServerҎ֎Ͱͳʹ͔͍ͨ͠৔߹ʹߟ͑Δ • CloudWatch logs or S3 + Lambda • ΠϕϯτυϦϒϯͰ࢖͑Δ୅දྫ
  6. FargateͰ͓֮͑ͯ͘ࣄ • ECR (Elastic Container Registory) • DockerImageͷอଘઌ • DockerImageͷgithubͱ͓΅͑ͯ΋Βͬͯ΄ͱΜͲ໰୊ͳ͍

    • ECS Cluster • ContainerΛ·ͱΊΔശ • Clusterࣗମ͸ContainerΛάϧʔϓԽͯ͠؅ཧ͍ͯ͠Δ͚ͩ • ECS TaskDifinitions • DockerImageΛͲ͏ಈ͔͔͢ʁ(serverΛىಈ͢Δͱ͔ɻಛఆͷόο νίϚϯυ࣮ߦ͢Δͱ͔) • Ͳͷ͘Β͍ͷMemory,CPUΛ࢖ͬͯTaskΛಈ͔͔͢ʁ • ౳ఆٛ͢Δ΋ͷ • ͜Ε΋rivisionͷ֓೦Λ΋͍ͬͯΔͷͰTaskఆٛͷgitͱ֮͑Ε͹ྑ͍
  7. FargateͰ͓֮͑ͯ͘ࣄ(ͦͷ2) • ECS (Task) • TaskΛಈ͔͢(୯ମͷόονͳΒ͜ΕͰ໰୊ ͳ͍) • ECS (Service)

    • TaskΛىಈͬ͠ͺͳ͠ʹ͢ΔͳΒͪ͜Β (HTTP Server౳) • CloudWatch Event • cronͷΑ͏ʹఆظ࣮ߦͰTaskΛىಈͤͨ͞ ͍৔߹͸࿈ܞͤ͞Δ
  8. Fargateͷ஫ҙࣄ߲ • SSH Ͱ͖ͳ͍ • ͕ཪٕϞʔυͰssm-agentΛΠϯετʔϧ͢Ε͹ System ManagerͷSession Manager͔ΒೖΕΔ •

    ίϯςφ͕ࢮΜͩ࣌͸ίϯςφ಺ͷStorage͸શͯͳ͘ ͳΔ • logσʔλ΍Fileอଘͷ΋ͷ͸Storage Serviceʹసૹ ͠ͳ͍ͱࣦΘΕΔ(S3,RDS,Cloudwatch log౳) • ϩά͸શͯstdoutͰcloudwatch logʹอଘ͞ΕΔ • firelensͱ͍͏fluentίϯςφʹసૹ͢Δ͜ͱ΋Ͱ͖ Δ
  9. Serverlessͷར఺ • ؅ཧ͢Δ΋ͷ͕ݮΔ • Ansible,PackerશࣺͯͯΕΔ • CloudFormationͱDockerImageҎ্!ʹͰ͖Δ • όονͳΒ͹CPUͱ͔΋ؾʹ͢Δඞཁ͸ͳ͍ •

    όον͕ճΓ੾Δ͚ͩͷϝϞϦͱCPUΛઃఆ͢Ε͹Α ͍ • εύΠΫͷΞϥʔϜ͸͖ʹ͠ͳͯ͘ྑ͍ • (RDSͱ͔ͷߟྀ͸ඞཁ͚ͩͲ΋ • Aurora RDSʹ͢Ε͹Φʔτεέʔϧ͢Δ͔Β͔ ͳΓ࠷ڧ(͚ͩͲ΋͓ۚ͸͔͔Δ)