Lesson 2 of 4 Amazon Cognito and IAM Identity Center

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.

How a Cognito identity pool gives an application AWS credentials. The user signs in with a user pool, a social provider or a SAML or OIDC provider. The identity pool maps the identity to an IAM role, AWS STS issues temporary credentials for that role, and the application's SDK calls services such as Amazon S3 or DynamoDB directly.
The application never holds long-term keys; the role chosen by the identity pool limits what each user can do.

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.

User pools
What it isA user directory
ProvidesAuthentication, sign-in and JWTs
Typical useSigning in and calling your own APIs
Identity pools
What it isA credential broker
ProvidesTemporary AWS credentials mapped to IAM roles
Typical useUploading to S3 or writing to DynamoDB directly from the app

Many applications use both: the user pool for sign-in and the identity pool for AWS credentials.

Sort into groups

User pool or identity pool?

Exam skills 1.2.3 · 1.1.6

Decide which Cognito component each statement describes.

Log in to try this activity Interactive activities are free with an account.

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.

Scenario questions

Per-user access

Exam skills 1.2.3 · 1.1.2

Scope access so each user reaches only their own data.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

NeedComponent
Sign-up, sign-in, MFA, Google sign-inCognito user pool with Google as an identity provider
Protect the APIAPI Gateway with a Cognito user pool authorizer validating the user's token
Upload to S3 without going through the APICognito identity pool exchanging the token for temporary AWS credentials
Each user only in their own S3 folderIdentity 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.

Check yourself

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.