Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
個人開発者のためのaws-serverless-express #kyotojs
Search
potato4d(Takuma HANATANI)
March 24, 2018
Programming
9
3.7k
個人開発者のためのaws-serverless-express #kyotojs
HANATANI Takuma(@potato4d) 2018.03.23
#kyotojs
potato4d(Takuma HANATANI)
March 24, 2018
Tweet
Share
More Decks by potato4d(Takuma HANATANI)
See All by potato4d(Takuma HANATANI)
どうせキレイに書けない処理は逆にAIに書いてもらうほうが良い説 / #kyotojs 22
potato4d
3
310
TSX First な Zero-Runtime SSG potato4d/dodai とその仕組み / owned static site generator #kyotojs
potato4d
1
2k
Vue.js with TSX - From Vue 2.x to Vue 3 #v_tokyo11
potato4d
9
4.7k
終わりゆく Vue 2.x 時代の状態設計のアンサー - Vue 3 の Provider への期待 / The Last Architecture of the Vue 2.x
potato4d
25
6.8k
Web Worker を使ってブラウザ上でポケモンの画像を解析したい! / Pokemon recognition from screenshots in browser using web worker
potato4d
0
1.2k
Firebase & Google Cloud によるサーバーレス帳票管理 #FJUG / Serverless Architecture in Candy
potato4d
8
3.6k
NestJS meetup Tokyo Opening Talk / What is NestJS? #nestjs_meetup
potato4d
11
3.9k
私たちはなぜ SPA で開発するのか / Why you choose SPA
potato4d
39
26k
Amplify Console 誕生以来本番運用しつづけてわかったこと #awswakaran_tokyo
potato4d
6
3.4k
Other Decks in Programming
See All in Programming
광고 소재 심사 과정에 AI를 도입하여 광고 서비스 생산성 향상시키기
kakao
PRO
0
170
役立つログに取り組もう
irof
28
9.6k
Realtime API 入門
riofujimon
0
150
『ドメイン駆動設計をはじめよう』のモデリングアプローチ
masuda220
PRO
8
540
Jakarta EE meets AI
ivargrimstad
0
520
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
2
350
macOS でできる リアルタイム動画像処理
biacco42
9
2.4k
Click-free releases & the making of a CLI app
oheyadam
2
110
型付き API リクエストを実現するいくつかの手法とその選択 / Typed API Request
euxn23
8
2.2k
WebフロントエンドにおけるGraphQL(あるいはバックエンドのAPI)との向き合い方 / #241106_plk_frontend
izumin5210
4
1.4k
CSC509 Lecture 11
javiergs
PRO
0
180
見せてあげますよ、「本物のLaravel批判」ってやつを。
77web
7
7.7k
Featured
See All Featured
Become a Pro
speakerdeck
PRO
25
5k
Speed Design
sergeychernyshev
24
610
Navigating Team Friction
lara
183
14k
10 Git Anti Patterns You Should be Aware of
lemiorhan
654
59k
Automating Front-end Workflow
addyosmani
1366
200k
StorybookのUI Testing Handbookを読んだ
zakiyama
27
5.3k
Raft: Consensus for Rubyists
vanstee
136
6.6k
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
24k
The World Runs on Bad Software
bkeepers
PRO
65
11k
Happy Clients
brianwarren
98
6.7k
Unsuck your backbone
ammeep
668
57k
jQuery: Nuts, Bolts and Bling
dougneiner
61
7.5k
Transcript
ݸਓ։ൃऀͷͨΊͷ aws-serverless-express HANATANI Takuma(@potato4d) 2018.03.23 #kyotojs
Ξ • ࣗݾհ • ݸਓ։ൃʹ͓͚ΔΠϯϑϥෛ୲ͷਏ͞ • PaaSʹΑΔղܾ • aws-serverless-expressͷྑ͞ •
ݸਓ։ൃऀʹͱͬͯͷNode.jsͷΠϯϑϥͷͭΒ͞ • aws-serverless-expressͷ͡Ί͔ͨ
ࣗݾհ
Ί͙ΈΜ - ాਅྱ / ߴڮཥґ ߚຐͷগঁɻ13ࡀˠ14ࡀ ʢWEB൛Ͱ17ࡀલޙʣɻεϐϯΦϑʮ͜ͷૉΒ͍͠ ੈքʹരԍΛʂ ʯͷओਓެɻ
ࠇʹߚ͍ɻࠇϚϯτ ʹࠇϩʔϒɺτϯΨϦࢠΛඃΓɺࠨʹ؟ଳʢผʹ ͕ѱ͍Θ͚Ͱͳ͘ɺͨͩͷϑΝογϣϯʣΛ͚͍ͭͯ ΔɻΧζϚͱΞΫΞͷग़ͨ͠ύʔςΟʔϝϯόʔืूͷ ுΓࢴΛݟͯؒʹՃΘΔɻମ֨খฑͰࡉɺ গͳ͘ͱΧζϚ(165cm)ΑΓ͍ɻෑͰͷύ IUUQTCJUMZ;#'.;P
@potato4d • ౦ژͰϑϦʔϥϯεͬͯΔ • ͍͍ͩͨϑϩϯτΤϯυ • ࠷ۙຆͲNuxtͷ։ൃͱ͔ϑϩϯτٕज़૬ஊ • Vue.js JP
/ VueFes Staff / Vue.js translator • ͜ͷؒٱʑʹNode.jsΛੜͰॻ͍ͨΒdeep copyͷͨ Ίʹobject-rest-spreadΛͬͯCIΛམͱͨ͠ਓ
ຊ
͍͖ͳΓͰ͕͢օ͞Μʂ
ݸਓ։ൃͯ͠·͔͢ʁ
WebApp։ൃͯ͠·͔͢ʁ
ݸਓ։ൃͷʮΠϯϑϥʯ ͭΒ͘ͳ͍Ͱ͔͢ʁ
ʮϝϯςφϯεʯʹ ৽͍͜͠ͱΛΔ࣌ؒΛ ୣΘΕ͍ͯ·ͤΜ͔ʁ
ຊ࣭తͰͳ͍࡞ۀͰ ϞνϕΛԼ͍͛ͯ·ͤΜ͔ʁ
ྫ͑ʮ͜Μͳ͜ͱʯ ىͬͯ͜ͳ͍Ͱ͔͢ʁ
ݸਓ։ൃͷΠϯϑϥϝϯςͷͭΒΈ • IaaSΛआΓ͍ͯΔͱηΩϡϦςΟΞοϓσʔτͳͲʹ ࣮֬ʹΘΕͯͲ͔͜ͰରԠ͢ΔϋϝʹͳΔ • Φʔτεέʔϧͷߏʹ͠ͳ͍ͱෛՙʹରॲͰ͖ͳ ͍͚ͲɺݸਓͰΦʔτεέʔϧ͓ࡒ͕ͭΒ͍ • Կނ͔Docker͕յΕͯରԠͰझຯ։ൃͷ༹͕͚࣌ؒ Δ
• ͜ͷۤ͠Έ͕WebAppͷ͚ͩ૿͍͑ͯ͘
͋Γ͕ͪ
࡞Ε࡞Δ΄Ͳ৽͍͠ͷΛ ࡞Δ͕࣌ؒݮ͍ͬͯ͘
ϝϯςͨ͘͠ͳ͍
Ͳ͏ʹ͔ͯ͠ղܾ͍ͨ͠
ࠓ͜ΕΛղܾ͠Α͏
Α͋͘Δղܾ๏
PaaSΛ͏
None
Heroku͍͕ͪ • ݴޠΛಛʹΘͣʹ͑ΔͷͰศར • جຊແྉͩ͠༗ྉͰ24hىಈʹ͔͔ͯͨͩ͠ ݄800ԁ͙Β͍ • Let’s Encrypt ͷࣗಈߋ৽Ͱ͖Δ͠Addon
͋ͬͯࢮ͵΄Ͳศར • େମ͜ΕͰΑ͘ͳΔ
͚ͲͭΒ͍࣌͋Δ
HerokuͰͭΒ͍ͱ͖ • ϨΠςϯγ͕ΩπΠ • ݸਓ։ൃͰFastlyΛ͑ΔΘ͚͕ͳ͍ΜͩΑͳ • Naked domainͷSSLʹରԠͯ͠ͳ͍ • ϧʔτυϝΠϯͰSSL͍ͨ͠ͳΒCloudFrontט·ͤΖ
• େྔʹಈ͔ͩ͢͠ͱແྉ͕2ඵͰރׇ͢Δ • εέʔϦϯάੑೳ͕ऴ͍ྃͯ͠Δ
None
GAE Standard͍͕ͪ • Google͕͖ͳݴޠͩͱLambdaతͳ࣮ߦ࣌ؒ՝ۚͰ ΞϓϦέʔγϣϯΛಈ͔ͤΔͷͰ͍҆ • cronͱ͔͍ͭͯͯҙ֎ʹߴੑೳ • ຊϦʔδϣϯ͕͋ΔͷͰͯ͘࠷ߴͬΆ͍ •
Naked domainͷSSLରԠόονϦ • σϑΥͰΦʔτεέʔϧ͢Δ • େମ͜ΕͰΑ͘ͳΔ
͜ΕͭΒ͍͕࣌͋Δ
GAE StandardͰͭΒ͍ͱ͖ • Google͕͖ͳݴޠҎ֎ߴ͍ϓϥϯ(Flexible)͔͠ ͳ͍ • ͦͷ͖ͳݴޠPython/Go/Java͋ͨΓͰͭΒ͍ • FlexibleDockerίϯςφΛಈ͔͚ͩ͢ͳͷͰී௨ʹ ֹ݄ͰGCEͷྉ͕͔͔ۚͬͯߴͯͭ͘Β͍
• FlexibleElasticBeanstalkΈ͍ͨͳΜ
NodeͰͷ։ൃऀʹਓݖ͕ͳ͍
ͦ͜Ͱ
͏Ұͭͷղܾํ๏
FaaSʹࡌͤΔ
Lambdaͱ͔CloudFunctions
FaaS • Node.jsͰॻ͚Δ(LambdaPython/Java) • LambdaΠϕϯτͷ൚༻ੑΛɺCloudFunctions Express LikeͳI/F͕ັྗͳܗͰఏڙ͞Ε͍ͯΔ • ϨΠςϯγॳճىಈ͕ඍົ͙Β͍ •
࣮ߦ࣌ؒ՝ۚͰ͍҆ • ٯʹແݶʹϦΫΤετ͕͖ͨΒۚΛ͔͚ͯΦʔτεέʔ ϧແݶʹͯ͘͠Εͯศར
͚ͲͭΒ͋͞Δ
FaaSͷͭΒ͞ • ݁ہҰఆҎ্ಠࣗه๏ʹམͪண͘ • طଘͷNode.jsΞϓϦέʔγϣϯͷ࡞ΓํͰ࡞Δ͜ͱ ͕Ͱ͖ΔΘ͚Ͱͳ͍ • ٕज़తʹ൚༻ੑ͕ͳ͍ʢϩοΫΠϯʣ • ϩʔΧϧ࣮ߦ͕ͱʹ͔͘ΊΜͲ͍͘͞
• API Gateway͔Βͬͯ͘Δ event ͷߏ͕…… • Αͯ͘։ൃ్த͕ͭΒ͍
ͭΒ͍
FaaSͷಛΛ࣋ͬͨ GAE/SE for Node.js͕͋Ε……
ͦΜͳ࣌ͷٹੈओ
aws-serverless-express
ͱ͜ΖͰ
aws-serverless-express Λ͍ͬͯΔਓʁ
aws-serverless-express ͬͨ͜ͱ͋Δਓʁ
aws-serverless-express • Amazon͕ϊϦͰ࡞ͬͪΌͬͨ “AWS LambdaͰExpress͕ಈ͘” ϥΠϒϥϦ • طଘͷExpressΞϓϦέʔγϣϯʹMiddlewareΛՃ ͢Δ͚ͩͰԿނ͔LambdaͰಈ͘ •
αʔόʔϨεͷԸܙΛड͚ͳ͕Β׳Ε͠ΜͩExpress ه๏ͰΞϓϦέʔγϣϯΛهड़Ͱ͖Δͷ͕ັྗ
Source Code const serverlessExpress = require('aws-serverless-express/middleware') app.use(serverlessExpress.eventContext()) app.get('/', (req, res)
=> { res.json(req.apiGateway.event) })
࣮ࡍʹͬͯΔWebApp https://connpassport.com
ྑ͞
Ͳ͕͜ྑ͍ͷ͔ • ී௨ʹNode.jsͰ։ൃ͍ͯ͠ΔͷʹԿނ͔ແݶʹε έʔϧ͢Δ • ී௨ʹNode.jsͰ։ൃ͍ͯ͠ΔͷʹԿނ͔࣮ߦ࣌՝ۚ • ී௨ʹNode.jsͰ։ൃ͍ͯ͠Δͷʹͳ͔ͥϝϯςφϯ εϑϦʔ •
Naked domainͷSSLɺϦʔδϣϯʹΑΔϨΠςϯγɺ ϩοΫΠϯ……͖ͬ͞·Ͱͷશ෦ͷ՝͕ղফ
ཉ͔ͬͨ͠ͷશ෦ೖΓ
aws-serverless-express શͯΛղܾͯ͘͠ΕΔ
ͭΒ͞ͷ͓͞Β͍ • ͦͦIaaSϝϯς͕ͭΒ͍ • HerokuϨΠςϯγ͕ͭΒ͍ • GAE/SEʹͳ͍ • FaaSϩοΫΠϯ͕ͭΒ͍
ղܾͰ͖͍ͯΔ͔ • ϨΠςϯγͳ͍͔ ->ϥϯλΠϜͷॳճىಈͷ͚ͩ͞ • ϚωʔδυPaaS Likeʹ͑Δ͔ ->͑ͯ࠷ߴ • ಠࣗه๏ͳ͍͔
->શͯExpressͦͷ··
ղܾͰ͖͍ͯΔ͔ • εέʔϦϯά • ΊͪΌΊͪΌ͢Δ͠ٯʹਓ͕͍ͳ͍͓͕͔͔࣌ۚ Βͳ͍ͷͰͲͬͪͷҙຯͰ࠷ߴ • ӡ༻ίετ • جຊతʹFaaSͳͷͰօແɺূ໌ॻACM
• API GW + ACM + Route53ʹͳΔͷͰNaked OK
ૉఢʂ
͍͍ͨʂ
͡Ί͔ͨ
awslabs/aws-serverless-express ʹΞΫηε
͓ΉΖʹexampleΛΫϦοΫ
Win/Mac/LinuxରԠͷςϯϓϨ
README͕ΊͬͪΌஸೡ
طଘϓϩδΣΫτͷҠߦ
yarn add aws-serverless-express
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)
Expressଆ(e.g. app.js)ʹه const serverlessExpress = require('aws-serverless-express/middleware') app.use(serverlessExpress.eventContext())
͜ͷ··LambdaʹσϓϩΠͰ OK
·ͱΊ
aws-serverless-express • LambdaͰExpressΛಈ͔͢ϥΠϒϥϦ • ExpressϛυϧΣΞͱͯ͠։ൃ͞Ε͓ͯΓɺ طଘϓϩδΣΫτͷಋೖՄೳ • Expressͷ͔͖৺ͰLambdaͷPros/ConsΛ Ҿ͖ܧ͙ΞϓϦέʔγϣϯΛ࡞Δ͜ͱ͕Մೳ
Α͞ • ݱঢ়ͷPaaSࣄͰຬͨͤͳ͍ཁ݅ΛຬͨͤΔ • ϨΠςϯγ • ࣮ߦ࣌ؒ՝͔ۚͭΦʔτεέʔϦϯά • Node.jsڥ͕ಈ͘ •
Naked domainͷSSLରԠ • ͷશ෦ೖΓڥ
͡ΊΔʹ • ެࣜϨϙδτϦͷexample͕ϘΠϥʔϓϨʔ τͳͷͰͦͷ··͑Δ • طଘϓϩδΣΫτͷಋೖREADMEʹ͋Δ ͱ͓ΓʹmiddlewareΛՃ͢Δ͚ͩ
͓͏
͓ΘΓʹ
None
GAE/SE for Node དྷΔΜ͚ʂ
એ
None
None
ご清聴ありがとうございま し . た .