Ravindra BagaleCourses & study guides मराठी Track your progress

What Is Amazon Cognito? User Pools and Identity Pools Explained

Your React app needs sign-up, login, MFA, and temporary AWS credentials to upload to S3 — without you building a custom auth server. Amazon Cognito gives User Pools (directory + tokens) and Identity Pools (federate to AWS IAM credentials).

What you'll learn in this chapter

  • Why managed auth beats home-grown password DBs
  • What user pools vs identity pools are
  • How JWT app clients and IdPs fit together
  • Industries and mobile/web patterns
  • Lab, errors, FAQ, interview

1. Everyday example first

A Kochi fitness app lets users sign in with email+OTP or Google. Cognito User Pool issues JWT. An Identity Pool exchanges the token for short-lived IAM rights to PutObject into s3://app-uploads/userId/*. API Gateway JWT authorizer protects /workouts.

2. Why Cognito?

Why do we need this?

Password storage, reset flows, MFA, and token crypto are easy to get wrong. Cognito provides standards-based auth (OAuth2/OIDC) and hooks into AWS IAM for fine-grained data access.

  • Hosted UI or own UI with SDKs
  • Social and SAML/OIDC enterprise IdPs
  • JWT for APIs
  • Identity Pool → temporary AWS credentials

3. What are User Pools and Identity Pools?

What is it?

User Pool = user directory: sign-up, sign-in, MFA, groups, app clients, hosted UI; outputs JWT (ID/access/refresh). Identity Pool (Federated Identities) = map authenticated (or guest) users to IAM roles via STS — for AWS resource access.

PiecePlain job
User poolAuthN directory + tokens
App clientPublic/confidential client settings
Hosted UIReady login pages
Identity poolToken → IAM role credentials
JWT authorizerAPI Gateway validates Cognito tokens
TriggersLambda customize sign-up/auth challenges
User signs in → User Pool JWT → (API) and/or Identity Pool → IAM → AWS APIsBrowser/mobile user
Authorized API & S3

4. How to wire a safe app client

How to do it (step by step)

  1. Create a user pool with strong password policy + MFA optional/required as needed.
  2. Create an app client; for public SPAs use PKCE; never embed client secrets in mobile/SPA.
  3. Configure callback/logout URLs tightly.
  4. Enable Google/Apple/SAML only after privacy review.
  5. Protect APIs with JWT authorizer or ALB auth.
  6. If S3 upload needed, Identity Pool roles with prefix-by-sub IAM.
  7. Use groups/custom claims carefully; authorize on server.
  8. Rotate refresh token strategy; handle revoke on logout.
  9. Log Advanced Security / Compromised credentials features if available.
  10. Never use Cognito only on client without API checks.
# Mental model
# 1) User Pool authenticates → JWT
# 2) API Gateway JWT authorizer checks iss/aud/exp
# 3) Optional: Identity Pool GetCredentialsForIdentity → S3 PutObject

5. User pool vs identity pool vs IAM Identity Center

ChoiceWhen
Cognito user poolCustomer/app users AuthN
Cognito identity poolAWS creds for app users
IAM Identity CenterWorkforce SSO into AWS accounts
Amazon Verified PermissionsFine-grained authZ policies (Cedar)

6. Multi-industry notes

EdTech apps use Cognito for millions of students with social login. Hospitals integrate SAML IdP for clinicians but still Cognito for patient apps. Fintech adds MFA mandatory and device tracking.

7. Industries and roles

  • Mobile/web startups: fast auth
  • EdTech: social + email OTP
  • Healthcare: SAML workforce + patient pools
  • Fintech: MFA + risk
  • API developers: JWT authorizers

8. Lab: user pool + Hosted UI sample

Do this in ap-south-1 (Mumbai) unless your org forbids it. Set a budget alert first.

How to do it (step by step)

  1. Create a user pool with email sign-in in ap-south-1.
  2. Create a public app client with callback https://localhost:3000/callback (lab).
  3. Enable Hosted UI; create a test user; sign in; inspect JWT claims carefully offline (lab token).
  4. Optional: create Identity Pool mapped to authenticated role with Deny all except one S3 prefix.
  5. Attach API Gateway JWT authorizer mindset (even if mock).

Cleanup (stop billing)

  1. Delete test users, app client, user pool, identity pool roles.
  2. Remove lab callback URLs from any reused pools.

9. Common errors and fixes

Each row is a real mistake students and juniors hit. Read the fix before you panic.

Error / symptomFix
Client secret in React bundlePublic client + PKCE; secrets only confidential apps.
Callback URL *Exact URLs only.
Trusted JWT on client onlyAlways verify on API.
Identity Pool role = AdministratorAccessPrefix-scoped least privilege.
Confused user pool with identity poolAuthN tokens vs AWS creds.
Disabled token revocation thinkingHandle logout/refresh carefully.
MFA off for finance appTurn on MFA.
Custom UI missing error handling for USER_PASSWORD_AUTH flowsHandle challenges.
PII in custom attributes without consentPrivacy review.
Long-lived access tokensShort exp + refresh.

10. Troubleshooting checklist

  1. Invalid redirect: callback mismatch.
  2. Unauthorized client: app client auth flows mis-set.
  3. CORS issues on Hosted UI/API: configure properly.
  4. Identity Pool no credentials: role trust / claim mapping.
  5. API 401: audience/issuer mismatch.
  6. Email delivery for OTP: SES sandbox limits.

11. FAQ

Q. Cognito free?

MAU pricing; free tier exists — check current.

Q. Replace Auth0/Firebase?

Comparable space; evaluate features/TCO.

Q. Offline mobile?

Refresh/session design; not magic offline auth.

Q. SAML enterprise?

Supported federation into user pools.

Q. Guest access?

Identity Pool unauthenticated role — tight limits.

Q. Passwordless?

Custom auth / features evolving — verify docs.

12. Interview Q&A

Q. User pool vs identity pool?

User pool=AuthN/JWT; identity pool=AWS IAM creds.

Q. What is JWT here?

Signed token APIs validate for identity/scopes.

Q. PKCE?

Protect public clients during auth code flow.

Q. Hosted UI?

Cognito-managed login pages.

Q. Secure S3 upload pattern?

Identity Pool role limited to user prefix.

Q. Workforce vs customer auth?

IDC for workforce; Cognito often customer apps.

13. Tips in English, Marathi and Hindi

Ravindra Bagale's Tip (EN)

Never ship a Cognito client secret inside a mobile or SPA binary. Public client + PKCE, and authorize on the server.

Ravindra Bagale's Tip (MR)

Mobile/SPA मध्ये client secret नका. Public+PKCE; server authorize.

Tip (HI)

मोबाइल/SPA में client secret मत डालो — public+PKCE और server पर authorize।

Try at home

  1. Draw user pool → JWT → API Gateway on paper.
  2. Write an IAM policy sketch: s3:PutObject on private/user-sub prefix.
  3. List three callback URL mistakes.

In short

  1. User pools authenticate users and issue JWTs.
  2. Identity pools exchange identity for IAM credentials.
  3. PKCE for public apps; verify JWT on APIs.
  4. Least-privilege roles for uploads.

Revision, interview and comparison cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Revision card: Cognito user and identity pools. Building your own auth (passwords, MFA, tokens) is slow and risky. User pools authenticate users (sign-up/sign-in/MFA/JWT). Identity pools can issue temporary AWS credentials. Create user pool + app client → host UI or SDK → validate JWTs on API; optional identity pool for S3 uploads. Mobile app signs in via user pool; gets scoped creds to upload to one S3 prefix.
Revision card: Cognito user and identity pools · Download PNG · Open card page
Revision card: Validate JWTs. Trusting a token without verifying signature/claims invites impersonation. APIs must verify issuer, audience, signature (JWKS), and expiry on Cognito access/ID tokens. Use API Gateway Cognito authorizer or verify in app middleware; never skip in “just for now”. Expired token → 401; wrong aud → reject even if signature looks fine.
Revision card: Validate JWTs · Download PNG · Open card page
Comparison: User pool vs Identity pool. User pool: Sign-up/sign-in/MFA/JWT for your app users. Identity pool: Federated identities → temporary AWS credentials.
Comparison: User pool vs Identity pool · Download PNG · Open card page
Comparison: JWT access token vs Server session cookie. JWT access token: Stateless bearer verified via JWKS; good for APIs/SPAs. Server session cookie: Server stores session; classic websites.
Comparison: JWT access token vs Server session cookie · Download PNG · Open card page
Interview card: User pool vs identity pool?. User pool authenticates users (JWT). Identity pool can exchange identity for temporary AWS credentials. Many apps need only a user pool + API JWT check. Sign-in JWT to API Gateway; identity pool for direct S3 upload.
Interview card: User pool vs identity pool? · Download PNG · Open card page
Interview card: Why enforce MFA?. Password-only accounts fall to phishing/stuffing; MFA raises the bar. Prefer TOTP/WebAuthn over SMS when possible. Admin console requires MFA ON for all users.
Interview card: Why enforce MFA? · Download PNG · Open card page
Interview card: What must you verify on a JWT?. Signature (JWKS), issuer, audience, expiry (and token_use as applicable). Never trust claims without verification. API Gateway Cognito authorizer handles checks.
Interview card: What must you verify on a JWT? · Download PNG · Open card page
Interview card: Public vs confidential app client?. Public (mobile/SPA) has no client secret; confidential (server) can use a secret. Use PKCE for public clients. React SPA = public+PKCE; backend = confidential.
Interview card: Public vs confidential app client? · Download PNG · Open card page

More cards: Ravi Sir's Cards.

Practice Tasks

About this scenario

Nairobi Ride is a transport business in Nairobi. Wanjiru Kamau looks after its IT and cloud work. They count money in KES and run their AWS resources in the af-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 16

Easy List user pools

Why: Auth inventory.

What to do: List user pools.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy Describe user pool

Why: MFA/password.

What to do: Describe user pool nairobiridepool.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy List resource servers

Why: Scopes.

What to do: List resource servers for nairobiridepool.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Easy User pool authenticates — 1

Why: AuthN.

What to do: Enter 1 if user pools authenticate users.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Easy Identity pool AWS creds — 1

Why: Creds.

What to do: Enter 1 if identity pools can issue temporary AWS credentials.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium List user pool clients

Why: App clients.

What to do: List user pool clients for nairobiridepool.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium List groups

Why: Authorization.

What to do: List groups in nairobiridepool.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium List identity pools

Why: AWS creds.

What to do: List identity pools.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Medium Allow GetId identity pool

Why: Mobile LP.

What to do: Allow cognito-identity:GetId on arn:aws:cognito-identity:af-south-1:104591109902:identitypool/nairobirideidpool.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Medium MFA required JSON

Why: Security.

What to do: JSON MfaConfiguration ON.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.

Medium Validate JWT — 1

Why: API auth.

What to do: Enter 1 if APIs should validate Cognito JWTs.

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Medium Sample access token minutes

Why: Token life.

What to do: Sample access token minutes often used (enter 60)?

How:
  1. Write down the numbers the task gives you.
  2. Work it out step by step on paper.
  3. Type only the final answer in the box.
  4. Click Check answer.

Hard List identity providers

Why: Federation.

What to do: List identity providers for nairobiridepool.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard List users

Why: Directory.

What to do: List users in nairobiridepool.

How:
  1. Read the task and decide which AWS service it is about.
  2. Write aws, then the service name, then the operation (the action word).
  3. Add each needed flag (--name) followed by its value from the task.
  4. Flags can be in any order. Click Check answer.

Hard Allow AdminGetUser

Why: Support LP.

What to do: Allow cognito-idp:AdminGetUser on arn:aws:cognito-idp:af-south-1:104591109902:userpool/nairobiridepool.

How:
  1. Start with { "Version": "2012-10-17", "Statement": [ ... ] }.
  2. In each statement write Effect, Action and Resource.
  3. Give only what the task needs (least privilege).
  4. Use double quotes and commas correctly. Click Check answer.

Hard Auth flows JSON

Why: SRPA.

What to do: JSON ExplicitAuthFlows USER_SRP_AUTH.

How:
  1. Start from the top-level keys the task names.
  2. Indent child keys with 2 spaces (no tabs).
  3. Fill in each property with the value from the task.
  4. Click Check answer. Key order does not matter.