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 Lambda + PlanetScale = 超絶スケールWebアプリ
Search
Hirokatsu Endo
July 18, 2023
Technology
1
220
AWS Lambda + PlanetScale = 超絶スケールWebアプリ
ServerlessでもRDBを利用して数百インスタンスが瞬時に起動するようなWebアプリをたった数ドルで作れる世界になりました
このスライドではその大枠を説明します
Hirokatsu Endo
July 18, 2023
Tweet
Share
More Decks by Hirokatsu Endo
See All by Hirokatsu Endo
Lambda&FargateとPlanetScaleで作るServerless Rails環境
ruzia
0
110
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
3.1k
Jets ~ Rubyで始めるServerless生活 ~
ruzia
0
1.1k
Jets ~Rubyで始めるServerless生活~
ruzia
0
700
プログラマがAMPと付き合う上で 知っておくべきこと
ruzia
0
2.2k
Other Decks in Technology
See All in Technology
EventHub Startup CTO of the year 2024 ピッチ資料
eventhub
0
110
強いチームと開発生産性
onk
PRO
34
11k
ExaDB-D dbaascli で出来ること
oracle4engineer
PRO
0
3.8k
Terraform CI/CD パイプラインにおける AWS CodeCommit の代替手段
hiyanger
1
240
初心者向けAWS Securityの勉強会mini Security-JAWSを9ヶ月ぐらい実施してきての近況
cmusudakeisuke
0
120
Platform Engineering for Software Developers and Architects
syntasso
1
510
Lexical Analysis
shigashiyama
1
150
【Startup CTO of the Year 2024 / Audience Award】アセンド取締役CTO 丹羽健
niwatakeru
0
980
社内で最大の技術的負債のリファクタリングに取り組んだお話し
kidooonn
1
550
なぜ今 AI Agent なのか _近藤憲児
kenjikondobai
4
1.4k
リンクアンドモチベーション ソフトウェアエンジニア向け紹介資料 / Introduction to Link and Motivation for Software Engineers
lmi
4
300k
[CV勉強会@関東 ECCV2024 読み会] オンラインマッピング x トラッキング MapTracker: Tracking with Strided Memory Fusion for Consistent Vector HD Mapping (Chen+, ECCV24)
abemii
0
220
Featured
See All Featured
The Power of CSS Pseudo Elements
geoffreycrofte
73
5.3k
Measuring & Analyzing Core Web Vitals
bluesmoon
4
120
Making Projects Easy
brettharned
115
5.9k
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
109
49k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.1k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
191
16k
Happy Clients
brianwarren
98
6.7k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
232
17k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
6.8k
Fashionably flexible responsive web design (full day workshop)
malarkey
405
65k
Transcript
AWS Lambda + PlanetScale = ઈεέʔϧWebΞϓϦ 2023/07/14 גࣜձࣾιχοΫΨʔσϯ ԕ౻ େհ
/ @ruzia ·ͩχονͰ Զಘ͔͠ͳ͍
ࣗݾհ ‣ ԕ౻ େհ / @ruzia ‣ גࣜձࣾ SonicGarden ‣
ϓϩάϥϚʔ / ࣥߦһ ‣ Πϯϑϥͱػցֶशܥ͕͖ ‣ ϩʔυόΠΫ࠷ۙΕͯͳ͍ ‣ FF16ʹͲͬΓਁ͔Γத
ٸʹSNSͰόζͬͨΓͯ͠ ྑ͍ײ͡ʹεέʔϧͯ͠ ͘ΕΔΠϯϑϥͳ͍Μ͔
ཉΛݴ͑ɾɾɾ ‣RailsͳͲͷϑϨʔϜϫʔΫΛͦͷ··͍͍ͨ ‣σʔλετΞʹRDB͕͍͍ͨ ‣ඦΠϯελϯεͱ͔ࣗಈͰεέʔϧͯ͠ཉ͍͠ ‣ඇಉظ / ఆظͷJob࣮ߦ͍ͨ͠ ‣One-o ff λεΫ࣮ߦ͍ͨ͠
Q ͦΜͳΠϯϑϥ ͋ΔΜͰ͔͢ʁ
A ࡞ΕͪΌ͏ΜͰ͢ ͔͠υϧͱ͔Ͱ
ࠓͦΜͳ ΠϯϑϥͷΛ͠·͢ ·ͩχον ͚ͩͲԶಘͳ
͢͜ͱ ‣LambdaͱFargateΛ׆༻࣮ͨ͠ߦڥ ‣Webαʔό ‣ඇಉظɾఆظJob ‣One-o f λεΫ ‣PlanetScaleʹΑΔεέʔϥϒϧͳRDB
None
LambdaͱFargateΛ ׆༻࣮ͨ͠ߦڥ
ͦΕͧΕͷ ڧΈ
LambdaͷڧΈ ‣େنʹεέʔϧ͢Δॠൃྗ ‣ܹ͍͠εύΠΫͷॊೈੑ Fargateʹͳ͍
FargateͷڧΈ ‣࣌ؒͷλεΫ࣮ߦ ‣ECS ExecʹΑΔϩάΠϯ Lambdaʹͳ͍
ͦΕͧΕͷڧΈΛ׆͔͢ ‣Lambda ‣εύΠΫͷܹ͍͠WebͷϦΫΤετड͚͚ ‣15ະຬͰऴྃ͢ΔܰΊͷJob ‣Fargate ‣15Ҏ্࣌ؒͷ͔͔ΔॏΊͷJob ‣ϩάΠϯͯ͠৭ʑͨ͘͠ͳΔOne-o f λεΫ
େࣄͳϙΠϯτ
େࣄͳϙΠϯτ ͢Ε ಉ͡DockerΠϝʔδΛ ྆ํͰ͑Δ ࣮
ϕʔεͷDockerΠϝʔδ Lambda Runtime APIͰ LambdaͱΓͱΓ Ͱ͖ΔΑ͏ʹ
# Install the Runtime Interface Client RUN gem install --no-document
aws_lambda_ric ENTRYPOINT ["/usr/local/bundle/bin/aws_lambda_ric"] CMD ["config/environment.Lamby.cmd"]
FargateͰඞཁʹԠͯ͡ CommandͱEntryPointΛ ΦʔόʔϥΠυ͢Δ
tasks: oneoff: name: ${self:service}-${self:provider.stage}-oneoff image: rails_image command: entryPoint: - '/bin/sh'
- '-c' - "sleep infinity"
WebJobOne-o f શͯΛ୯ҰͷDocker ΠϝʔδͰ࣮ߦͰ͖Δ
Webαʔό
Webαʔό ‣API Gatewayܦ༝Ͱݺͼग़͞ΕΔ ‣Runtime Interface Clientʹ ४ڌ͍ͯ͠Δඞཁ͕͋Δ
Railsͷ߹ Lambyͱ͍͏OSS͕ ໘Λݟͯ͘ΕΔ
None
ඇಉظJob
ඇಉظJob ‣WebαʔόͰಉظॲཧ͠ͳͯ͘ ྑ͍͕͛ΒΕͯ͘Δ ‣SQSʹͨΊͯLambdaͰॱ࣍ ࣮ߦ͍ͯ͘͠
Railsͷ߹ Lambdakiqͱ͍͏ OSSͰରԠՄೳ
None
ఆظJob
ఆظJob ‣ఆظతʹಈ͔͍ͨ͠όονॲཧ ‣EventBridgeCloudWatch Events͔Β ࣮ߦ͢Δ ‣࣌ؒͷͷLambda ‣࣌ؒͷͷFargate
functions: job: image: <<: *rails_image environment: ${self:custom.environment} memorySize: 1024 timeout:
890 events: - schedule: name: HourlyJob-${self:service}-${self:provider.stage} rate: cron(0 * * * ? *) input: lamby: runner: './bin/rails task:hourly_job'
fargate: tasks: HourlyJob: name: HourlyJob-${self:service}-${self:provider.stage} image: rails_image command: entryPoint: -
'/bin/sh' - '-c' - "./bin/rails task:hourly_job" schedule: 'cron(0 * * * ? *)'
One-o f λεΫ
One-o f λεΫ ‣खಈͰ࣮ߦ͍ͨ͠όονॲཧͱ͔ ‣ԿΒ͔ͷཧ༝Ͱ rails c ͍ͨ͠߹ͱ͔ ‣ίϯςφ෦͔ΒௐࠪΛ͍ͨ͠߹ͱ͔ ‣etc...
One-o f λεΫར༻࣌ʹ Fargateͷઐ༻λεΫΛ ىಈͯ͠ECS Exec͢Δ
One-o f λεΫ͕ ऴΘͬͨΒFargateͷ λεΫऴྃͤ͞Δ
fargate: tasks: oneoff: name: ${self:service}-${self:provider.stage}-oneoff image: rails_image command: entryPoint: -
'/bin/sh' - '-c' - "sleep infinity"
#!/usr/bin/env bash set -e APP_NAME="sample" STAGE=${1:-staging} CLUSTER="${APP_NAME}-${STAGE}" ... LATEST_TASK_DEFINITION_ARN=$( aws
ecs list-task-definitions --family-prefix $CLUSTER-oneoff ... ) echo "== Run console on Fargate..." echo "== Starting $LATEST_TASK_DEFINITION_ARN" TASK_ARN=$( aws ecs run-task --cluster $CLUSTER --task-definition $LATEST_TASK_DEFINITION_ARN ... ) echo "== TaskArn: ${TASK_ARN}" aws ecs wait tasks-running --cluster $CLUSTER --tasks $TASK_ARN echo "== Waiting for managed agent launch..." while true; do AGENT_STATUS=$( aws ecs describe-tasks --cluster $CLUSTER --tasks $TASK_ARN ... ) if [[ "$AGENT_STATUS" != "RUNNING" ]]; then sleep 1 else # ΤʔδΣϯτͷىಈ͔Β͞Βʹ 2 ඵ͘Β͍ͨͳ͍ͱ ssm ͕ܨ͕Βͳ͍ sleep 2 break fi done aws ecs execute-command --cluster $CLUSTER --task $TASK_ARN --container ${CLUSTER}-oneoff ... echo "== Stopping $LATEST_TASK_DEFINITION_ARN" aws ecs stop-task --cluster $CLUSTER --task $TASK_ARN > /dev/null 2>&1 ࠷৽ͷλεΫఆٛARNΛऔಘ λεΫΛ࣮ߦ λεΫͷىಈͪ Managed Agentͷىಈͪ ECS ExecͰϩάΠϯ λεΫΛऴྃ
͢͜ͱ ‣LambdaͱFargateΛ׆༻࣮ͨ͠ߦڥ ‣Webαʔό ‣ඇಉظɾఆظJob ‣One-o f λεΫ ‣PlanetScaleʹΑΔεέʔϥϒϧͳRDB
PlanetScale
None
Serverless MySQL Platform
ଟগͷบ͋Δ͚Ͳ DBʹίωΫγϣϯΛ ܨ͍ͰΫΤϦΛ͛ΒΕΔ
WebΞϓϦ͔ΒݟΔͱ ΄΅௨ৗͷMySQL
ͦΕͳͷʹ ίωΫγϣϯͱ͍͏ ᐫ͕ଘࡏ͠ͳ͍ ڪΖ͍͠ ͜ͱʹ
None
PlanetScaleͰ͋Ε Lambda͕εέʔϧͯ͠ ා͘ͳ͍
PlanetScaleͷར ‣΄΅ී௨ͷMySQL ‣ίωΫγϣϯͱ͍͏ᐫ͕ແ͍ ‣՝͕ۚ༰ྔɾಡΈॻ͖ͷྔϕʔε
PlanetScaleͷҙ ‣1τϥϯβΫγϣϯͷ੍࣌ؒݶ͕͋Δ ‣1ΫΤϦ͋ͨΓͷ੍࣌ؒݶ͕͋Δ ‣ΧϥϜςʔϒϧͷϦωʔϜ͕ෆՄ
LambdaFargateͱ Έ߹ΘͤΔͱ࠷ڧͷ σʔλετΞʹͳΔ
·ͱΊ
ͨ͜͠ͱ ‣LambdaͱFargateΛ׆༻࣮ͨ͠ߦڥ ‣Webαʔό ‣ඇಉظɾఆظJob ‣One-o f λεΫ ‣PlanetScaleʹΑΔεέʔϥϒϧͳRDB
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ