Amazon Cognito
User pools authenticate and issue tokens. Identity pools exchange tokens for temporary AWS credentials, including guest access.
The decision: does the requirement need user authentication (user pool) or AWS credentials for those users (identity pool)?
Cognito is customer identity for web and mobile apps. Two parts, independent but often combined. A user pool answers "who is this user" and issues JWTs. An identity pool answers "which AWS resources may they reach" and issues temporary AWS credentials.
User pools versus identity pools
| User pool | Identity pool | |
|---|---|---|
| Purpose | Authentication, user directory | Authorization to AWS resources |
| Output | ID, access and refresh tokens (OIDC and OAuth 2.0 JWTs) | Temporary AWS credentials from STS |
| Sources | Built-in directory, social (Google, Apple, Facebook, Amazon), SAML, OIDC | User pool, social, SAML, OIDC, developer-authenticated identities |
| Extras | MFA (TOTP, SMS, email), sign-up and sign-in pages, groups, custom attributes, Lambda triggers, WAF web ACL | Role-based and attribute-based access control, guest identities |
| Guests | No | Yes, unauthenticated identities |
| Needs the other | No | No |
Exam signal
"Sign-up, sign-in, hosted login page, MFA": user pool. "Mobile app uploads directly to S3 with per-user permissions" or "guests can view": identity pool. "Protect an API Gateway API with user tokens": a Cognito user pool authorizer.
Feature plans for user pools
New pools default to Essentials.
| Plan | Adds |
|---|---|
| Lite | Basic sign-in, classic hosted UI, social and SAML federation, groups, MFA with authenticator apps and SMS, Lambda triggers |
| Essentials | Managed login pages, passwordless one-time codes, passkeys, email MFA, access token customization |
| Plus | Threat protection. Checks passwords for compromise, scores risky sign-ins, logs user activity and risk, and can export those logs |
Some of these controls are unavailable for federated users.
Identity pool authorization
How an authenticated user gets an IAM role.
- Use the role from the token if user pool groups assigned roles (
cognito:preferred_roleorcognito:rolesclaims). - Otherwise apply rule-based mapping. A rule compares a claim with Equals, NotEqual, StartsWith or Contains. Rules run in order and the first match wins. Up to 25 rules per provider.
- Otherwise fall back to the default authenticated role, or deny, as set by the role resolution setting.
Guests get the unauthenticated role. Give it minimal permissions, for example read-only on one bucket. The trust policy of each role should allow only cognito-identity.amazonaws.com for your identity pool ID and the matching authenticated or unauthenticated value.
Attribute-based access control maps claims to principal tags on the session. Policies then check aws:PrincipalTag.
Exam trap
Never map a claim that users can edit themselves to a privileged role. Make the attribute read-only for the app client or the user can pick their own role.
Why Cognito instead of raw web identity federation
Cognito brokers the federation. Without it, the app calls the identity provider and then AssumeRoleWithWebIdentity itself. Cognito also adds guest access.