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
これでLambdaが不要に?!Step FunctionsのJSONata対応について
iwatatomoya
2
3.7k
テストコード文化を0から作り、変化し続けた組織
kazatohiei
2
1.5k
創造的活動から切り拓く新たなキャリア 好きから始めてみる夜勤オペレーターからSREへの転身
yjszk
1
140
Scalaから始めるOpenFeature入門 / Scalaわいわい勉強会 #4
arthur1
1
340
Amazon S3 NYJavaSIG 2024-12-12
sullis
0
110
Kaigi on Railsに初参加したら、その日にLT登壇が決定した件について
tama50505
0
100
Zoneless Testing
rainerhahnekamp
0
120
各クラウドサービスにおける.NETの対応と見解
ymd65536
0
160
ある日突然あなたが管理しているサーバーにDDoSが来たらどうなるでしょう?知ってるようで何も知らなかったDDoS攻撃と対策 #phpcon.2024
akase244
2
370
技術的負債と向き合うカイゼン活動を1年続けて分かった "持続可能" なプロダクト開発
yuichiro_serita
0
140
nekko cloudにおけるProxmox VE利用事例
irumaru
3
450
KMP와 kotlinx.rpc로 서버와 클라이언트 동기화
kwakeuijin
0
180
Featured
See All Featured
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
170
Large-scale JavaScript Application Architecture
addyosmani
510
110k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
251
21k
Building Adaptive Systems
keathley
38
2.3k
YesSQL, Process and Tooling at Scale
rocio
169
14k
Being A Developer After 40
akosma
87
590k
GraphQLとの向き合い方2022年版
quramy
44
13k
A Philosophy of Restraint
colly
203
16k
GraphQLの誤解/rethinking-graphql
sonatard
67
10k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
169
50k
For a Future-Friendly Web
brad_frost
175
9.4k
KATA
mclloyd
29
14k
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
εέʔϧʹର͢Δ ෆ҆Λͳͯ͘͠ ΞϓϦͷ։ൃʹ ूதͰ͖ΔΑ͏ʹ
͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ