$30 off During Our Annual Pro Sale. View Details »

Jets ~ Rubyで始めるServerless生活 ~

Jets ~ Rubyで始めるServerless生活 ~

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

Hirokatsu Endo

November 28, 2019
Tweet

More Decks by Hirokatsu Endo

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  5. Serverlessͱ͸ʁ
    ‣αʔόͷߏஙɾӡ༻͕ඞཁͳ͍
    ‣՝ۚ͸αʔόͷىಈ࣌ؒͰ͸ͳ͘ɺ

    ϓϩάϥϜͷ࣮ߦ࣌ؒʹରͯ͠ߦΘΕΔ
    ‣ϓϩάϥϜͷ࣮ߦ؀ڥ͸ϑϧϚωʔδυ

    View Slide

  6. ͱ͸͍͑ɾɾɾ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  10. ʘͦΜͳ͋ͳͨʹʗ

    View Slide

  11. Ruby on Jets

    View Slide

  12. Q
    Jetsͬͯͳʹʁ

    View Slide

  13. A
    “The Ruby Serverless
    Framework”

    View Slide

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

    View Slide

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

    View Slide

  16. API
    Gateway
    Lambda
    Data Store
    S3
    CloudWatch
    Event

    View Slide

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

    View Slide

  18. Controllers

    View Slide

  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

    View Slide

  20. Routes

    View Slide

  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

    View Slide

  22. Jobs

    View Slide

  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

    View Slide

  24. ΊͬͪΌγϯϓϧ͡Ό
    ͳ͍Ͱ͔͢ʁ

    View Slide

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

    View Slide

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

    View Slide

  27. class ThermostatJob < ApplicationJob
    iot_event "SELECT * FROM 'my/topic'"
    def measure
    puts "event #{JSON.dump(event)}"
    end

    end

    View Slide

  28. ྫ͑͹Kinesisͱ
    ࿈ܞ͍ͤͨ͞৔߹

    View Slide

  29. class DataJob < ApplicationJob
    kinesis_event "my-stream"
    def file
    puts "event #{JSON.dump(event)}"
    puts "kinesis_data #{JSON.dump(kinesis_data)}"
    end

    end

    View Slide

  30. ΊͬͪΌγϯϓϧ͡Ό
    ͳ͍Ͱ͔͢ʁ (2ճ໨

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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/

    View Slide

  36. ίʔυ͸ඞཁͳ෺Λ
    ·ͱΊͯLambdaʹ
    Ξοϓϩʔυͯ͘͠ΕΔ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  40. Controller͸
    Functionʹ

    View Slide

  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

    View Slide

  42. View Slide

  43. Route͸
    API Gatewayʹ

    View Slide

  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

    View Slide

  45. View Slide

  46. Job͸CloudWatch
    EventͳͲʹ

    View Slide

  47. class HardJob < ApplicationJob
    rate "10 hours" # every 10 hours
    def dig
    puts "done digging"
    end

    end

    View Slide

  48. View Slide

  49. ΍Γ͍ͨॲཧΛॻ͚͹
    Jets͕ࣗಈͰϦιʔεΛ
    ؅ཧͯ͘͠ΕΔ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  54. ·ͱΊ

    View Slide

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

    View Slide

  56. ͓·͚

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide