Amazon Cognito
Sign-up, sign-in and AWS access for the users of web and mobile apps, with user pools and identity pools.
What it is
Amazon Cognito gives the users of web and mobile apps an identity: a way to sign up, sign in and, if needed, reach AWS resources directly. It has two parts:
- User pools: a user directory and sign-in service for an app. Users sign in with a username or email and password, or with Google, Facebook, Apple, SAML or OIDC, and get tokens the app and its APIs can trust.
- Identity pools (also called federated identities): swap a token from a user pool or another identity provider for temporary AWS credentials, so the app can call AWS services such as S3 or DynamoDB directly.
Cognito exists because IAM isn't built for app customers. Creating an IAM user for each of a million shoppers isn't practical. Cognito scales to millions of users, handles password resets and MFA, and gives each user only narrow, temporary access.
Cognito user pools
A user pool is a serverless database of users for an app, with everything around sign-in built in:
- Sign-up and sign-in with username (or email) and password, and password reset.
- Email and phone number verification.
- MFA, including authenticator apps, and on higher plans passwordless sign-in with passkeys or one-time codes.
- Federated sign-in with social providers (Google, Facebook, Apple, Amazon), SAML 2.0 and OpenID Connect providers.
- Managed login: a ready-made, customisable sign-in page, so there's no need to build one.
After sign-in, the user pool issues JSON Web Tokens (JWTs) that prove who the user is.
User pool integrations
- The user signs in to the Cognito user pool and the app receives a token.
- The app calls the API (on API Gateway, or behind an Application Load Balancer) and passes the token.
- API Gateway (or the ALB) verifies the token against the user pool. Invalid or expired tokens are rejected.
- Valid requests reach the backend, such as a Lambda function or EC2 instances.
With an ALB, the listener rule authenticates the user with Cognito before forwarding to the target group. Either way, the backend code doesn't need to check passwords itself.
Cognito identity pools
Identity pools give app users temporary AWS credentials, so the app can call AWS services directly instead of going through the app's own backend.
- The user logs in to an identity provider: a Cognito user pool, Google, Facebook, a SAML or OIDC provider. The app gets a token.
- The app sends the token to the Cognito identity pool, which validates it with the provider and returns temporary AWS credentials (through STS).
- The app uses those credentials to reach S3, DynamoDB or other services directly.
How permissions work:
- The IAM policies behind the credentials are defined in Cognito, as IAM roles.
- There's a default role for authenticated users and another for guest (unauthenticated) users, if guests are allowed.
- Rules can pick a different role per user, and policies can use the user's ID for fine-grained control.
Row-level security in DynamoDB
This policy lets each user read and write only the DynamoDB items whose partition key equals their own Cognito identity ID:
{
"Version": "2012-10-17",
"Statement": [{
"Effect": "Allow",
"Action": [
"dynamodb:GetItem", "dynamodb:BatchGetItem", "dynamodb:Query",
"dynamodb:PutItem", "dynamodb:UpdateItem", "dynamodb:DeleteItem",
"dynamodb:BatchWriteItem"
],
"Resource": ["arn:aws:dynamodb:us-west-2:123456789012:table/MyTable"],
"Condition": {
"ForAllValues:StringEquals": {
"dynamodb:LeadingKeys": ["${cognito-identity.amazonaws.com:sub}"]
}
}
}]
}${cognito-identity.amazonaws.com:sub} is replaced at request time by the caller's identity ID. A user can never read someone else's rows, even though everyone uses the same table and the same role. The same idea works for S3, with a folder per user.
User pools vs identity pools
| User pool | Identity pool | |
|---|---|---|
| Answers | Who is this user? | What AWS access do they get? |
| Gives the app | Tokens (JWTs) | Temporary AWS credentials |
| Typical use | Sign-in, protect an API | Direct access to S3, DynamoDB |
They're often used together: the user pool signs users in, and the identity pool turns that sign-in into AWS credentials.
Pricing
User pools are priced per monthly active user (MAU) and come in three feature plans:
- Lite: sign-up, sign-in, social and enterprise federation, MFA.
- Essentials: adds managed login, passwordless sign-in (passkeys, email or SMS codes), and more token customisation.
- Plus: adds threat protection, such as risk-based adaptive authentication and compromised-credential detection.
Lite and Essentials include 10,000 MAU free every month, and this free allowance doesn't expire after the first year. Identity pools themselves have no charge.
Updated since the slides
The feature plans (Lite, Essentials, Plus), managed login and passkey support are newer than the course slides. The core ideas, user pools for sign-in and identity pools for AWS credentials, haven't changed.
Cognito vs IAM
The course's rule of thumb for spotting Cognito in a question:
- "Hundreds of users", "mobile users", "web app users", "authenticate with SAML / Google / Facebook" → Cognito.
- People and workloads managing the AWS account → IAM (or IAM Identity Center).
Exam tips
- "Add sign-up and sign-in to a web or mobile app" → Cognito user pool.
- "Authenticate API Gateway requests with app users" → Cognito user pool authorizer.
- "Authenticate users at an Application Load Balancer" → ALB with Cognito authentication.
- "Mobile app users need direct access to S3 / DynamoDB" → Cognito identity pool (temporary credentials).
- "Each user can only access their own items / folder" → identity pool + policy variable
${cognito-identity.amazonaws.com:sub}. - "Guest access to AWS resources" → identity pool with unauthenticated identities.
Common traps
- "Create an IAM user for each app user." Doesn't scale and isn't safe. Use Cognito.
- Mixing up the pools. User pools sign users in; identity pools hand out AWS credentials.
- "Cognito is for employee single sign-on to AWS accounts." That's IAM Identity Center.
Hands-on lab: a user pool with managed login
Goal: create a user pool, sign up through Cognito's managed login page, and see the user appear.
Free
Under 10,000 monthly active users on the Lite or Essentials plan, a user pool costs nothing.
Create the user pool
Open Amazon Cognito → User pools → Create user pool. Choose application type Single-page application (SPA), name the app saa-lab-app, sign-in identifier Email, required attribute email, and add a return URL https://localhost:3000. Create the pool.
Open the managed login page
In the new pool, open App clients → saa-lab-app → View login page. A hosted sign-in page opens.
Sign up
Choose Create an account, enter your email and a password, and confirm the code Cognito emails you. After sign-in, the browser goes to https://localhost:3000/?code=… and shows an error page: that's expected, there's no app running. The code in the address is what a real app would exchange for tokens.
See the user
Back in the console, open Users. Your user is listed, with Confirmation status: Confirmed and Email verified: Yes.
Expected result: a working sign-up and sign-in flow without writing any code, and a confirmed user in the pool.
Clean up: delete the user pool (User pools → select → Delete; you may need to turn off deletion protection first).