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
Y_uuu
January 29, 2020
Programming
920
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
More Decks by Y_uuu
See All by Y_uuu
PicoRuby for IoT: Connecting to the Cloud with MQTT
yuuu
2
940
Porting filesystem-fat to Another Microcontroller: ESP32
yuuu
2
440
Porting PicoRuby to Another Microcontroller: ESP32
yuuu
4
1.2k
RubyKaigi 2025でプロポーザルが初めて採択されるまでにやったこと
yuuu
1
810
地方企業の強みを活かす!AWSを活用したIoTシステムの開発事例とその裏側
yuuu
0
200
あの重機は今どこにある?- 重機管理IoTプロダクトのバックエンドをAWSで構築した話 -
yuuu
0
1.1k
mruby-esp32におけるペリフェラルAPIの実装検討
yuuu
1
930
育成力 - エンジニアの才能を引き出す環境とチューターの立ち回り -
yuuu
4
5.3k
committee-railsとOpenAPI Generatorでスキーマ駆動なAPI開発をする
yuuu
0
1.1k
Other Decks in Programming
See All in Programming
AIエージェント時代のコードレビューを設計する
nogu66
6
2.6k
XHTMLが残したもの
yosuke_furukawa
PRO
2
710
数年滞っていたダークモード対応をおよそ2週間で完了させる
chigichan24
0
700
20260828_品質と開発生産性を両立させる、AI時代のE2Eテストの考え方
magicpod
0
200
Foundry Localでエージェント開発
seosoft
0
180
スマート反転とウェブアクセシビリティ
camiha
0
200
AIとGame Jamで、ゲームを完成させた話
takahirosaeki
0
120
Kiroで創り、AgentCoreで繋ぐ!AWSで実践する「AI-DLC」から「AIエージェント統合」までの最新地図
licux
4
660
AGENTS.md Is Not Enough:Build Skills, Don't Download Them
lx_t
0
110
新人はどこまで自力でやり、どこからAIに頼るべきか/エンジニア育成に向き合う_先輩たちの悩みと知見共有会
toppan_digital_dev
1
610
AWS DevOps Agentで インシデント対応をAIに任せたい
honmarkhunt
7
2.8k
Heart of Swift Concurrency
koher
0
330
Featured
See All Featured
Designing Experiences People Love
moore
143
24k
Stewardship and Sustainability of Urban and Community Forests
pwiseman
0
520
Navigating the moral maze — ethical principles for Al-driven product design
skipperchong
2
530
First, design no harm
axbom
PRO
2
1.3k
Building Adaptive Systems
keathley
44
3.2k
Easily Structure & Communicate Ideas using Wireframe
afnizarnur
194
17k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
10
1.3k
Designing for Timeless Needs
cassininazir
1
470
Beyond borders and beyond the search box: How to win the global "messy middle" with AI-driven SEO
davidcarrasco
3
240
Into the Great Unknown - MozCon
thekraken
41
2.7k
Large-scale JavaScript Application Architecture
addyosmani
515
110k
Mind Mapping
helmedeiros
1
350
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は技術が好きなエンジニアを 募集しています