Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Ruby on Jets + Cognitoで認証付きAPIサーバを5分でデプロイする
Search
Y_uuu
January 29, 2020
Programming
890
1
Share
Ruby on Jets + Cognitoで認証付きAPIサーバを5分でデプロイする
2019/01/29 Fukuoka.rb #167 - 新年LT大会!
Y_uuu
January 29, 2020
More Decks by Y_uuu
See All by Y_uuu
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
620
Porting filesystem-fat to Another Microcontroller: ESP32
yuuu
2
310
Porting PicoRuby to Another Microcontroller: ESP32
yuuu
4
1k
RubyKaigi 2025でプロポーザルが初めて採択されるまでにやったこと
yuuu
1
690
地方企業の強みを活かす!AWSを活用したIoTシステムの開発事例とその裏側
yuuu
0
170
あの重機は今どこにある?- 重機管理IoTプロダクトのバックエンドをAWSで構築した話 -
yuuu
0
1.1k
mruby-esp32におけるペリフェラルAPIの実装検討
yuuu
1
870
育成力 - エンジニアの才能を引き出す環境とチューターの立ち回り -
yuuu
4
5k
committee-railsとOpenAPI Generatorでスキーマ駆動なAPI開発をする
yuuu
0
1k
Other Decks in Programming
See All in Programming
Back to the roots of date
jinroq
0
250
ふりがな Deep Dive try! Swift Tokyo 2026
watura
0
230
Coding as Prompting Since 2025
ragingwind
0
840
Offline should be the norm: building local-first apps with CRDTs & Kotlin Multiplatform
renaudmathieu
0
220
瑠璃の宝石に学ぶ技術の声の聴き方 / 【劇場版】アニメから得た学びを発表会2026 #エンジニアニメ
mazrean
0
260
GoogleCloudとterraform完全に理解した
terisuke
1
100
Kingdom of the Machine
yui_knk
2
380
The Less-Told Story of Socket Timeouts
coe401_
3
430
AI時代のエンジニアリングの原則 / Engineering Principles in the AI Era
haru860
0
480
YJITとZJITにはイカなる違いがあるのか?
nakiym
0
220
t *testing.T は どこからやってくるの?
otakakot
1
690
tRPCの概要と少しだけパフォーマンス
misoton665
2
220
Featured
See All Featured
The State of eCommerce SEO: How to Win in Today's Products SERPs - #SEOweek
aleyda
2
10k
Impact Scores and Hybrid Strategies: The future of link building
tamaranovitovic
0
260
Typedesign – Prime Four
hannesfritz
42
3k
Why You Should Never Use an ORM
jnunemaker
PRO
61
9.8k
The Invisible Side of Design
smashingmag
303
52k
Digital Ethics as a Driver of Design Innovation
axbom
PRO
1
260
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
180
What’s in a name? Adding method to the madness
productmarketing
PRO
24
4k
Building Adaptive Systems
keathley
44
3k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
270
Designing for Timeless Needs
cassininazir
0
200
Transcript
Ruby on Jets + Cognitoで 認証付きAPIサーバを5分でデプロイする 2019/01/29 Fukuoka.rb #167 -
新年LT⼤会︕ 株式会社Fusic 岡嵜雄平
͚͓͋Ί
⾃⼰紹介 岡嵜 雄平(@Y_uuu) RubyKaigi 2019 ローカルオーガナイザー 今年の⽬標︓Ruby on Jetsをガンガン使う。
⾃⼰紹介
最近のお仕事 • IoTサーバへデバイスの代わりにデータ送信 • さまざまな⽅法でテストデータを⾃動⽣成 • 各種クラウドサービス(AWS, GCP, Azure)やSORACOMと連携
IoTのテストを⽀援する mockmock https://bit.ly/37tA9bf で検索
Ruby on Jetsとは AWS上で動作するサーバレスアプリケーションを Rubyで書くためのフレームワーク
Ruby on Jetsとは • ORMがRailsと同じActiveRecord • MVCやルーティングの書き⽅(メソッド)が同じ • ⼀部のGemが使える
Ruby on Railsと同じように実装できる
Ruby on Jetsとは 1コマンドでAWSへデプロイできる
今⽇紹介すること サーバレスな認証付きAPIサーバを 5分で作ってデプロイする⽅法
仕様 • Fukuoka.rb API • 開催⽇時、connpassのURLを登録・取得
-FUbTUSZ
Jetsのインストール アプリケーションを作成する ❯ ruby -v ruby 2.5.7p206 (2019-10-01 revision
67816) [x86_64-darwin19] ❯ gem install jets Successfully installed jets-2.3.12 ❯ jets new fukuoka_rb_api --mode api --no-database Congrats You have successfully created a Jets project.
jets generate scaffold Scaffoldする ❯ cd fukuoka_rb_api ❯ jets
generate scaffold Ev title:string url:string --no- migration
΄΅3BJMTͱಉ͡
DynamoDBに対応(モデル) デフォルトRDB向けの実装になっているので修正 class Ev < ApplicationItem #ApplicationRecord column :id,
:title, :url # 追記 end
DynamoDBに対応(コントローラ) デフォルトRDB向けの実装になっているので修正 def index @evs = Ev.scan #Ev.all render
json: @evs end
DynamoDBに対応(コントローラ) デフォルトRDB向けの実装になっているので修正 def create @ev = Ev.new #Ev.new(ev_params) if
@ev.replace(ev_params) #ev.save render json: @ev, status: :created else render json: @ev.errors, status: :unprocessable_entity end end
DynamoDBに対応(Config) IAM PolicyにDynamoDBを追加 Jets.application.configure do # 省略 config.iam_policy =
[ʻdynamodbʼ] end
jets dynamodb:migrate マイグレーションする ❯ docker run -p 8000:8000 amazon/dynamodb-local
❯ jets dynamodb:generate evs ❯ jets dynamodb:migrate dynamodb/migrate/*
jets server ローカルサーバを起動 ❯ jets server
イベントを登録してみる JSONをPOST ❯ curl -X POST -H "Content-Type: application/json"
-d '{"ev": {"title":" Fukuoka.rb #167 - 新年LT⼤会︕", "url":"https://fukuokarb.connpass.com/event/162983/"}}' http://localhost:8888/evs {"id":"f97b50fd7d6330728ea3eea61ef6889a5035e2bb","title" :" Fukuoka.rb #167 - 新年LT⼤会︕ ","url":"https://fukuokarb.connpass.com/event/162983/","cre ated_at":"2020-01-25T19:41:00Z","updated_at":"2020-01- 25T19:41:00Z"}%
イベントを取得してみる JSONをGET ❯ curl http://localhost:8888/evs [{"created_at":"2020-01- 25T19:41:00Z","id":"f97b50fd7d6330728ea3eea61ef6889a50 35e2bb","title":" Fukuoka.rb
#167 - 新年LT⼤会︕ ","updated_at":"2020-01- 25T19:41:00Z","url":"https://fukuokarb.connpass.com/event /162983/"}]
ϩʔΧϧڥͰಈ͍ͨ
Cognitoユーザープールを作成 詳細は以下を参照 チュートリアル: ユーザープールの作成 - Amazon Cognito https://docs.aws.amazon.com/ja_jp/cognito/latest/developerguide/ tutorial-create-user-pool.html
諸々デフォルトでOK
Cognitoアプリクライアントを作成 後で使うのでメモ チェック
Authorizerを作成 app/authorizers/main_authorizer.rbを作成 class MainAuthorizer < Jets::Authorizer::Base authorizer( name: ʻMyCognitoʼ,
identity_source: ʻAuthorizationʼ, type: :cognito_user_pools, provider_arns: [ʻ(CognitoのARNを記載)'], ) end
Authorizerを有効にする config/routes.rbに追記 resources :evs, authorizer: ʻmain#my_cognitoʼ
デプロイ AWSでCloudFormationが⾛る ❯ JETS_ENV=production jets dynamodb:migrate dynamodb/migrate/* ❯ jets
deploy production
JWT Tokenを取得 aws-cliでcognitoにログイン ❯ aws cognito-idp initiate-auth --auth-flow USER_PASSWORD_AUTH
--client-id 6nvis8bu0ehri8tsk6d3qkimef --auth-parameters USERNAME=(ユーザ名),PASSWORD=(パスワード) {"ChallengeParameters": {}, "AuthenticationResult": {”IdToken": ”xxxxx",
イベントを登録してみる JSONをPOST ❯ curl -X POST -H "Authorization: xxxxx"
-H "Content-Type: application/json" -d '{"ev": {"title":" Fukuoka.rb #167 - 新年LT⼤ 会︕", "url":"https://fukuokarb.connpass.com/event/162983/"}}' https://z6kpk9boza.execute-api.ap-northeast- 1.amazonaws.com/prod/evs {"id":"1fdb21ed1bd9ca05b36b4f5a16fa7acda3ee077e","title":" Fukuoka.rb #167 - 新年LT⼤会︕ ","url":"https://fukuokarb.connpass.com/event/162983/","create d_at":"2020-01-25T20:46:45Z","updated_at":"2020-01- 25T20:46:45Z"}%
イベントを取得してみる JSONをGET ❯ curl -H "Authorization: xxxxx" https://z6kpk9boza.execute- api.ap-northeast-1.amazonaws.com/prod/evs
[{"updated_at":"2020-01- 25T20:48:20Z","created_at":"2020-01- 25T20:48:20Z","id":"14ff588afe141e6121238214725d0e3db 51c9eca","url":"https://fukuokarb.connpass.com/event/1629 83/","title":" Fukuoka.rb #167 - 新年LT⼤会︕"}]%
まとめ • Ruby on Jetsを使ってAPIサーバを簡単に構築できた • Cognitoを使って簡単に認証機能を実現できた • RubyはRailsだけじゃない、まだまだ死なない︕
Thank you! Fusicは技術が好きなエンジニアを 募集しています