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
CognitoのLambdaトリガーで認証処理をカスタマイズ!
Search
sasayan
August 06, 2026
51
2
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
CognitoのLambdaトリガーで認証処理をカスタマイズ!
sasayan
August 06, 2026
More Decks by sasayan
See All by sasayan
Lambda共通処理の管理
sasayan
0
0
Cognitoのカスタム認証とAmazon Rekognitionで顔認証をやってみた!
sasayan
0
11
はじめてのくらうどりふとみつもり
sasayan
0
23
Kiroを使って「仕様」から「デプロイ」まで丸ごとやらせてみた
sasayan
1
87
オーソライザーを用いた存在証明
sasayan
0
51
Featured
See All Featured
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
31
2.9k
The Limits of Empathy - UXLibs8
cassininazir
2
730
How to build a perfect <img>
jonoalderson
1
6.1k
Intergalactic Javascript Robots from Outer Space
tanoku
273
28k
YesSQL, Process and Tooling at Scale
rocio
174
15k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
730
Designing for Timeless Needs
cassininazir
1
530
Done Done
chrislema
187
17k
Chasing Engaging Ingredients in Design
codingconduct
0
370
Navigating Weather and Climate Data
rabernat
0
560
What the history of the web can teach us about the future of AI
inesmontani
PRO
2
740
Claude Code どこまでも/ Claude Code Everywhere
nwiizo
68
59k
Transcript
CognitoのLambdaトリガーで 認証処理をカスタマイズ! 2026/08/06 ささやん
自己紹介 ささやん(佐々谷 厚生) ・3年目 ・バックエンドエンジニア、AI開発推進 ・2026 Japan AWS Jr.Champions
話すこと ・Lambdaトリガーでできる事 ・ユースケース ・実装方法 ※イメージです
背景 • サーバーレスWebアプリで、Cognitoによる認証・認可 を実装 • Lambdaトリガーは知っていたが、期間・工数の都合で 標準機能のみを利用 • 次回の設計をスムーズにするため、認証処理をどこまで カスタマイズできるか調査(楽できる部分は楽したい)
Lambdaトリガーとは Cognitoの認証処理タイミングでLambdaを自動実行し、 標準の認証処理をカスタマイズする仕組み 情報渡すんで、、 支援してほしいっす うい
Lambdaトリガーとは トリガー名 Pre sign-up Post confirmation Pre authentication Post authentication
Define auth challenge Create auth challenge Verify auth challenge response Pre token generation User migration Custom message Custom email sender Custom SMS sender Inbound federation 概要 ユーザー登録の直前に、登録可否の判定や自動確認を行う ユーザー登録やパスワードリセットの確認完了後に処理を行う 認証処理の直前に、ログイン可否を判定する 認証成功後に、ログ記録や外部システム連携を行う カスタム認証で、次に実行する認証チャレンジを決定する カスタム認証で、質問や認証コードなどのチャレンジを生成する カスタム認証で、ユーザーの回答が正しいか判定する トークン発行前に、ID・アクセストークンのクレームを編集する ログイン時などに、既存のユーザー管理基盤からユーザーを移行する Cognitoが送信するメールやSMSの件名・本文を変更する Cognitoの代わりに、Lambdaからメールを送信する Cognitoの代わりに、LambdaからSMSを送信する 外部IdPから受け取ったユーザー属性を、Cognitoへの反映前に変換する
Lambdaトリガーとは トリガー名 Pre sign-up Post confirmation Pre authentication Post authentication
Define auth challenge Create auth challenge Verify auth challenge response Pre token generation User migration Custom message Custom email sender Custom SMS sender Inbound federation 概要 ユーザー登録の直前に、登録可否の判定や自動確認を行う ユーザー登録やパスワードリセットの確認完了後に処理を行う 認証処理の直前に、ログイン可否を判定する 認証成功後に、ログ記録や外部システム連携を行う カスタム認証で、次に実行する認証チャレンジを決定する カスタム認証で、質問や認証コードなどのチャレンジを生成する カスタム認証で、ユーザーの回答が正しいか判定する トークン発行前に、ID・アクセストークンのクレームを編集する ログイン時などに、既存のユーザー管理基盤からユーザーを移行する Cognitoが送信するメールやSMSの件名・本文を変更する Cognitoの代わりに、Lambdaからメールを送信する Cognitoの代わりに、LambdaからSMSを送信する 外部IdPから受け取ったユーザー属性を、Cognitoへの反映前に変換する
Lambdaトリガーとは
Pre sign-up ユーザー登録の直前に、登録可否の判定や自動確認を行う ➀特定のドメインのみ登録許可する 社内メールアドレスのみ許可し、それ以外は拒否する def lambda_handler(event, context): email =
event["request"]["userAttributes"].get("email", "") if not email.endswith("@example.co.jp"): raise Exception("会社のメールアドレスを使用してください")
Pre sign-up ②ValidationDataが利用できる サインアップ処理中のみ利用できる一時情報の参照 例:招待コードを用いたサインアップ def lambda_handler(event, context): code =
event["request"]["validationData"].get("code", "") if not isinstance (code, 123456): raise Exception("招待コードを入力して下さい")
Post confirmation ユーザー登録完了後に処理を行う(ユーザーの操作に依存しない) ・SQS経由で業務用DBに登録 def lambda_handler(event, context): attributes = event["request"]["userAttributes"]
sqs.send_message( QueueUrl=os.environ["QUEUE_URL"], MessageBody=json.dumps({ "userId": attributes["sub"], "email": attributes.get("email") }) ) Lambdaトリガーは5秒以内 に応答する必要あり。
Pre authentication 認証処理の直前に、ログイン可否を判定する ・ステータスによるログイン可否判定 def lambda_handler(event, context): user_id = event["request"]["userAttributes"]["sub"]
result = table.get_item( Key={"userId": user_id} ) user = result.get("Item") if not user or user.get("status") == "STOP": raise Exception("このユーザーは利用停止中です")
Pre token generation トークン発行前に、ID・アクセストークンのクレームを編集する ➀IDトークンに所属企業IDを追加し、その情報をもとにAPに反映 def lambda_handler(event, context): event["response"]["claimsAndScopeOverrideDetails"] =
{ "idTokenGeneration": { "claimsToAddOrOverride": { "tenant_id": "company-a", } } }
Pre token generation ②アクセストークンのスコープを追加・削除し、細かい認可を行う 例:担当者は参照のみ、リーダーは編集可能 def lambda_handler(event, context): role =
event["request"]["userAttributes"].get( "custom:role", "maintenance" ) scopes_to_add = ["system.read"] scopes_to_suppress = [] if role == "leader": scopes_to_add.append("system.write") else: scopes_to_suppress.append("system.write") event["response"]["claimsAndScopeOverrideDetails"] = { "accessTokenGeneration": { "claimsToAddOrOverride": { "application_role": role }, "scopesToAdd": scopes_to_add, "scopesToSuppress": scopes_to_suppress } }
Custom message Cognitoが送信するメールやSMSの件名・本文を変更する ・言語、ユーザー属性などに応じて内容を動的に変更 def lambda_handler(event, context): code = event["request"]["codeParameter"]
language = event["request"]["userAttributes"].get( "custom:language", "ja" ) 同一トリガーで複数の利用ケース if event["triggerSource"] == "CustomMessage_SignUp": がある場合、条件分岐を行う if language == "en": event["response"]["emailSubject"] = ¥ "Confirm your account" event["response"]["emailMessage"] = ¥ f"Your confirmation code is {code}." else: event["response"]["emailSubject"] = ¥ "アカウント登録の確認" event["response"]["emailMessage"] = ¥ f"確認コードは {code} です。"
まとめ ・CognitoはLambdaトリガーと組み合わせることで、 業務要件に合わせた認証・認可基盤を構築できる ・同一トリガーで複数の利用用途がある場合、 条件分岐を行う ・Lambdaトリガーは5秒以内に応答しないといけない