Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

TV Ͱ ͨ Α ʂ

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

ͱ͸͍͑ɾɾɾ

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

ʘͦΜͳ͋ͳͨʹʗ

Slide 18

Slide 18 text

Ruby on Jets

Slide 19

Slide 19 text

Q Jetsͬͯͳʹʁ

Slide 20

Slide 20 text

A “The Ruby Serverless Framework”

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

API Gateway Lambda Data Store S3 CloudWatch Event

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

Controllers

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

Routes

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

Jobs

Slide 30

Slide 30 text

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

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

$ 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/

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

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

Slide 45

Slide 45 text

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

Slide 46

Slide 46 text

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

Slide 47

Slide 47 text

Controller͸ Functionʹ

Slide 48

Slide 48 text

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

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

Route͸ API Gatewayʹ

Slide 51

Slide 51 text

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

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

Job͸CloudWatch EventͳͲʹ

Slide 54

Slide 54 text

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

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

·ͱΊ

Slide 62

Slide 62 text

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

Slide 63

Slide 63 text

͓·͚

Slide 64

Slide 64 text

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

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

࢖͑ΔΑ͏ʹ ͨ͠Αʂ

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

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

Slide 69

Slide 69 text

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

Slide 70

Slide 70 text

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

Slide 71

Slide 71 text

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