Amazon Cognito
User Pools, Identity Pools, Authentication, Social Login, JWT Tokens
Tổng quan
Amazon Cognito cung cấp authentication, authorization, và user management cho web và mobile applications. Cognito giúp bạn thêm tính năng đăng nhập/đăng ký mà không cần tự build backend xác thực.
Hiểu đơn giản
User Pools - Chi tiết
User Pool = User Directory để quản lý users và xác thực.
Features
Authentication Flow
JWT Tokens
| Token | Mô tả | Expiry |
|---|---|---|
| ID Token | Chứa user info (email, name...) | 1 hour |
| Access Token | Dùng để call APIs | 1 hour |
| Refresh Token | Dùng để lấy tokens mới | 30 days (configurable) |
Identity Pools - Chi tiết
Identity Pool = Cấp AWS credentials tạm thời để access AWS services trực tiếp.
Workflow
IAM Roles
Identity Pool có 2 loại IAM Roles:
User Pools vs Identity Pools
Cốt lõi: 2 câu hỏi hoàn toàn khác nhau
Đây là điểm hay làm người mới rối — tưởng 2 cái cùng làm 1 việc (login). Thực ra chúng giải quyết 2 vấn đề riêng biệt:
Ví dụ đời thực: Đi vào tòa nhà công ty
Vì sao phải tách ra 2 thứ?
Câu trả lời ngắn: JWT từ User Pool không phải AWS credentials. AWS SDK (S3, DynamoDB...) chỉ chấp nhận AWS credentials, nó không hiểu JWT.
Bảng so sánh
| Aspect | User Pools | Identity Pools |
|---|---|---|
| Purpose | Authentication (Who are you?) | Authorization (What can you do on AWS?) |
| Output | JWT Tokens (ID/Access/Refresh) | AWS Credentials tạm thời (~1h) |
| Ai "hiểu" output này? | Backend của bạn (API Gateway, Lambda) | AWS Services (S3, DynamoDB, ...) |
| Use case | Login vào app của bạn | Cho client gọi thẳng AWS service |
| Lưu trữ users? | ✅ Có (như database user) | ❌ Không |
| Social login | ✅ Native (Google, Facebook, SAML, OIDC) | Thường lấy identity từ User Pool |
2 kịch bản thực tế — khi nào cần cái nào
Kịch bản A — Chỉ cần User Pool (phổ biến nhất)
→ Backend của BẠN tự gọi DynamoDB. User chỉ nói chuyện với API của bạn. Không cần Identity Pool.
Kịch bản B — Cần cả 2 (client gọi AWS trực tiếp)
→ App gọi thẳng S3 từ điện thoại, không qua backend của bạn. Bắt buộc cần Identity Pool để có AWS credentials hợp lệ.
Khi nào cần Identity Pool?
Chỉ khi client (mobile/web/IoT) gọi trực tiếp dịch vụ AWS — không qua backend của bạn:
- Upload ảnh/video thẳng từ điện thoại lên S3
- Đọc/ghi DynamoDB trực tiếp từ web app
- Gọi AWS AppSync / Kinesis / IoT Core từ device
- Cho phép guest user (chưa login) xem một số resource công khai trong S3
Nếu bạn có backend (API Gateway + Lambda, hoặc EC2/ECS chạy code của bạn) làm trung gian → chỉ cần User Pool.
Tóm 1 câu để nhớ
User Pool = "thẻ nhân viên" để app của BẠN nhận biết user. Identity Pool = "chìa khóa AWS" để user (qua app) tự mở cửa kho AWS.
Common Architecture
Architecture 1: User Pool + API Gateway
Architecture 2: User Pool + Identity Pool + S3
Lambda Triggers
User Pool có thể trigger Lambda functions tại các events:
Hosted UI
Cognito cung cấp Hosted UI - trang login ready-to-use:
Security Features
| Feature | Description |
|---|---|
| MFA | SMS, TOTP (Google Authenticator), Hardware keys |
| Adaptive Authentication | Risk-based, block suspicious logins |
| Compromised Credentials | Block logins with leaked passwords |
| Advanced Security | WAF integration, bot detection |
| Encryption | Data encrypted at rest và in transit |
Pricing
| Component | Free Tier | After Free Tier |
|---|---|---|
| MAU (Monthly Active Users) | 50,000 MAU | $0.0055/MAU |
| SAML/OIDC federation | - | $0.015/MAU |
| Advanced Security | - | Additional cost |
[!TIP] 50,000 MAU free tier rất generous - hầu hết startups không cần trả tiền.
Exam Tips cho Cloud Practitioner
[!IMPORTANT] Keywords để nhớ Cognito:
- User Pools = Authentication (WHO are you?) → JWT Tokens
- Identity Pools = Authorization (WHAT can you do?) → AWS Credentials
- Social login (Google, Facebook), SAML, OIDC
- MFA, password policies, email verification
Câu hỏi thường gặp
| Câu hỏi | Trả lời |
|---|---|
| Service nào cho user sign-up/sign-in? | Cognito User Pools |
| Làm sao mobile app access S3 trực tiếp? | Cognito Identity Pools |
| Integrate với Google/Facebook login? | Cognito User Pools |
| User Pools output gì? | JWT Tokens |
| Identity Pools output gì? | AWS Credentials (temporary) |
| Serverless authentication? | Cognito |