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
110
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.1k
AWS Lambda + PlanetScale = 超絶スケールWebアプリ
ruzia
1
210
Jets ~ Rubyで始めるServerless生活 ~
ruzia
0
1.1k
Jets ~Rubyで始めるServerless生活~
ruzia
0
690
プログラマがAMPと付き合う上で 知っておくべきこと
ruzia
0
2.2k
Other Decks in Programming
See All in Programming
Kubernetes for Data Engineers: Building Scalable, Reliable Data Pipelines
sucitw
1
200
Synchronizationを支える技術
s_shimotori
1
150
Streams APIとTCPフロー制御 / Web Streams API and TCP flow control
tasshi
1
290
飲食業界向けマルチプロダクトを実現させる開発体制とリアルな現状
hiroya0601
1
390
破壊せよ!データ破壊駆動で考えるドメインモデリング / data-destroy-driven
minodriven
16
4k
アジャイルを支えるテストアーキテクチャ設計/Test Architecting for Agile
goyoki
7
2.8k
Webの技術スタックで マルチプラットフォームアプリ開発を可能にするElixirDesktopの紹介
thehaigo
2
910
Jakarta Concurrencyによる並行処理プログラミングの始め方 (JJUG CCC 2024 Fall)
tnagao7
1
230
Piniaの現状と今後
waka292
5
1.4k
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
540
カラム追加で増えるActiveRecordのメモリサイズ イメージできますか?
asayamakk
4
1.5k
CPython 인터프리터 구조 파헤치기 - PyCon Korea 24
kennethanceyer
0
240
Featured
See All Featured
RailsConf 2023
tenderlove
29
880
Reflections from 52 weeks, 52 projects
jeffersonlam
346
20k
GitHub's CSS Performance
jonrohan
1030
460k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
31
2.7k
Build your cross-platform service in a week with App Engine
jlugia
229
18k
The Cost Of JavaScript in 2023
addyosmani
45
6.6k
Speed Design
sergeychernyshev
24
570
Dealing with People You Can't Stand - Big Design 2015
cassininazir
364
22k
Learning to Love Humans: Emotional Interface Design
aarron
272
40k
Code Reviewing Like a Champion
maltzj
519
39k
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
92
16k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
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
εέʔϧʹର͢Δ ෆ҆Λͳͯ͘͠ ΞϓϦͷ։ൃʹ ूதͰ͖ΔΑ͏ʹ
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ