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
Lambda&FargateとPlanetScaleで作るServerless Rails環境
Search
Hirokatsu Endo
July 13, 2024
Programming
0
130
Lambda&FargateとPlanetScaleで作るServerless Rails環境
AWSのLambdaとFargate、そしてPlanetScaleを組み合わせてサーバの管理をほとんどしなくても良い感じでスケールしてくれるインフラ環境を作れたのでそれの紹介をしています
Hirokatsu Endo
July 13, 2024
Tweet
Share
More Decks by Hirokatsu Endo
See All by Hirokatsu Endo
デフォルトにして至高、RubyMineの大好きな所
ruzia
0
3.3k
AWS Lambda + PlanetScale = 超絶スケールWebアプリ
ruzia
1
230
Jets ~ Rubyで始めるServerless生活 ~
ruzia
0
1.1k
Jets ~Rubyで始めるServerless生活~
ruzia
0
720
プログラマがAMPと付き合う上で 知っておくべきこと
ruzia
0
2.2k
Other Decks in Programming
See All in Programming
chibiccをCILに移植した結果 (NGK2025S版)
kekyo
PRO
0
130
PHPカンファレンス 2024|共創を加速するための若手の技術挑戦
weddingpark
0
140
見えないメモリを観測する: PHP 8.4 `pg_result_memory_size()` とSQL結果のメモリ管理
kentaroutakeda
0
940
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
7.7k
良いユニットテストを書こう
mototakatsu
11
3.6k
Azure AI Foundryのご紹介
qt_luigi
1
210
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
250
Rubyでつくるパケットキャプチャツール
ydah
0
170
PicoRubyと暮らす、シェアハウスハック
ryosk7
0
220
GitHub CopilotでTypeScriptの コード生成するワザップ
starfish719
26
6k
盆栽転じて家具となる / Bonsai and Furnitures
aereal
0
1.9k
いりゃあせ、PHPカンファレンス名古屋2025 / Welcome to PHP Conference Nagoya 2025
ttskch
1
180
Featured
See All Featured
Unsuck your backbone
ammeep
669
57k
Typedesign – Prime Four
hannesfritz
40
2.5k
VelocityConf: Rendering Performance Case Studies
addyosmani
327
24k
Site-Speed That Sticks
csswizardry
3
270
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
28
2.2k
[RailsConf 2023 Opening Keynote] The Magic of Rails
eileencodes
28
9.2k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
6
500
Dealing with People You Can't Stand - Big Design 2015
cassininazir
365
25k
YesSQL, Process and Tooling at Scale
rocio
170
14k
Distributed Sagas: A Protocol for Coordinating Microservices
caitiem20
330
21k
Scaling GitHub
holman
459
140k
Transcript
Serverless Rails ~ γϯϓϧͰεέʔϧ͢ΔΠϯϑϥ ~ 2024/07/13 JAWS-UG Okayama 2024 גࣜձࣾιχοΫΨʔσϯ
ԕ౻ େհ / @ruzia LambdaˍFargateͱ PlanetScaleͰ࡞Δ
ࣗݾհ ‣ ԕ౻ େհ / @ruzia ‣ גࣜձࣾ SonicGarden ‣
ϓϩάϥϚʔ / ࣥߦһ ‣ Πϯϑϥͱػցֶशܥ͕͖ ‣ ࠷ۙGoogle Cloudʹුؾ͗Έ ‣ Vision Proָ͍͠
એ
None
https://sonicgarden.connpass.com/event/324540/
None
https://tsukuaso.connpass.com/
ຊ
͜ΜͳΠϯϑϥڥཉ͘͠ͳ͍Ͱ͔͢ʁ ‣RailsͳͲͷϑϨʔϜϫʔΫΛͦͷ··͑Δ ‣σʔλετΞʹRDB͕͑Δ ‣ඦΠϯελϯεͱ͔ࣗಈͰεέʔϧͯ͘͠ΕΔ ‣ඇಉظ / ఆظͷJob࣮ߦͰ͖Δ ‣One-o ff λεΫ࣮ߦͰ͖Δ
Q ͦΜͳΠϯϑϥ ͋ΔΜͰ͔͢ʁ
A AWSͱPlanetScaleΛ Έ߹ΘͤΕ ɹ࡞ΕΔʂ
ࠓͦΜͳ ΠϯϑϥͷΛ͠·͢
͢͜ͱ ‣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 entryPoint: - '/bin/sh' -
'-c' - "sleep infinity” command:
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 entryPoint: - '/bin/sh'
- '-c' - "./bin/rails task:hourly_job" command: 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
The world’s most advanced, fully- managed MySQL database platform.
10,000ίωΫγϣϯ ܨ͛ΔԽ͚Έ͍ͨͳ MySQL
ଟগͷบ͋Δ͚Ͳ WebΞϓϦ͔ΒݟΔͱ ΄΅ී௨ͷMySQL
PlanetScaleͳΒ Lambda͕ٸʹ εέʔϧͯ͠ා͘ͳ͍
ྉۚͦ͜·Ͱ ͓ߴ͘ͳ͍
None
PlanetScaleͷར ‣΄΅ී௨ͷMySQL ‣ίωΫγϣϯͱ͍͏ᐫ͕ແ͍ ‣RDSͱൺֱͯͦ͠Μͳߴ͘ͳ͍
PlanetScaleͷҙ ‣1τϥϯβΫγϣϯͷ੍࣌ؒݶ͕͋Δ ‣1ΫΤϦͰѻ͑Δใྔͷ੍ݶ͕͋Δ ‣ΧϥϜςʔϒϧͷϦωʔϜ͕ෆՄ
LambdaFargateͱ Έ߹ΘͤΔͱ࠷ڧͷ σʔλετΞʹͳΔ
·ͱΊ
ͨ͜͠ͱ ‣LambdaͱFargateΛ׆༻࣮ͨ͠ߦڥ ‣Webαʔό ‣ඇಉظɾఆظJob ‣One-o f λεΫ ‣PlanetScaleʹΑΔεέʔϥϒϧͳRDB
εέʔϧʹର͢Δ ෆ҆Λͳͯ͘͠ ΞϓϦͷ։ൃʹ ूதͰ͖ΔΑ͏ʹ
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ