Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
Ruby on Jets + Cognitoで認証付きAPIサーバを5分でデプロイする
Search
Sponsored
·
Your Podcast. Everywhere. Effortlessly.
Share. Educate. Inspire. Entertain. You do you. We'll handle the rest.
→
Y_uuu
January 29, 2020
Programming
930
1
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
Ruby on Jets + Cognitoで認証付きAPIサーバを5分でデプロイする
2019/01/29 Fukuoka.rb #167 - 新年LT大会!
Y_uuu
January 29, 2020
Transcript
33 slides
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は技術が好きなエンジニアを 募集しています
More Decks by Y_uuu
See All by Y_uuu
Growing PicoRuby's Ecosystem on ESP32
yuuu
2
150
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
1.1k
Porting filesystem-fat to Another Microcontroller: ESP32
yuuu
2
490
Porting PicoRuby to Another Microcontroller: ESP32
yuuu
4
1.2k
RubyKaigi 2025でプロポーザルが初めて採択されるまでにやったこと
yuuu
1
840
地方企業の強みを活かす!AWSを活用したIoTシステムの開発事例とその裏側
yuuu
0
210
あの重機は今どこにある?- 重機管理IoTプロダクトのバックエンドをAWSで構築した話 -
yuuu
0
1.1k
mruby-esp32におけるペリフェラルAPIの実装検討
yuuu
1
950
育成力 - エンジニアの才能を引き出す環境とチューターの立ち回り -
yuuu
4
5.3k
Other Decks in Programming
See All in Programming
[DroidKnights 2026] Workflow AX: From Auto-Documented Policies to E2E Testing
raeminkang
0
600
Beyond Chatbots: Agentic UI with Open Standards @W-JAX 2026, München
manfredsteyer
PRO
0
120
AI時代のFlutter品質設計 — アプリ品質を守るQuality Layers
omatty198
0
390
AI時代のIaC入門 〜なぜインフラをコードで管理したいのか?〜 / Introduction to IaC in the AI Era
akihisaikeda
6
1.6k
React Native経験者が Flutterを30⽇間触って驚いたこと
3saito
0
200
Vapor Mode でアクセシビリティは 壊れないか検証した話
fuqda
1
350
Web3Dライブラリを作ってVue Custom Rendererの仕組みを理解しよう / lets learn vue custom renderer by making 3d library
drumath2237
1
140
作れるエンジニアから、価値を届けるエンジニアへ
ryunakayama
1
310
Kotlin JVM用に作られたライブラリのKMPを前提とした再設計
jsoizo
0
220
例外からRich Errorsまで Kotlinエラーハンドリングの進化を読み解く
shiita0903
0
230
なぜ私たちは「現時点では」KMPの導入を見送ったのか
chigichan24
1
310
Claude Codeのおかげでやりたいことがやれるようになったんだよォー!
sms_tech
0
170
Featured
See All Featured
Why Mistakes Are the Best Teachers: Turning Failure into a Pathway for Growth
auna
0
360
We Are The Robots
honzajavorek
0
460
The Art of Programming - Codeland 2020
erikaheidi
57
15k
Visual Storytelling: How to be a Superhuman Communicator
reverentgeek
2
750
Avoiding the “Bad Training, Faster” Trap in the Age of AI
tmiket
1
310
The Curse of the Amulet
leimatthew05
4
17k
Primal Persuasion: How to Engage the Brain for Learning That Lasts
tmiket
0
520
How to Build an AI Search Optimization Roadmap - Criteria and Steps to Take #SEOIRL
aleyda
1
2.3k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
430
Context Engineering - Making Every Token Count
addyosmani
9
1.3k
Agile Actions for Facilitating Distributed Teams - ADO2019
mkilby
0
350
ラッコキーワード サービス紹介資料
rakko
5
6M