個人開発者のためのaws-serverless-express #kyotojs

個人開発者のためのaws-serverless-express #kyotojs

HANATANI Takuma(@potato4d) 2018.03.23
#kyotojs

A54b31d4ebbce222dff88a5c42bac033?s=128

Potato4d(Hanatani Takuma)

March 24, 2018
Tweet

Transcript

  1. ݸਓ։ൃऀͷͨΊͷ aws-serverless-express HANATANI Takuma(@potato4d) 2018.03.23 #kyotojs

  2. Ξ • ࣗݾ঺հ • ݸਓ։ൃʹ͓͚ΔΠϯϑϥෛ୲ͷਏ͞ • PaaSʹΑΔղܾ • aws-serverless-expressͷྑ͞ •

    ݸਓ։ൃऀʹͱͬͯͷNode.jsͷΠϯϑϥͷͭΒ͞ • aws-serverless-expressͷ͸͡Ί͔ͨ
  3. ࣗݾ঺հ

  4. Ί͙ΈΜ ੠ - ಺ాਅྱ / ߴڮཥґ ߚຐ଒ͷগঁɻ13ࡀˠ14ࡀ ʢWEB൛Ͱ͸17ࡀલޙʣɻεϐϯΦϑʮ͜ͷૉ੖Β͍͠ ੈքʹരԍΛʂ ʯͷओਓެɻ

    ࠇ൅ʹߚ͍໨ɻࠇϚϯτ ʹࠇϩʔϒɺτϯΨϦ๧ࢠΛඃΓɺࠨ໨ʹ؟ଳʢผʹ໨ ͕ѱ͍Θ͚Ͱ͸ͳ͘ɺͨͩͷϑΝογϣϯʣΛ͚͍ͭͯ ΔɻΧζϚͱΞΫΞͷग़ͨ͠ύʔςΟʔϝϯόʔืूͷ ுΓࢴΛݟͯ஥ؒʹՃΘΔɻମ֨͸খฑͰࡉ਎ɺ਎௕͸ গͳ͘ͱ΋ΧζϚ(165cm)ΑΓ௿͍ɻ԰ෑͰ͸੺ͷύ IUUQTCJUMZ;#'.;P
  5. @potato4d • ౦ژͰϑϦʔϥϯε΍ͬͯΔ • ͍͍ͩͨϑϩϯτΤϯυ • ࠷ۙ͸ຆͲNuxtͷ։ൃͱ͔ϑϩϯτٕज़૬ஊ • Vue.js JP

    / VueFes Staff / Vue.js translator • ͜ͷؒٱʑʹNode.jsΛੜͰॻ͍ͨΒdeep copyͷͨ Ίʹobject-rest-spreadΛ࢖ͬͯCIΛམͱͨ͠ਓ
  6. ຊ୊

  7. ͍͖ͳΓͰ͕͢օ͞Μʂ

  8. ݸਓ։ൃͯ͠·͔͢ʁ

  9. WebApp։ൃͯ͠·͔͢ʁ

  10. ݸਓ։ൃͷʮΠϯϑϥʯ ͭΒ͘ͳ͍Ͱ͔͢ʁ

  11. ʮϝϯςφϯεʯʹ ৽͍͜͠ͱΛ΍Δ࣌ؒΛ ୣΘΕ͍ͯ·ͤΜ͔ʁ

  12. ຊ࣭తͰͳ͍࡞ۀͰ ϞνϕΛԼ͍͛ͯ·ͤΜ͔ʁ

  13. ྫ͑͹ʮ͜Μͳ͜ͱʯ ىͬͯ͜ͳ͍Ͱ͔͢ʁ

  14. ݸਓ։ൃͷΠϯϑϥϝϯςͷͭΒΈ • IaaSΛआΓ͍ͯΔͱηΩϡϦςΟΞοϓσʔτͳͲʹ ࣮֬ʹ௥ΘΕͯͲ͔͜ͰରԠ͢ΔϋϝʹͳΔ • Φʔτεέʔϧͷߏ੒ʹ͠ͳ͍ͱෛՙʹରॲͰ͖ͳ ͍͚ͲɺݸਓͰΦʔτεέʔϧ͸͓ࡒ෍͕ͭΒ͍ • Կނ͔Docker͕յΕͯରԠͰझຯ։ൃͷ༹͕͚࣌ؒ Δ

    • ͜ͷۤ͠Έ͕WebAppͷ਺͚ͩ૿͍͑ͯ͘
  15. ͋Γ͕ͪ

  16. ࡞Ε͹࡞Δ΄Ͳ৽͍͠΋ͷΛ ࡞Δ͕࣌ؒݮ͍ͬͯ͘

  17. ϝϯςͨ͘͠ͳ͍

  18. Ͳ͏ʹ͔ͯ͠ղܾ͍ͨ͠

  19. ࠓ೔͸͜ΕΛղܾ͠Α͏

  20. Α͋͘Δղܾ๏

  21. PaaSΛ࢖͏

  22. None
  23. Heroku࢖͍͕ͪ • ݴޠΛಛʹ໰Θͣʹ࢖͑ΔͷͰศར • جຊແྉͩ͠༗ྉͰ24hىಈʹͯ͠΋͔͔ͨͩ ݄800ԁ͙Β͍ • Let’s Encrypt ͷࣗಈߋ৽΋Ͱ͖Δ͠Addon΋

    ͋ͬͯࢮ͵΄Ͳศར • େମ͜ΕͰΑ͘ͳΔ
  24. ͚ͲͭΒ͍࣌΋͋Δ

  25. HerokuͰͭΒ͍ͱ͖ • ϨΠςϯγ͕ΩπΠ • ݸਓ։ൃͰFastlyΛ࢖͑ΔΘ͚͕ͳ͍ΜͩΑͳ • Naked domainͷSSLʹରԠͯ͠ͳ͍ • ϧʔτυϝΠϯͰSSL͍ͨ͠ͳΒCloudFrontט·ͤΖ

    • େྔʹಈ͔ͩ͢͠ͱແྉ࿮͕2ඵͰރׇ͢Δ • εέʔϦϯάੑೳ͕ऴ͍ྃͯ͠Δ
  26. None
  27. GAE Standard࢖͍͕ͪ • Google͕޷͖ͳݴޠͩͱLambdaతͳ࣮ߦ࣌ؒ՝ۚͰ ΞϓϦέʔγϣϯΛಈ͔ͤΔͷͰ͍҆ • cronͱ͔΋͍ͭͯͯҙ֎ʹߴੑೳ • ೔ຊϦʔδϣϯ͕໪࿦͋ΔͷͰ͸΍ͯ͘࠷ߴͬΆ͍ •

    Naked domainͷSSLରԠ΋όονϦ • σϑΥͰΦʔτεέʔϧ͢Δ • େମ͜ΕͰΑ͘ͳΔ
  28. ͜Ε΋ͭΒ͍͕࣌͋Δ

  29. GAE StandardͰͭΒ͍ͱ͖ • Google͕޷͖ͳݴޠҎ֎͸ߴ͍ϓϥϯ(Flexible)͔͠ ͳ͍ • ͦͷ޷͖ͳݴޠ͸Python/Go/Java͋ͨΓͰͭΒ͍ • Flexible͸DockerίϯςφΛಈ͔͚ͩ͢ͳͷͰී௨ʹ ֹ݄ͰGCEͷྉ͕͔͔ۚͬͯߴͯͭ͘Β͍

    • Flexible͸ElasticBeanstalkΈ͍ͨͳ΋Μ
  30. NodeͰͷ։ൃऀʹਓݖ͕ͳ͍

  31. ͦ͜Ͱ

  32. ΋͏Ұͭͷղܾํ๏

  33. FaaSʹࡌͤΔ

  34. Lambdaͱ͔CloudFunctions

  35. FaaS • Node.jsͰॻ͚Δ(Lambda͸Python/Java΋) • Lambda͸Πϕϯτͷ൚༻ੑΛɺCloudFunctions͸ Express LikeͳI/F͕ັྗͳܗͰఏڙ͞Ε͍ͯΔ • ϨΠςϯγ΋ॳճىಈ͕ඍົ͙Β͍ •

    ໪࿦࣮ߦ࣌ؒ՝ۚͰ͍҆ • ٯʹແݶʹϦΫΤετ͕͖ͨΒۚΛ͔͚ͯΦʔτεέʔ ϧແݶʹͯ͘͠Εͯศར
  36. ͚ͲͭΒ͞΋͋Δ

  37. FaaSͷͭΒ͞ • ݁ہҰఆҎ্ಠࣗه๏ʹམͪண͘ • طଘͷNode.jsΞϓϦέʔγϣϯͷ࡞ΓํͰ࡞Δ͜ͱ ͕Ͱ͖ΔΘ͚Ͱ͸ͳ͍ • ٕज़తʹ൚༻ੑ͕ͳ͍ʢϩοΫΠϯʣ • ϩʔΧϧ࣮ߦ͕ͱʹ͔͘ΊΜͲ͍͘͞

    • API Gateway͔Β౉ͬͯ͘Δ event ͷߏ଄͕…… • ׬੒඼͸Αͯ͘΋։ൃ్த͕ͭΒ͍
  38. ͭΒ͍

  39. FaaSͷಛ௃Λ࣋ͬͨ GAE/SE for Node.js͕͋Ε͹……

  40. ͦΜͳ࣌ͷٹੈओ

  41. aws-serverless-express

  42. ͱ͜ΖͰ

  43. aws-serverless-express Λ஌͍ͬͯΔਓʁ

  44. aws-serverless-express ࢖ͬͨ͜ͱ͋Δਓʁ

  45. aws-serverless-express • Amazon͕ϊϦͰ࡞ͬͪΌͬͨ
 “AWS LambdaͰExpress͕ಈ͘” ϥΠϒϥϦ • طଘͷExpressΞϓϦέʔγϣϯʹMiddlewareΛ௥Ճ ͢Δ͚ͩͰԿނ͔LambdaͰಈ͘ •

    αʔόʔϨεͷԸܙΛड͚ͳ͕Β׳Ε਌͠ΜͩExpress ه๏ͰΞϓϦέʔγϣϯΛهड़Ͱ͖Δͷ͕ັྗ
  46. Source Code const serverlessExpress = require('aws-serverless-express/middleware') app.use(serverlessExpress.eventContext()) app.get('/', (req, res)

    => { res.json(req.apiGateway.event) })
  47. ࣮ࡍʹ࢖ͬͯΔWebApp https://connpassport.com

  48. ྑ͞

  49. Ͳ͕͜ྑ͍ͷ͔ • ී௨ʹNode.jsͰ։ൃ͍ͯ͠ΔͷʹԿނ͔ແݶʹε έʔϧ͢Δ • ී௨ʹNode.jsͰ։ൃ͍ͯ͠ΔͷʹԿނ͔࣮ߦ࣌՝ۚ • ී௨ʹNode.jsͰ։ൃ͍ͯ͠Δͷʹͳ͔ͥϝϯςφϯ εϑϦʔ •

    Naked domainͷSSLɺϦʔδϣϯʹΑΔϨΠςϯγɺ ϩοΫΠϯ……͖ͬ͞·Ͱͷશ෦ͷ՝୊͕ղফ
  50. ཉ͔ͬͨ͠΋ͷશ෦ೖΓ

  51. aws-serverless-express͸ શͯΛղܾͯ͘͠ΕΔ

  52. ͭΒ͞ͷ͓͞Β͍ • ͦ΋ͦ΋IaaS͸ϝϯς͕ͭΒ͍ • Heroku͸ϨΠςϯγ͕ͭΒ͍ • GAE/SEʹ͸ͳ͍ • FaaS͸ϩοΫΠϯ͕ͭΒ͍

  53. ղܾͰ͖͍ͯΔ͔ • ϨΠςϯγ͸ͳ͍͔
 ->ϥϯλΠϜͷॳճىಈͷ஗͚ͩ͞ • ϚωʔδυPaaS Likeʹ࢖͑Δ͔
 ->࢖͑ͯ࠷ߴ • ಠࣗه๏͸ͳ͍͔


    ->શͯExpressͦͷ··
  54. ղܾͰ͖͍ͯΔ͔ • εέʔϦϯά • ΊͪΌΊͪΌ͢Δ͠ٯʹਓ͕͍ͳ͍࣌͸͓͕͔͔ۚ Βͳ͍ͷͰͲͬͪͷҙຯͰ΋࠷ߴ • ӡ༻ίετ • جຊతʹFaaSͳͷͰօແɺূ໌ॻ΋ACM

    • API GW + ACM + Route53ʹͳΔͷͰNaked OK
  55. ૉఢʂ

  56. ࢖͍͍ͨʂ

  57. ͸͡Ί͔ͨ

  58. awslabs/aws-serverless-express ʹΞΫηε

  59. ͓΋ΉΖʹexampleΛΫϦοΫ

  60. Win/Mac/LinuxରԠͷςϯϓϨ

  61. README͕ΊͬͪΌஸೡ

  62. طଘϓϩδΣΫτͷҠߦ

  63. yarn add aws-serverless-express

  64. lambda.jsΛ࡞੒ const awsServerlessExpress = require('aws-serverless-express') const app = require('./app') const

    server = awsServerlessExpress.createServer(app) exports.handler = (event, context) => awsServerlessExpress.proxy(server, event, context)
  65. Expressଆ(e.g. app.js)ʹ௥ه const serverlessExpress = require('aws-serverless-express/middleware') app.use(serverlessExpress.eventContext())

  66. ͜ͷ··LambdaʹσϓϩΠͰ OK

  67. ·ͱΊ

  68. aws-serverless-express • LambdaͰExpressΛಈ͔͢ϥΠϒϥϦ • Expressϛυϧ΢ΣΞͱͯ͠։ൃ͞Ε͓ͯΓɺ طଘϓϩδΣΫτ΁ͷಋೖ΋Մೳ • Expressͷ͔͖৺஍ͰLambdaͷPros/ConsΛ Ҿ͖ܧ͙ΞϓϦέʔγϣϯΛ࡞Δ͜ͱ͕Մೳ

  69. Α͞ • ݱঢ়ͷPaaSࣄ৘Ͱ͸ຬͨͤͳ͍ཁ݅ΛຬͨͤΔ • ௿ϨΠςϯγ • ࣮ߦ࣌ؒ՝͔ۚͭΦʔτεέʔϦϯά • Node.js؀ڥ͕ಈ͘ •

    Naked domainͷSSLରԠ • ͷશ෦ೖΓ؀ڥ
  70. ͸͡ΊΔʹ͸ • ެࣜϨϙδτϦͷexample͕ϘΠϥʔϓϨʔ τͳͷͰͦͷ··࢖͑Δ • طଘϓϩδΣΫτ΁ͷಋೖ΋READMEʹ͋Δ ͱ͓ΓʹmiddlewareΛ௥Ճ͢Δ͚ͩ

  71. ࢖͓͏

  72. ͓ΘΓʹ

  73. None
  74. GAE/SE for Node དྷΔ΍Μ͚ʂ

  75. એ఻

  76. None
  77. None
  78. ご清聴ありがとうございま し . た    .