The Problem It Solves
A mobile or browser app wants to upload a photo straight to S3 or read its own DynamoDB items. You must not embed AWS access keys in the app. An identity pool gives each signed-in user short-lived credentials tied to an IAM role you control.
Authenticated and Unauthenticated Roles
| Role | For | Typical permissions |
|---|---|---|
| Authenticated role | Users who signed in through a trusted provider | Read/write their own S3 prefix and DynamoDB items |
| Unauthenticated (guest) role | Visitors with no sign-in (optional, off by default) | Very limited: read public content, send anonymous analytics |
- Both roles have a trust policy for
cognito-identity.amazonaws.comwith conditions on the identity pool id andamr(authenticatedorunauthenticated) - You can use **rules or token claims (groups,
cognito:preferred_role)** to choose different roles for admins and normal users
Fine-Grained Access with Policy Variables
One policy can give each user access only to their own data by using the identity id variable.
${cognito-identity.amazonaws.com:sub}is the user's identity id (a value likeap-south-1:uuid)- S3: each user only sees
my-app-private/IDENTITY_ID/... - DynamoDB:
dynamodb:LeadingKeysforces the partition key to equal the identity id, so users can touch only their own rows
Using It in Code
Supported Identity Providers
- Cognito user pools
- OIDC providers and SAML providers
- Social: Google, Facebook, Apple, Amazon
- Developer authenticated identities (your own backend vouches for the user)
Enhanced (Simplified) vs Basic Flow
| Flow | Steps | Note |
|---|---|---|
| Enhanced (recommended) | GetId then GetCredentialsForIdentity. Cognito assumes the role for you | Simpler and more secure. Role choice stays in Cognito |
| Basic (classic) | GetId, GetOpenIdToken, then your app calls AssumeRoleWithWebIdentity itself | More control, you can choose the role and session policy |
User Pool vs Identity Pool Summary
| User pool | Identity pool | |
|---|---|---|
| Purpose | Authentication (sign-up and sign-in) | Authorization to AWS resources |
| Output | JWT tokens | Temporary AWS credentials |
| Used with | API Gateway authorizers, your own APIs | S3, DynamoDB, IoT, AppSync with IAM, direct SDK calls |
| Needs the other? | No, can work alone | Often uses a user pool as a login provider |
Security Best Practices
- Keep permissions minimal. These credentials live in the client, so assume a user can call any allowed API directly and write policies for that
- Use per-user prefixes and leading keys
- Disable the guest role unless you really need it
- Prefer API Gateway + Lambda with token validation when the backend must apply business rules (validation, quotas, auditing)
- For uploads, prefer presigned URLs created by your backend when you need size and type checks
Practice Tasks
- Create an identity pool with a user pool as provider and set an authenticated role
- Write the per-user S3 prefix policy and test that user A cannot read user B files
- Upload a file from a small web page with the SDK and the Cognito credentials
- Compare the same upload through a presigned URL
- Remove permissions and observe the
AccessDeniedmessage
Conclusion
Identity pools exchange tokens from user pools, social or SAML providers for temporary AWS credentials with authenticated and guest roles. Use IAM policy variables and LeadingKeys for per-user isolation, prefer the enhanced flow, keep roles minimal, and use a backend when business logic is needed.

