Granting AWS Credentials to App Users with Cognito Identity Pools
This lesson explains how a Cognito identity pool exchanges an identity for temporary AWS credentials, so web and mobile applications can call AWS services such as S3 and DynamoDB directly.
Learning outcomes
After completing this lesson, you will be able to:
- Describe how identity pools issue temporary credentials.
- Distinguish user pools from identity pools.
- Scope access to each user's own data with policy variables.
How identity pools work
An identity pool exchanges an authenticated identity for temporary AWS credentials, so a mobile or web application can call AWS services, such as Amazon S3 or DynamoDB, directly. The user first signs in with a user pool, a social provider or a SAML or OIDC provider; identity pools can also allow guest, unauthenticated access.
The identity pool then maps the identity to an IAM role, either a single authenticated role, a role chosen by rules on the token's claims, or the role associated with the user's user pool group. AWS STS issues temporary credentials for that role, and the application's AWS SDK uses them to call AWS. Because credentials are temporary and scoped by the role, no long-term keys are ever embedded in the application.
The following diagram shows the exchange from sign-in to a direct AWS call.

User pools or identity pools?
Cognito user pools and identity pools serve distinct purposes. A user pool is a user directory: it handles sign-up, sign-in, MFA and federation with social or enterprise identity providers, and issues JSON Web Tokens that prove who the user is. An application uses these tokens to call its own APIs, for example through an API Gateway authorizer.
An identity pool is a credential broker. It accepts a token from a user pool or another identity provider, maps the user to an IAM role, and returns temporary AWS credentials, so a mobile or web application can call AWS services directly, such as uploading a photo to S3 or writing to DynamoDB. Policy variables restrict each user to their own data, such as their own S3 prefix. The following table summarises the differences.
Many applications use both: the user pool for sign-in and the identity pool for AWS credentials.
User pool or identity pool?
Scoping access per user
Policy variables restrict each user to their own data with a single role. For example, the authenticated role can allow S3 access only under the user's own prefix:
{
"Effect": "Allow",
"Action": ["s3:GetObject", "s3:PutObject"],
"Resource": "arn:aws:s3:::photo-app/${cognito-identity.amazonaws.com:sub}/*"
}The same approach with dynamodb:LeadingKeys limits users to their own items. Guest roles should be kept to read-only access to public content.
Per-user access
Authentication for a serverless app
A mobile app lets customers sign up, sign in with Google, call the company's API, and upload photos straight to S3.
| Need | Component |
|---|---|
| Sign-up, sign-in, MFA, Google sign-in | Cognito user pool with Google as an identity provider |
| Protect the API | API Gateway with a Cognito user pool authorizer validating the user's token |
| Upload to S3 without going through the API | Cognito identity pool exchanging the token for temporary AWS credentials |
| Each user only in their own S3 folder | Identity pool role policy using ${cognito-identity.amazonaws.com:sub} in the resource ARN |
Without the identity pool, photos would have to pass through the API and Lambda, adding cost and latency. The identity pool lets the browser or phone talk to S3 directly, but only within each user's own prefix.
What does an identity pool give a signed-in mobile user?
Show answer
Temporary AWS credentials for an IAM role, so the app can call services such as S3 or DynamoDB directly with the permissions that role allows.
Exam Tips
Remember for the exam
- When application users need direct access to S3 or DynamoDB, use a Cognito identity pool for temporary credentials.
- To give each user access to only their own S3 prefix, use the
${cognito-identity.amazonaws.com:sub}policy variable. - For limited access by users who have not signed in, use identity pool guest access with a restricted role.
- A user pool authenticates; an identity pool provides AWS credentials.