Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Jets ~Rubyで始めるServerless生活~

Jets ~Rubyで始めるServerless生活~

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

Hirokatsu Endo

August 17, 2019
Tweet

More Decks by Hirokatsu Endo

Other Decks in Programming

Transcript

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

    View Slide

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

    View Slide

  3. ೲ඼ͷͳ͍डୗ։ൃ
    Ϗδωεͷ੒௕Λࢧ͑Διϑτ΢ΣΞΛ
    ֹ݄ఆֹͷސ໰ελΠϧͰఏڙ


    ͩ
    Α
    ʂ

    View Slide

  4. ιϑτ΢ΣΞ։ൃ๊͕͍͑ͯΔ՝୊
    ‣ϢʔβχʔζʹԠͯ͡ɺܧଓతʹόʔδϣϯ

    Ξοϓ͍ͨ͠
    ‣౤ࢿ͸খ࢝͘͞Ίͯɺࣄۀن໛ʹԠͯ͡

    ֦େ͍͖͍ͯͨ͠
    ‣ۀ຿ͷվળʹ͋Θͤͯɺܧଓతʹόʔδϣϯ

    Ξοϓ͍ͨ͠
    Ϗδωεͷ՝୊

    View Slide

  5. ιϑτ΢ΣΞ։ൃ๊͕͍͑ͯΔ՝୊
    ‣಺੡ɿΤϯδχΞͷ࠾༻ɺ૊৫ͮ͘Γɺ

    ڭҭධՁ͕೉͍͠
    ‣֎஫ɿ֎ʹཔΉʹ͸

    ʮԿΛ࡞Δͷ͔ʹཁ݅ఆٛʯ͕೉͍͠
    ͜Ε·Ͱͷ໰୊
    ࠾༻ɾҰׅൃ஫Ͱͳ͍खஈ͸ʁ

    View Slide

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

    View Slide

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

    ೲ඼ͷͳ͍डୗ։ൃ

    View Slide

  8. TV
    Ͱ
    ͨ
    Α
    ʂ

    View Slide

  9. https://www.sonicgarden.jp
    Ұॹʹಇ͖͍ͨํ΋

    ืूதͰ͢ʂ

    View Slide

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

    View Slide

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

    View Slide

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

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

    View Slide

  13. ͱ͸͍͑ɾɾɾ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  17. ʘͦΜͳ͋ͳͨʹʗ

    View Slide

  18. Ruby on Jets

    View Slide

  19. Q
    Jetsͬͯͳʹʁ

    View Slide

  20. A
    “The Ruby Serverless
    Framework”

    View Slide

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

    View Slide

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

    View Slide

  23. API
    Gateway
    Lambda
    Data Store
    S3
    CloudWatch
    Event

    View Slide

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

    View Slide

  25. Controllers

    View Slide

  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

    View Slide

  27. Routes

    View Slide

  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

    View Slide

  29. Jobs

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    end

    View Slide

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

    View Slide

  36. 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

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  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/

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  47. Controller͸
    Functionʹ

    View Slide

  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

    View Slide

  49. View Slide

  50. Route͸
    API Gatewayʹ

    View Slide

  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

    View Slide

  52. View Slide

  53. Job͸CloudWatch
    EventͳͲʹ

    View Slide

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

    end

    View Slide

  55. View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  61. ·ͱΊ

    View Slide

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

    View Slide

  63. ͓·͚

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide