Jets ~Rubyで始めるServerless生活~

Jets ~Rubyで始めるServerless生活~

JAWS-UG Okayama 2019 で使った資料です。

19675bc717632ffd7c53aa8b9a9c3cbf?s=128

Hirokatsu Endo

August 17, 2019
Tweet

Transcript

  1. Jets ~ RubyͰ࢝ΊΔServerlessੜ׆ ~ 2019/08/17 JAWS-UG Okayama 2019

  2. ࣗݾ঺հ ‣ԕ౻ େհ / @ruzia ‣౦ژ͔Β༡ͼʹ͖·ͨ͠ ‣גࣜձࣾ SonicGarden ‣ΠϯϑϥपΓͱ͔େن໛ ͱ͔ߴෛՙͱ͔େ޷͖

    ‣ϩʔυόΠΫ΋ϋϚΓத
  3. ೲ඼ͷͳ͍डୗ։ൃ Ϗδωεͷ੒௕Λࢧ͑Διϑτ΢ΣΞΛ ֹ݄ఆֹͷސ໰ελΠϧͰఏڙ એ ఻ ͩ Α ʂ

  4. ιϑτ΢ΣΞ։ൃ๊͕͍͑ͯΔ՝୊ ‣ϢʔβχʔζʹԠͯ͡ɺܧଓతʹόʔδϣϯ
 Ξοϓ͍ͨ͠ ‣౤ࢿ͸খ࢝͘͞Ίͯɺࣄۀن໛ʹԠͯ͡
 ֦େ͍͖͍ͯͨ͠ ‣ۀ຿ͷվળʹ͋Θͤͯɺܧଓతʹόʔδϣϯ
 Ξοϓ͍ͨ͠ Ϗδωεͷ՝୊

  5. ιϑτ΢ΣΞ։ൃ๊͕͍͑ͯΔ՝୊ ‣಺੡ɿΤϯδχΞͷ࠾༻ɺ૊৫ͮ͘Γɺ
 ڭҭධՁ͕೉͍͠ ‣֎஫ɿ֎ʹཔΉʹ͸
 ʮԿΛ࡞Δͷ͔ʹཁ݅ఆٛʯ͕೉͍͠ ͜Ε·Ͱͷ໰୊ ࠾༻ɾҰׅൃ஫Ͱͳ͍खஈ͸ʁ

  6. ʮೲ඼ͷͳ͍डୗ։ൃʯαʔϏε֓ཁ ʮֹ݄ఆֹʯͰֹۚͷݟੵΓ͸͋Γ·ͤΜ Ͱ͖ΔൣғͰʮ։ൃͱӡ༻ʯ͕ͣͬͱଓ͖·͢ หޢ࢜΍੫ཧ࢜ͷΑ͏ͳʮސ໰ʯͷΑ͏Ͱ͋Γɺ ·ΔͰʮ༏लͳνʔϜʯ͕͍ΔΑ͏ͳ΋ͷͰ͢ ͨͩ͠ɺಇ࣌ؒ͘Ͱ͸ͳ͘ʮ੒ՌʯͰܖ໿͠·͢

  7. ʮೲ඼ͷͳ͍डୗ։ൃʯαʔϏε֓ཁ ֹ݄ఆֹʴ੒Ռܖ໿ʹ
 ೲ඼ͷͳ͍डୗ։ൃ

  8. TV Ͱ ͨ Α ʂ

  9. https://www.sonicgarden.jp Ұॹʹಇ͖͍ͨํ΋
 ืूதͰ͢ʂ

  10. ຊ೔ͷ͓୊໨΍Β ‣RubyͱServerlessɺ૬ੑ͍͍Αʂ ‣30෼͔͠ͳ͍ͷͰɺ޿͘ઙ͘࿩͢Α ‣ϑΟʔυόοΫ͸ #jawsug #jawsoka ·Ͱ

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

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

  13. ͱ͸͍͑ɾɾɾ

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

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

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

  17. ʘͦΜͳ͋ͳͨʹʗ

  18. Ruby on Jets

  19. Q Jetsͬͯͳʹʁ

  20. A “The Ruby Serverless Framework”

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

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

  23. API Gateway Lambda Data Store S3 CloudWatch Event

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

  25. Controllers

  26. 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
  27. Routes

  28. 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
  29. Jobs

  30. 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
  31. ΊͬͪΌγϯϓϧ͡Ό ͳ͍Ͱ͔͢ʁ

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

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

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

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

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

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

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

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

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

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

  42. $ 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/
  43. ίʔυ͸ඞཁͳ෺Λ ·ͱΊͯLambdaʹ Ξοϓϩʔυͯ͘͠ΕΔ

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

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

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

  47. Controller͸ Functionʹ

  48. 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
  49. None
  50. Route͸ API Gatewayʹ

  51. 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
  52. None
  53. Job͸CloudWatch EventͳͲʹ

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

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

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

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

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

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

  61. ·ͱΊ

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

  63. ͓·͚

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

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

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

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

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

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

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

  71. ࣭ٙԠ౴ λΠϜ ͕࣌ؒ͋Ε͹