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
230
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
130
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
3.3k
Jets ~ Rubyで始めるServerless生活 ~
ruzia
0
1.1k
Jets ~Rubyで始めるServerless生活~
ruzia
0
720
プログラマがAMPと付き合う上で 知っておくべきこと
ruzia
0
2.2k
Other Decks in Technology
See All in Technology
re:Invent 2024のふりかえり
beli68
0
110
[IBM TechXchange Dojo]Watson Discoveryとwatsonx.aiでRAGを実現!座学①
siyuanzh09
0
110
When Windows Meets Kubernetes…
pichuang
0
310
Alignment and Autonomy in Cybozu - 300人の開発組織でアラインメントと自律性を両立させるアジャイルな組織運営 / RSGT2025
ama_ch
1
2.4k
商品レコメンドでのexplicit negative feedbackの活用
alpicola
2
370
GoogleのAIエージェント論 Authors: Julia Wiesinger, Patrick Marlow and Vladimir Vuskovic
customercloud
PRO
0
160
メールヘッダーを見てみよう
hinono
0
110
#TRG24 / David Cuartielles / Post Open Source
tarugoconf
0
590
【JAWS-UG大阪 reInvent reCap LT大会 サンバが始まったら強制終了】“1分”で初めてのソロ参戦reInventを数字で振り返りながら反省する
ttelltte
0
140
Docker Desktop で Docker を始めよう
zembutsu
PRO
0
180
【NGK2025S】動物園(PINTO_model_zoo)に遊びに行こう
kazuhitotakahashi
0
240
Visual StudioとかIDE関連小ネタ話
kosmosebi
1
380
Featured
See All Featured
Code Reviewing Like a Champion
maltzj
521
39k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
44
9.4k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.1k
How to train your dragon (web standard)
notwaldorf
89
5.8k
KATA
mclloyd
29
14k
Side Projects
sachag
452
42k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
7
570
jQuery: Nuts, Bolts and Bling
dougneiner
62
7.6k
The Success of Rails: Ensuring Growth for the Next 100 Years
eileencodes
44
7k
How to Think Like a Performance Engineer
csswizardry
22
1.3k
What’s in a name? Adding method to the madness
productmarketing
PRO
22
3.2k
ピンチをチャンスに:未来をつくるプロダクトロードマップ #pmconf2020
aki_iinuma
113
50k
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
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ