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
340
TSX First な Zero-Runtime SSG potato4d/dodai とその仕組み / owned static site generator #kyotojs
potato4d
1
2.1k
Vue.js with TSX - From Vue 2.x to Vue 3 #v_tokyo11
potato4d
9
4.8k
終わりゆく 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
4k
私たちはなぜ SPA で開発するのか / Why you choose SPA
potato4d
39
26k
Amplify Console 誕生以来本番運用しつづけてわかったこと #awswakaran_tokyo
potato4d
6
3.4k
Other Decks in Programming
See All in Programming
DevFest - Serverless 101 with Google Cloud Functions
tunmise
0
140
Итераторы в Go 1.23: зачем они нужны, как использовать, и насколько они быстрые?
lamodatech
0
1.4k
ドメインイベント増えすぎ問題
h0r15h0
2
570
テストコード書いてみませんか?
onopon
2
340
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
420
Оптимизируем производительность блока Казначейство
lamodatech
0
950
ecspresso, ecschedule, lambroll を PipeCDプラグインとして動かしてみた (プロトタイプ) / Running ecspresso, ecschedule, and lambroll as PipeCD Plugins (prototype)
tkikuc
2
1.9k
Scaling your build logic
antalmonori
1
100
Amazon Nova Reelの可能性
hideg
0
200
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
300
Fixstars高速化コンテスト2024準優勝解法
eijirou
0
190
Flatt Security XSS Challenge 解答・解説
flatt_security
0
740
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
46
7.2k
Documentation Writing (for coders)
carmenintech
67
4.5k
Making Projects Easy
brettharned
116
6k
KATA
mclloyd
29
14k
Become a Pro
speakerdeck
PRO
26
5.1k
GitHub's CSS Performance
jonrohan
1030
460k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
226
22k
Making the Leap to Tech Lead
cromwellryan
133
9k
Java REST API Framework Comparison - PWX 2021
mraible
28
8.3k
The World Runs on Bad Software
bkeepers
PRO
66
11k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
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
ご清聴ありがとうございま し . た .