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
120
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.2k
AWS Lambda + PlanetScale = 超絶スケールWebアプリ
ruzia
1
230
Jets ~ Rubyで始めるServerless生活 ~
ruzia
0
1.1k
Jets ~Rubyで始めるServerless生活~
ruzia
0
710
プログラマがAMPと付き合う上で 知っておくべきこと
ruzia
0
2.2k
Other Decks in Programming
See All in Programming
今年のアップデートで振り返るCDKセキュリティのシフトレフト/2024-cdk-security-shift-left
tomoki10
0
210
Асинхронность неизбежна: как мы проектировали сервис уведомлений
lamodatech
0
840
Security_for_introducing_eBPF
kentatada
0
110
testcontainers のススメ
sgash708
1
120
Keeping it Ruby: Why Your Product Needs a Ruby SDK - RubyWorld 2024
envek
0
190
PHPUnitしか使ってこなかった 一般PHPerがPestに乗り換えた実録
mashirou1234
0
230
毎日13時間もかかるバッチ処理をたった3日で60%短縮するためにやったこと
sho_ssk_
1
160
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
どうして手を動かすよりもチーム内のコードレビューを優先するべきなのか
okashoi
3
170
PHPとAPI Platformで作る本格的なWeb APIアプリケーション(入門編) / phpcon 2024 Intro to API Platform
ttskch
0
270
開発者とQAの越境で自動テストが増える開発プロセスを実現する
92thunder
1
190
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
110
Featured
See All Featured
Optimizing for Happiness
mojombo
376
70k
Embracing the Ebb and Flow
colly
84
4.5k
The Pragmatic Product Professional
lauravandoore
32
6.3k
Navigating Team Friction
lara
183
15k
The Illustrated Children's Guide to Kubernetes
chrisshort
48
48k
Agile that works and the tools we love
rasmusluckow
328
21k
How GitHub (no longer) Works
holman
311
140k
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
159
15k
Imperfection Machines: The Place of Print at Facebook
scottboms
266
13k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
2
290
個人開発の失敗を避けるイケてる考え方 / tips for indie hackers
panda_program
95
17k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
127
18k
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
εέʔϧʹର͢Δ ෆ҆Λͳͯ͘͠ ΞϓϦͷ։ൃʹ ूதͰ͖ΔΑ͏ʹ
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ