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

aws-lambda-in-practice

tannai
February 20, 2016

 aws-lambda-in-practice

tannai

February 20, 2016
Tweet

More Decks by tannai

Other Decks in Technology

Transcript

  1. %FWFMPQFST*0 -B ୮಺༏لϞόΠϧΞϓϦαʔϏε෦ Ϋϥεϝιουגࣜձࣾ ⡥$MBTTNFUIPE *OD ೥݄೔ ࣮຿Ͱ࢖͏"84-BNCEB  #cmdevio2016

    #La
  2. ࣗݾ঺հ • ୮಺༏ل • Ϋϥεϝιουגࣜձࣾ • ϞόΠϧΞϓϦαʔϏε෦ • αʔόαΠυ୲౰ 2

    #cmdevio2016 #La
  3. ϞόΠϧόοΫΤϯυάϧʔϓ • AWSͱiOS/Androidͷهࣄ͕ଟ͍Classmethod • ࢢ৔ͷมԽʹରԠͯ͠αʔόαΠυͷ։ൃ΋ ڧԽ • AWS্Ͱಈ͘APIαʔό΍WebΞϓϦͷ։ൃ • ্ཱͪ͛ظʹ͖ͭืूதʂ

    3 #cmdevio2016 #La
  4. ຊ୊ 4

  5. Agenda • AWS Lambdaͱ࣮຿ • ࣮຿ͰͷϢʔεέʔε • LambdaͷTips • ࣮຿ʹ૊ΈࠐΉצॴ

    5
  6. AWS Lambdaͱ͸ • https://aws.amazon.com/jp/lambda/faqs/ • ొ࿥ͨ͠ϓϩάϥϜΛ࣮ߦͯ͘͠ΕΔ
 ϑϧϚωʔδυαʔϏε • EC2ͷΑ͏ͳ؅ཧ͢Βෆཁ •

    ϑϧϚωʔδυ • AWSͱͷ࿈ܞ͕ڧྗ • Πϕϯτιʔε 6 #cmdevio2016 #La
  7. ࣮຿ͱ͸ • ࣮ࡍͷ۩ମతͳ࢓ࣄ • https://kotobank.jp/word/࣮຿-521746 • ʮ੹຿ΛՌ͓ͨͯۚ͠Λ΋Β͏ʯ • ظݶ/είʔϓ/༧ࢉͷ͍͔͕ܾͭ͘·͍ͬͯΔ 7

    #cmdevio2016 #La
  8. ͪΐͬͱͰ΋LambdaΛ ࢖ͬͨ͜ͱ͕͋Δਓ 8

  9. ࣮຿ͰLambdaΛ ࢖͍ͬͯΔਓ 9

  10. ࣮຿ͰLambdaΛ ͕ͬͭΓ࢖͍ͬͯΔਓ 10

  11. ࣮຿ͰLambdaΛ࢖͏ͷ͸೉͍͠ʁ • Lambda͕ྑͦ͞͏ͳ͜ͱ͸෼͔Δ • EC2Ͱ͸ΦϯϓϨͱಉ͡Α͏ʹϓϩάϥϜΛ ࣮ߦ͢Δ͜ͱ͕Ͱ͖ͨ • Lambdaͷ࢖͍ํ͸ɺͦΕͱ͸ҟͳΔ • ೲظ͕ܾ·͍ͬͯΔͷͰԼखʹϋϚΔͱਏ͍

    • ӡ༻࣌ʹ໰୊͕ग़ΔͱࠔΔ 11 #cmdevio2016 #La
  12. ͜ͷηογϣϯͷΰʔϧ • LambdaΛ࣮຿Ͱ࢖͏͖͔͚ͬʹͳΔ • ࣮຿ʹແཧͳ͘औΓೖΕΒΕΔઃܭͷ
 ͖͔͚ͬΛಘΔ 12 #cmdevio2016 #La

  13. Agenda • AWS Lambdaͱ࣮຿ • ࣮຿ͰͷϢʔεέʔε • LambdaͷTips • ࣮຿ʹ૊ΈࠐΉצॴ

    13
  14. S3ϑΝΠϧૢ࡞ 14

  15. S3ϑΝΠϧૢ࡞ • ϝλσʔλ෇༩ • αϜωΠϧ࡞੒ 15 #cmdevio2016 #La

  16. CSVͷDynamoDBςʔϒϧԽ 16

  17. CSVͷDynamoDBςʔϒϧԽ • S3ʹΞοϓϩʔυ͞ΕͨCSVϑΝΠϧΛ DynamoDBςʔϒϧʹॻ͖ࠐΉFunction • FunctionΛ࣮ߦ͢ΔFunctionͰಉ࣮࣌ߦ਺Λ
 ੍ޚ 17 #cmdevio2016 #La

  18. CSVͷDynamoDBςʔϒϧԽ 18 #cmdevio2016 #La

  19. CSVͷDynamoDBςʔϒϧԽ • ΋ͱ͸ΞϓϦͷػೳͱͯ͠ߟ͍͑ͯͨ • ࣌ؒͷ͔͔ΔॲཧͩͬͨͷͰLambdaԽ • workerͰ΋ྑ͔͕ͬͨLambdaͷํ͕
 ӡ༻͠ͳͯ͘ྑ͍ͿΜָ 19 #cmdevio2016

    #La
  20. ϞόΠϧΞϓϦ͔ΒݺͿAPI 20

  21. ϞόΠϧΞϓϦ͔ΒݺͿAPI • APIαʔό͕୲͍ͬͯͨॲཧΛLambdaͰߦ͏ • DynamoDBʹ௚઀ॻ͖ࠐΉΑΓ΋༻్͕޿͍ • ϩδοΫΛޙ͔ΒมߋͰ͖Δ • ηΩϡϦςΟ্ͪΐͬͱ҆৺ 21

    #cmdevio2016 #La
  22. ϞόΠϧΞϓϦ͔ΒݺͿAPI 22 #cmdevio2016 #La

  23. ϞόΠϧΞϓϦ͔ΒݺͿAPI 23 #cmdevio2016 #La

  24. ϞόΠϧΞϓϦ͔ΒݺͿAPI • ݁ہɺཁ͕݅ᐆດͳஈ֊Ͱ͸Lambda࠾༻Λ
 ܾஅͰ͖ͳ͔ͬͨ • APIαʔόΛNode.jsͰॻ͍ͯਐΊΔ • ཁ͕݅ݻ·ͬͨΓɺLambdaͷػೳ֦ு΍
 ΞΫηε਺͕ݟ௨ͤͨΒLambdaʹ͢Δ
 ͔΋͠Εͳ͍

    24 #cmdevio2016 #La
  25. σʔλͷऩू 25

  26. σʔλͷऩू • KinesisʹLambdaͰσʔλΛS3ʹஷΊΔ • ஷΊͨσʔλ͸όονͰRDS΍Redshift΁ • PullܕͰىಈ͢ΔͷͰϨίʔυ؅ཧ͕ෆཁ • Lambdaͷػೳ֦ுͱڞʹมԽ 26

    #cmdevio2016 #La
  27. σʔλͷऩू 27 Firehoseొ৔લ #cmdevio2016 #La

  28. σʔλͷऩू 28 Firehoseొ৔ޙ #cmdevio2016 #La

  29. CloudFormation 29

  30. CloudFormation • https://docs.aws.amazon.com/ja_jp/AWSCloudFormation/latest/UserGuide/ template-custom-resources-lambda.html 30 #cmdevio2016 #La

  31. Agenda • AWS Lambdaͱ࣮຿ • ࣮຿ͰͷϢʔεέʔε • LambdaͷTips • ࣮຿ʹ૊ΈࠐΉצॴ

    31
  32. όʔδϣχϯάͱΤΠϦΞε 32

  33. όʔδϣχϯάͱΤΠϦΞε • όʔδϣϯ • ಉ͡FunctionͰ΋൛͝ͱʹมΘΔARN • ΤΠϦΞε • ඥͮ͘όʔδϣϯΛมߋͰ͖ΔARN •

    ͜ΕΒΛ࢖ͬͯLambda FunctionΛ؅ཧ͢Δ 33 #cmdevio2016 #La
  34. όʔδϣχϯάͱΤΠϦΞε 34 #cmdevio2016 #La

  35. σϓϩΠ 35

  36. σϓϩΠ • ϑΝΠϧΛzipͨ͠ΞϓϦέʔγϣϯόϯυϧ Λ࡞ͬͯΞοϓϩʔυ • Functionຊମ • ґଘϥΠϒϥϦ • aws-sdk΍aws-cliΛϥοϓ͢Δͱྑ͍

    36 #cmdevio2016 #La
  37. σϓϩΠ 37 σϓϩΠύοέʔδʹؚΊΔϥΠϒϥϦͷऔಘ #cmdevio2016 #La

  38. σϓϩΠ 38 σϓϩΠύοέʔδͷΞοϓϩʔυ #cmdevio2016 #La

  39. σϓϩΠ • ϚωδϝϯτίϯιʔϧͰΤΠϦΞεͷ੾Γ ସ͑ 39 #cmdevio2016 #La

  40. ؂ࢹͱϦΧόϦ 40

  41. ؂ࢹͱϦΧόϦ • CloudWatchͰLambdaϝτϦΫεΛݟ͓ͯ͘ • ࣮ߦ݁Ռ͔Βҟৗ͕Θ͔Δͱϕετ • ҟৗ͕͋ͬͨΒϦτϥΠͰ͖ΔΑ͏ʹͯ͠
 ͓͘ 41 #cmdevio2016

    #La
  42. ؂ࢹͱϦΧόϦ • Lambdaࣗମ͕མͪͨ͜ͱ͸ແ͍ • ↑ݸਓͷײ૝Ͱ͢ • ΞϓϦಉ༷ɺFunction಺ͷόά͔Βͷෆ۩߹ • ྫ֎Λर͑ͯͳ͔ͬͨɺͳͲ •

    ࣮ߦ݁Ռ͔Βෆ۩߹Λݕ஌ • ࣄલʹܾΊͨ௨ΓͷϦτϥΠͰରԠ 42 #cmdevio2016 #La
  43. Ϧϛοτ 43

  44. Ϧϛοτ • http://docs.aws.amazon.com/ja_jp/lambda/ latest/dg/limits.html • ࣮༻্͸ϝϞϦͱಉ࣮࣌ߦ਺ͱαΠζʹ஫ҙ • ಉظݺͼग़͠͸ௐ੔͞ΕΔͱΤϥʔ͕ฦΔ 44 #cmdevio2016

    #La
  45. LambdaόοΫΤϯυͷฒྻ଱ੑ 45

  46. LambdaόοΫΤϯυͷฒྻ଱ੑ • Function͸ɺฒྻͤ͞ΒΕΔΑ͏ʹॻ͘લఏ • Lambdaࣗମ͸ྑ͍͕ɺόοΫΤϯυ͕ͦΕʹ ଱͑ΒΕͳ͍Մೳੑ͕͋Δ 46 #cmdevio2016 #La

  47. LambdaόοΫΤϯυͷฒྻ଱ੑ • ରࡦ1: εέʔϥϒϧͳόοΫΤϯυΛ࢖͏ • DynamoDB • ରࡦ2: ฒྻ౓Λ੍ޚ͢Δ࢓૊ΈΛೖΕΔ •

    FunctionΛىಈ͢ΔFunctionΛઃ͚Δ 47 #cmdevio2016 #La
  48. Lambdaͷඅ༻ 48

  49. Lambdaͷඅ༻ • Lambdaࣗମ͸֨҆ͩ͠ɺແྉ࿮͕େ͖͍ • AWSτʔλϧͰͷࢼࢉΛ͓ͯ͘͠΂͖ • ࣮ࡍͷෛՙΛ͔͚ͯྉۚͷ্͕ΓํΛݟΔ 49 #cmdevio2016 #La

  50. Lambdaͷඅ༻ • CloudWatch Logsͷ৔߹ • Function಺Ͱඪ४ग़ྗͨ͠಺༰͕Logsʹ
 อଘ͞Εͯɺͦ͜Ͱͷ՝͕ۚൃੜ͢Δ • DynamoDBͷ৔߹ •

    ΩϟύγςΟ௒͑Λ๷͙৔߹͸ଟΊʹઃఆ ͢Δඞཁ͕͋Γɺͦ͜Ͱ՝͕ۚൃੜ͢Δ 50 #cmdevio2016 #La
  51. Agenda • AWS Lambdaͱ࣮຿ • ࣮຿ͰͷϢʔεέʔε • LambdaͷTips • ࣮຿ʹ૊ΈࠐΉצॴ

    51
  52. ͲͷॲཧΛLambdaʹ͢Δ͔ 52

  53. Lambdaʹ͢Δॲཧ • ϏδωεϩδοΫҎ֎Λ୅ସ͢Δ৔߹ • ϏδωεϩδοΫΛ୅ସ͢Δ৔߹ • ӡ༻ิॿͰ࢖͏৔߹ • ఘΊΔ৔߹ 53

    #cmdevio2016 #La
  54. ϏδωεϩδοΫҎ֎Λ୅ସ͢Δ৔߹ • αϜωΠϧ࡞੒ɺϑΥʔϚοτม׵ͳͲ • ΞϓϦέʔγϣϯͷҡ࣋ʹෆՄܽͳػೳ • LambdaԽ͠΍͍͢ॲཧ • LambdaԽͷԸܙΛड͚΍͍͢ 54

    #cmdevio2016 #La
  55. ϏδωεϩδοΫΛ୅ସ͢Δ৔߹ • ैདྷAPIαʔό͕ߦ͍ͬͯͨॲཧΛ୅ସ͢Δ • Ұ൪ߴίετͳ෦෼͕ղܾ͢Δ • ཁ݅ʹΑͬͯ͸೉͔ͬͨ͠Γ͢Δ • Cognito΍APIGWΛซ༻͢Δ 55

    #cmdevio2016 #La
  56. ӡ༻ิॿͰ࢖͏৔߹ • CloudFormationͷΧελϜϦιʔεɺcronͷ ୅ସͳͲ • ෆ۩߹࣌΋ൺֱతΧόʔ͠΍͍͢ͷͰಋೖ͠ ΍͍͔͢΋͠Εͳ͍ 56 #cmdevio2016 #La

  57. ఘΊΔ৔߹ • ࣮຿Ͱ͸Ϗδωεͷ੒ޭ͕༏ઌ͞ΕΔ͜ͱ͕ଟ͍ • ແཧʹ࢖͓͏ͱ͠ͳ͍ • Lambdaʹͩ͜ΘͬͯϏδωε͕ࣦഊͨ͠Β • ຊ຤స౗ •

    ఫୀ͢Δͱ͖ʹLambdaԽͷ༨஍Λ࢒͢ • LambdaରԠݴޠͰബ͍APIαʔό • όοΫΤϯυʹDynamoDB 57 #cmdevio2016 #La
  58. ͲͷॲཧΛLambdaԽ͢Δ͔ • ӡ༻ิॿ͔ΒॳΊͯɺϏδωεϩδοΫҎ֎ ͷ୅ସΛ໨ࢦ͢ͷ͕ྑͦ͞͏ • ΞϓϦͷ୅ସͱͯ͠࢖͏ͷ͸ෑډ͕ߴ͍ • Ϗδωε΋ߟྀ͢Δͱঘྑ͠ 58 #cmdevio2016

    #La
  59. ෆ҆ʹඋ͑Δ 59

  60. ෆ҆ʹඋ͑Δ • ࣦഊΛϋϯυϦϯάͰ͖Δͱ͜ΖͰߦ͏ • ӡ༻ٕज़ͷηΧϯμϦ • ࠷ॳ͸Lambdaͷ୅ସखஈΛߟ͓͑ͯ͘ • ؂ࢹͱτϥϒϧ࣌ͷ෮چखஈΛ๨Εͳ͍ 60

    #cmdevio2016 #La
  61. 61 44 44 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ classmethod.jp

  62. 62 45 45 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ ͋Β͔͡ΊσʔλςʔϒϧΛ࡞੒͠ [email protected]

    OBNF TDPSF EBUB XSJUFશৼΓ classmethod.jp
  63. 63 46 46 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ ॲཧྔ $47ϑΝΠϧͷߦ਺

    ΛಡΈࠐΈ [email protected] OBNF TDPSF EBUB XSJUFશৼΓ classmethod.jp
  64. 64 47 47 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF ؅ཧςʔϒϧʹ ૯਺Λొ࿥͓ͯ͘͠ EBUB XSJUFશৼΓ classmethod.jp
  65. 65 48 48 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE  σʔλΛ֨ೲͨ͠Β EBUB XSJUFશৼΓ classmethod.jp
  66. 66 49 49 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE  Χ΢ϯλΛՃࢉ ΞτϛοΫΧ΢ϯλ EBUB XSJUFશৼΓ classmethod.jp
  67. 67 50 50 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE   FGHI  σʔλΛ֨ೲͨ͠Β EBUB XSJUFશৼΓ classmethod.jp
  68. 68 51 51 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE   FGHI  Χ΢ϯλΛՃࢉ ΞτϛοΫΧ΢ϯλ EBUB XSJUFશৼΓ classmethod.jp
  69. 69 52 52 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE   FGHI  શͯͷ੒ޭΛ֬ೝͨ͠Β EBUB XSJUFશৼΓ  classmethod.jp
  70. 70 53 53 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE   FGHI  SFBEશৼΓʹ͢Δ EBUB SFBEશৼΓ classmethod.jp
  71. 71 55 55 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE  ΋ࣦ͠ഊͨ͠Β EBUB XSJUFશৼΓ '"*-&% 9 classmethod.jp
  72. 72 56 56 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     [email protected] OBNF TDPSF  BCDE  ਺͸Ұக͠ͳ͍ͷͰ EBUB XSJUFશৼΓ 㱠 '"*-&% classmethod.jp
  73. 73 57 57 ؅ཧςʔϒϧ σʔλςʔϒϧ ςʔϒϧ໊ ૯਺ ࣮੷ EBUB 

     EBUB   શͯ࡞Γ௚͢ [email protected] OBNF TDPSF EBUB XSJUFશৼΓ classmethod.jp
  74. มԽʹඋ͑Δ 74

  75. Lambdaػೳ௥Ճͷྺ࢙ • 2015/04/09 Ұൠެ։ • 2015/05/28 σϓϩΠύοέʔδͷS3ରԠ • 2015/06/15 JavaରԠ

    • 2015/07/09 APIGWରԠ • 2015/07/14 DynamoDB StreamsҰൠެ։ • 2015/10/08 Pythonɺόʔδϣϯɺ5෼ɺcron • 2016/02/11 VPCରԠ 75 http://aws.amazon.com/releasenotes/AWS-Lambda?browse=1 #cmdevio2016 #La
  76. มԽʹඋ͑Δ • Lambdaͷػೳ௥Ճεϐʔυ͸଎͍ • ৽ػೳͰطଘΞϓϦΛLambdaʹ
 ੾Γग़ͨ͠ΓɺLambdaΛݮΒ͠ଓ͚Δ • ΞϓϦຊମͷςετ΍ྑ͍ઃܭΛҡ࣋͢Δ 76 #cmdevio2016

    #La
  77. 77 Before After #cmdevio2016 #La

  78. ·ͱΊ 78

  79. ·ͱΊ • ͱΓ࣮͋͑ͣ຿ʹऔΓೖΕͯΈΑ͏ • ϏδωεΛՃ଎ͤ͞Δ࢖͍ํΛ͠Α͏ • ͜ͷձ৔Ͱ૬ஊ͠Α͏ • 3FΞʔΩςΫςΟϯά •

    3F͓೰Έ૬ஊࣨ • ࠙਌ձ 79 #cmdevio2016 #La
  80. 80

  81. None