Jets ~ Rubyで始めるServerless生活 ~

Jets ~ Rubyで始めるServerless生活 ~

AWSのLambda上でRubyを使ってサーバレスアプリを作成・運用することが出来るフレームワーク「Jets」の紹介です。

19675bc717632ffd7c53aa8b9a9c3cbf?s=128

Hirokatsu Endo

November 28, 2019
Tweet

Transcript

  1. Jets ~ RubyͰ࢝ΊΔServerlessੜ׆ ~ 2019/11/28 SonicGarden Tech גࣜձࣾιχοΫΨʔσϯ ԕ౻ େհ

    / @ruzia
  2. ࣗݾ঺հ ‣ԕ౻ େհ / @ruzia ‣גࣜձࣾ SonicGarden ‣ΠϯϑϥपΓͱ͔େن໛ ͱ͔ߴෛՙͱ͔େ޷͖ ‣ϩʔυόΠΫ΋ϋϚΓத

    ‣σεɾετָ͍͠Ͷʂ
  3. ຊ೔ͷ͓୊໨΍Β ‣RubyͱServerlessɺ૬ੑ͍͍Αʂ ‣30෼͔͠ͳ͍ͷͰɺ޿͘ઙ͘࿩͢Α ‣ϑΟʔυόοΫ͸ͦͷ৔Ͱ߹͍ͷखΛ

  4. Q ͱ͜ΖͰΈͳ͞Μ Serverlessͯ͠·͔͢ʁ

  5. Serverlessͱ͸ʁ ‣αʔόͷߏஙɾӡ༻͕ඞཁͳ͍ ‣՝ۚ͸αʔόͷىಈ࣌ؒͰ͸ͳ͘ɺ
 ϓϩάϥϜͷ࣮ߦ࣌ؒʹରͯ͠ߦΘΕΔ ‣ϓϩάϥϜͷ࣮ߦ؀ڥ͸ϑϧϚωʔδυ

  6. ͱ͸͍͑ɾɾɾ

  7. αʔόͷ؅ཧ͔Β͸ ։์͞Ε͚ͨͲɾɾɾ

  8. Q ίʔυͱAWSͷϦιʔε ؅ཧΊΜͲ͏͡Όͳ͍ʁ Lambda ΍Β API Gateway΍Β Cloud Watch Event΍Β

  9. ServerlessͰಈ͔͢ίʔυ΋ɺ ߏ੒؅ཧ΋ɺશ෦·Δͬͱ γϯϓϧʹ؅ཧ͍ͨ͠ΑͶʂ

  10. ʘͦΜͳ͋ͳͨʹʗ

  11. Ruby on Jets

  12. Q Jetsͬͯͳʹʁ

  13. A “The Ruby Serverless Framework”

  14. RubyͰServerlessΛ ָʹѻ͏ͨΊʹ࡞ΒΕͨ ϑϨʔϜϫʔΫ

  15. ͋Γ͕ͪͳߏ੒Λ ʮָʹɺγʔϜϨεʹʯ ѻ͏͜ͱ͕Ͱ͖Δ

  16. API Gateway Lambda Data Store S3 CloudWatch Event

  17. Jetsͷૉఢͳͱ͜Ζ ‣هड़͢Δίʔυ͕γϯϓϧ ‣֤छAWSͷαʔϏεͱͷ࿈ܞָ͕ͪΜ ‣AWSͷ֤छϦιʔε͸ࣗಈ؅ཧ

  18. Controllers

  19. class PostsController < ApplicationController def index # renders Lambda Proxy

    structure compatible with # API Gateway render json: {hello: "world", action: "index"} end def show # params available id = params[:id] # puts goes to the lambda logs puts event # raw lambda event available render json: {action: "show", id: id} end … end
  20. Routes

  21. Jets.application.routes.draw do get "posts", to: "posts#index" get “posts/new", to: "posts#new"

    get “posts/:id", to: "posts#show" post "posts", to: "posts#create" get "posts/:id/edit", to: "posts#edit" put "posts", to: "posts#update" delete “posts", to: "posts#delete" # expands to the RESTful routes above resources :posts # GET, POST, PUT, etc request all work any "posts/hot", to: “posts#hot” … end
  22. Jobs

  23. class HardJob < ApplicationJob rate "10 hours" # every 10

    hours def dig puts "done digging" end cron "0 */12 * * ? *" # every 12 hours def lift puts "done lifting" end … end
  24. ΊͬͪΌγϯϓϧ͡Ό ͳ͍Ͱ͔͢ʁ

  25. Jetsͷૉఢͳͱ͜Ζ ‣هड़͢Δίʔυ͕γϯϓϧ ‣֤छAWSͷαʔϏεͱͷ࿈ܞָ͕ͪΜ ‣AWSͷ֤छϦιʔε͸ࣗಈ؅ཧ

  26. ྫ͑͹IoT Eventsͱ ࿈ܞ͍ͤͨ͞৔߹

  27. class ThermostatJob < ApplicationJob iot_event "SELECT * FROM 'my/topic'" def

    measure puts "event #{JSON.dump(event)}" end … end
  28. ྫ͑͹Kinesisͱ ࿈ܞ͍ͤͨ͞৔߹

  29. class DataJob < ApplicationJob kinesis_event "my-stream" def file puts "event

    #{JSON.dump(event)}" puts "kinesis_data #{JSON.dump(kinesis_data)}" end … end
  30. ΊͬͪΌγϯϓϧ͡Ό ͳ͍Ͱ͔͢ʁ (2ճ໨

  31. ΄͔ʹ΋৭ʑ࿈ܞͰ͖Δ ‣CloudWatch Log Events ‣DynamoDB Events ‣S3 Events ‣SNS Events

    ‣SQS Events
  32. Jetsͷૉఢͳͱ͜Ζ ‣هड़͢Δίʔυ͕γϯϓϧ ‣֤छAWSͷαʔϏεͱͷ࿈ܞָ͕ͪΜ ‣AWSͷ֤छϦιʔε͸ࣗಈ؅ཧ

  33. Q ίʔυ͸γϯϓϧͰ΋ σϓϩΠ·Ͱ͕໘౗ͩͬͨΓ ͢ΔΜͰ͠ΐʁ

  34. A ʘຐ๏ͷίϚϯυʗ jets deploy

  35. $ jets deploy Deploying to Lambda demo-dev environment... => Copying

    current project directory to temporary build area: /tmp/ jets/demo/app_root => Setting up a vendored copy of ruby. => Replacing compiled gems with AWS Lambda Linux compiled versions. Deploying CloudFormation stack with jets app! Uploading /tmp/jets/demo/code/code-7169d0ac.zip (88.8 MB) to S3 Time to upload code to s3: 1s 02:08:20AM UPDATE_IN_PROGRESS AWS::CloudFormation::Stack demo-dev User Initiated ... 02:08:48AM CREATE_IN_PROGRESS AWS::CloudFormation::Stack PostsController 02:10:03AM UPDATE_COMPLETE AWS::CloudFormation::Stack demo-dev Stack success status: UPDATE_COMPLETE Time took for stack deployment: 1m 46s. Prewarming application... API Gateway Endpoint: https://xxxxxxxxxx.execute-api.us- west-2.amazonaws.com/dev/
  36. ίʔυ͸ඞཁͳ෺Λ ·ͱΊͯLambdaʹ Ξοϓϩʔυͯ͘͠ΕΔ

  37. CloudFormationͷ ςϯϓϨʔτΛੜ੒ͯ͠ ֤छϦιʔεΛ࡞ͬͯ͘ΕΔ

  38. API Gateway͔Β Lambdaͷܨ͗͜Έ΋ ࣗಈͰ΍ͬͯ͘ΕΔ

  39. αϯϓϧͰݟͤͨ ίʔυͩͱҎԼͷΑ͏ͳ Ϧιʔε͕ࣗಈੜ੒͞ΕΔ

  40. Controller͸ Functionʹ

  41. class PostsController < ApplicationController def index # renders Lambda Proxy

    structure compatible with # API Gateway render json: {hello: "world", action: "index"} end def show # params available id = params[:id] # puts goes to the lambda logs puts event # raw lambda event available render json: {action: "show", id: id} end … end
  42. None
  43. Route͸ API Gatewayʹ

  44. Jets.application.routes.draw do get "posts", to: "posts#index" get “posts/new", to: "posts#new"

    get “posts/:id", to: "posts#show" post "posts", to: "posts#create" get "posts/:id/edit", to: "posts#edit" put "posts", to: "posts#update" delete “posts", to: "posts#delete" … end
  45. None
  46. Job͸CloudWatch EventͳͲʹ

  47. class HardJob < ApplicationJob rate "10 hours" # every 10

    hours def dig puts "done digging" end … end
  48. None
  49. ΍Γ͍ͨॲཧΛॻ͚͹ Jets͕ࣗಈͰϦιʔεΛ ؅ཧͯ͘͠ΕΔ

  50. զʑ͕͢Δ͜ͱ͸ ͨͩίʔυΛॻ͍ͯ σϓϩΠ͢Δ͚ͩ

  51. ߏ੒ͱίʔυ͕ ៉ྷʹରԠͯ͠Δ͔Β ͱͯ΋ݟ௨͕͠ྑ͍

  52. ΋ͪΖΜLambdaͷ ϝϞϦ༰ྔͷࢦఆͱ͔΋ Ͱ͖ΔΑ

  53. JetsΛ࢖͏͜ͱͰ ServerlessͷϦιʔε؅ཧ ͔Β΋։์͞ΕΔ

  54. ·ͱΊ

  55. ࠓ೔࿩ͨ͜͠ͱ ‣Serverlessͱ͸ ‣Jetsͱ͸Կ͔ ‣JetsͰͷServerlessΞϓϦͷ࡞Γํ ‣JetsΛ࢖͏ͱԿ͕خ͍͠ͷ͔

  56. ͓·͚

  57. Jetsͷ࡞ऀ͸ αϙʔτ΋खް͍

  58. haml͍ͬͯ͏ͷ ࢖͍͍ͨͰ͢

  59. ࢖͑ΔΑ͏ʹ ͨ͠Αʂ

  60. ฦ৴΋ૣ͍͠ஸॏͩ͠ ײܹ͔͠͠ͳ͍

  61. ͥͻօ͞Μ΋JetsΛ ࢖ͬͯServerlessͷ ੈքΛָ͠΋͏ʂ

  62. Jets͕ؾʹͳͬͨํ͸ ͥͻҎԼͷURLΛʂ https://rubyonjets.com API Ϟʔυͱ͔ IAM पΓͱ͔ ϑΝΠϧͷ Ξοϓϩʔυͱ͔

  63. ͝ਗ਼ௌ͋Γ͕ͱ͏ ͍͟͝·ͨ͠ʂ