Amazon Cognito
アプリユーザーのログイン
エンドユーザーのサインアップ/サインイン(メール、Google、Facebook、SAML)を提供し、API 呼び出し用の JWT トークンを発行します。
- コスト$$ ふつう
- 難易度●●●●●
- 未使用でも課金?いいえ
- AWS マネージド
- 出題試験SAA · DVA
たとえるなら店舗のお客様向けの会員カード発行窓口のようなものです。
Cognito はシステムのどこにある?
Cognito を呼び出す
API Gatewayトークン検証
- ユーザーログイン
具体例でステップごとに
- 写真共有アプリを作っていて、サインアップ、サインイン、パスワードのリセット、「Google でサインイン」が必要です。
- Cognito のユーザープールを作り、MFA と Google ボタンを有効にします。サインイン画面も用意されています。
- サインインしたユーザーは JWT トークンを受け取り、API Gateway は API を呼ばせる前にトークンを確認します。
- ID プールがそのトークンを一時的な AWS 権限に交換するので、アプリは写真をユーザー専用の S3 フォルダに直接アップロードできます。
- パスワードを自分で保存したり、確認コードの仕組みを作ったりする必要はありません。
いつ使う?
✓ 使うべき場面
ユーザーアカウントが必要な Web/モバイルアプリ。
✗ 避けるべき場面
従業員が AWS にログインする → IAM Identity Center。
よくある使い方
- Web とモバイルアプリのサインアップ / サインイン
- Google、Facebook、Apple、または企業の SAML でログイン
- アプリが S3 に直接アクセスするための一時的な権限付与(identity pool)
Cognito・IAM Identity Center・IAM の違い
| 対象 | アプリのエンドユーザー | AWS アカウントにアクセスする従業員 | AWS アカウント内の権限 |
|---|---|---|---|
| サインイン方法 | メール、ソーシャル、SAML/OIDC | 社内ディレクトリ(SSO) | パスワード、アクセスキー、ロール |
| 規模 | 数百万人のユーザー | 社内の従業員 | 管理者とサービス |
| 料金 | 月間アクティブユーザー単位 | 無料 | 無料 |
料金はいくら?
- 課金方式: MAU(月間アクティブユーザー)単位で、料金は Lite / Essentials / Plus プランごとに異なります。SAML/OIDC でログインするユーザーは別料金です。
- 無料枠: 毎月最初の 10,000 MAU は無料(Lite/Essentials プラン)。
必要以上に上位のプランを選ぶ。OTP コードを送る SMS は別途課金。SAML 連携ユーザーは料金が高い。
ユーザープールはサインアップ/サインインを担いトークンを返し、ID プールは ID を一時的な AWS 認証情報に交換します。アクセスキーを埋め込まずにモバイルアプリから S3/DynamoDB を使うなら ID プールです。Cognito は API Gateway のオーソライザーとしても使えます。
本番の試験問題に挑戦
1A company designs a mobile app for its customers to upload photos to a website. The app needs a secure login with multi-factor authentication (MFA). The company wants to limit the initial build time and the maintenance of the solution. Which solution should a solutions architect recommend to meet these requirements?
- AUse Amazon Cognito user pools with SMS-based MFA.
- BEdit IAM policies to require MFA for all users.
- CFederate IAM against the corporate Active Directory that requires MFA.
- DUse Amazon API Gateway and require server-side encryption (SSE) for photos.
答えを見る
正解: A
Cognito のユーザープールには、アプリ利用者向けのサインアップ、サインイン、SMS による MFA が標準で用意されているので、素早く作れて保守も楽です。IAM は AWS の管理者向けで、アプリの顧客向けではありません。
2A company wants to restrict access to the content of its web application. The company needs to protect the content by using authorization techniques that are available on AWS. The company also wants to implement a serverless architecture for authorization and authentication that has low login latency. The solution must integrate with the web application and serve web content globally. The application currently has a small user base, but the company expects the application's user base to increase. Which solution will meet these requirements?
- AConfigure Amazon Cognito for authentication. Implement Lambda@Edge for authorization. Configure Amazon CloudFront to serve the web application globally.
- BConfigure AWS Directory Service for Microsoft Active Directory for authentication. Implement AWS Lambda for authorization. Use an Application Load Balancer to serve the web application globally.
- CConfigure Amazon Cognito for authentication. Implement AWS Lambda for authorization. Use Amazon S3 Transfer Acceleration to serve the web application globally.
- DConfigure AWS Directory Service for Microsoft Active Directory for authentication. Implement Lambda@Edge for authorization. Use AWS Elastic Beanstalk to serve the web application globally.
答えを見る
正解: A
Cognito がユーザー認証を、Lambda@Edge がエッジでの認可を、CloudFront が世界中への配信を担います。すべてサーバーレスで、レイテンシーも低く抑えられます。
よくある質問
ユーザープールと ID プールの違いは何ですか?
ユーザープールはユーザーディレクトリで、サインアップ、サインイン、MFA、Google や Facebook によるソーシャルサインインを提供し、JWT トークンを返します。ID プールは ID(ゲストを含む)を一時的な AWS 認証情報に交換し、S3 や DynamoDB などのサービスを直接呼び出せるようにします。
Cognito の料金はどのように決まりますか?
主にユーザープールの月間アクティブユーザー数(MAU)で課金され、機能プラン(Lite、Essentials、Plus)によって料金が異なります。SAML や OIDC によるフェデレーション、高度なセキュリティなど一部の機能は追加料金がかかります。
AWS 認定試験では Cognito について何が問われますか?
ユーザープールと ID プールの違い、API Gateway のオーソライザーとしての Cognito の利用、ソーシャルサインイン、アクセスキーを埋め込まずにモバイルアプリへ一時的な AWS 権限を与える方法などです。