Skip to Content
Asterrr's Handbook
3 · Infrastructure security

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.

sign infederateJWTIAM roleaccessMobile or webappSocial or SAMLIdPUser poolIdentity poolTemporarycredentialsS3 or DynamoDB
The app signs in at the user pool, then trades the token at the identity pool for STS credentials that call AWS services.

User pools versus identity pools

User poolIdentity pool
PurposeAuthentication, user directoryAuthorization to AWS resources
OutputID, access and refresh tokens (OIDC and OAuth 2.0 JWTs)Temporary AWS credentials from STS
SourcesBuilt-in directory, social (Google, Apple, Facebook, Amazon), SAML, OIDCUser pool, social, SAML, OIDC, developer-authenticated identities
ExtrasMFA (TOTP, SMS, email), sign-up and sign-in pages, groups, custom attributes, Lambda triggers, WAF web ACLRole-based and attribute-based access control, guest identities
GuestsNoYes, unauthenticated identities
Needs the otherNoNo

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.

PlanAdds
LiteBasic sign-in, classic hosted UI, social and SAML federation, groups, MFA with authenticator apps and SMS, Lambda triggers
EssentialsManaged login pages, passwordless one-time codes, passkeys, email MFA, access token customization
PlusThreat 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.

  1. Use the role from the token if user pool groups assigned roles (cognito:preferred_role or cognito:roles claims).
  2. 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.
  3. 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.

Further reading

On this page