Lesson 2 of 2 Amazon Cognito

Granting AWS Credentials to App Users with Amazon Cognito Identity Pools

This lesson explains how an Amazon Cognito identity pool exchanges an identity for temporary AWS credentials, so a client app can call AWS services directly. Identity pools answer the question of authorisation to AWS resources.

Learning outcomes

After completing this lesson, you will be able to:

  • Distinguish user pools from identity pools.
  • Describe how an identity pool exchanges an authenticated identity for temporary credentials and how roles are chosen.
  • Explain how policy variables scope one IAM policy to each user's own data.
  • Choose a user pool, an identity pool or both for a given requirement.

User pools vs identity pools

User pools and identity pools are frequently confused because both appear in Cognito, but they answer different questions. A user pool is a user directory: it signs users up and in, with passwords, MFA or a social or corporate identity provider, and issues JWTs that prove who the user is. Those tokens are meant for your own APIs, for example through an API Gateway authorizer.

An identity pool issues AWS credentials. It accepts a token from a user pool or another identity provider, maps the user to an IAM role, and returns temporary credentials from STS, so a mobile or web application can call AWS services such as S3 or DynamoDB directly. Separate roles apply to authenticated users and, optionally, to guests. Because every user receives a unique identity ID, the role's policy can use the ${cognito-identity.amazonaws.com:sub} variable to limit each user to their own S3 prefix or DynamoDB items. Applications often use both: the user pool signs the user in, and the identity pool exchanges the resulting token for AWS credentials. The following table summarises the differences.

User pool
AnswersWho is the user?
IssuesJWTs (ID, access and refresh tokens)
Typical useSigning in and calling your APIs
Identity pool
AnswersWhat AWS resources can they access?
IssuesTemporary AWS credentials from STS
Typical useCalling S3, DynamoDB or Kinesis directly from the app

How identity pools work

An identity pool exchanges an authenticated identity for temporary AWS credentials. The process consists of the following steps.

  1. Sign in

    The app signs the user in with a user pool, a social provider, or a SAML or OIDC provider. Guest (unauthenticated) access can also be allowed.

  2. Map the user to a role

    The identity pool maps the user to an IAM role.

  3. Return temporary credentials

    The identity pool obtains temporary credentials for that role from STS and returns them to the app.

Roles can be chosen in three ways:

  • A single authenticated role for everyone.
  • Rules based on token claims.
  • The cognito:preferred_role claim from user pool groups, so administrators and ordinary users get different permissions.

Example. A browser app exchanges the user's ID token for credentials and calls S3 directly:

import { S3Client } from "@aws-sdk/client-s3";
import { fromCognitoIdentityPool } from "@aws-sdk/credential-providers";

const s3 = new S3Client({
  region: "eu-west-1",
  credentials: fromCognitoIdentityPool({
    clientConfig: { region: "eu-west-1" },
    identityPoolId: "eu-west-1:1a2b3c4d-...",
    logins: { "cognito-idp.eu-west-1.amazonaws.com/eu-west-1_AbCdEf": idToken },
  }),
});

Per-user access

Policy variables such as ${cognito-identity.amazonaws.com:sub} let one IAM policy scope access per user, for example to their own S3 prefix or DynamoDB items. This is how apps upload files, send analytics events or read their own data with no backend server in between.

Worked example

User pool, identity pool or both?

Applications may require a user pool, an identity pool or both. The following table identifies the appropriate combination for several requirements.

RequirementUse
Users sign up and sign in to a web app that calls your APIUser pool only
A mobile app uploads photos directly to S3 into each user's own folderUser pool for sign-in, plus identity pool for AWS credentials
Guests can browse a catalogue in DynamoDB without signing inIdentity pool with unauthenticated (guest) access and a read-only role
Users sign in with Google and need temporary AWS credentialsIdentity pool with Google as a provider, or a user pool federated with Google plus an identity pool

The distinction is between authentication (user pools: who are you?) and authorisation to AWS services (identity pools: here are temporary credentials). When the app only calls your own API, the identity pool is unnecessary.

Check yourself

A mobile app must upload files directly to S3 for signed-in users. Which Cognito component provides the AWS credentials?

Show answer

An identity pool. It exchanges the user's sign-in token for temporary credentials of an IAM role; a user pool issues tokens but not AWS credentials.

Exam Tips

Remember for the exam

  • Identity pools handle authorisation to AWS resources.
  • When a client app must call AWS services directly with temporary credentials, use an identity pool.
  • For mobile users uploading to their own S3 folder, use an identity pool with per-user policy variables.
  • To give administrators and ordinary users different AWS permissions, use role mapping by group or claim.