Lesson 1 of 4 Amazon Cognito and IAM Identity Center

Managing Customer Sign-In with Cognito User Pools

This lesson explains how a Cognito user pool provides a managed user directory that handles sign-up, sign-in and tokens for the customers of web and mobile applications, without building an identity system.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe what user pools provide.
  • Explain how Lambda triggers customise sign-up and sign-in flows.
  • Explain how Cognito tokens authorise API requests.

What user pools provide

A Cognito user pool is a complete user directory and sign-in service for applications. It handles sign-up and sign-in, password policies, account recovery and multi-factor authentication. Managed login, formerly called the hosted UI, provides ready-made sign-in pages and OAuth 2.0 flows, so applications need not build their own login pages.

User pools can federate with social identity providers, such as Google, Apple, Facebook and Amazon, and with enterprise providers through SAML or OpenID Connect (OIDC). On sign-in, they issue JSON Web Tokens: an ID token describing who the user is, an access token listing scopes and groups, and a refresh token for obtaining new tokens. Users can be placed in groups, which appear in the tokens and can drive authorisation decisions in applications.

Customising user pools

A Cognito user pool's sign-up and sign-in flows can be customised with Lambda triggers, which Cognito invokes at specific points. A pre sign-up trigger can validate a registration, for example allowing only company email domains, and a post confirmation trigger can run follow-up actions, such as creating the user's profile record in a database.

A user migration trigger moves users from a legacy directory without forcing password resets: when an unknown user signs in, the function checks the credentials against the old system and, if they are valid, Cognito creates the user in the pool. A pre token generation trigger adds, removes or changes claims in the tokens, such as adding the user's subscription tier. Threat protection, also called adaptive authentication, assesses the risk of each sign-in, requiring MFA for risky ones and blocking credentials known to be compromised. The following table summarises these features.

FeatureUse
Pre sign-up and post confirmation triggersValidate registrations and run follow-up actions
User migration triggerMove users from a legacy directory on first sign-in, without password resets
Pre token generation triggerAdd or change claims in tokens
Threat protection (adaptive authentication)Require MFA for risky sign-ins and block compromised credentials
Match the pairs

User pool customisation

Exam skills 1.2.3

Match each requirement with the Cognito feature that meets it.

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

Using Cognito as an authorizer

Cognito tokens let other services validate users before requests reach application code. In a typical serverless flow, users sign in to a user pool and receive a JWT, and the application sends it with each request to Amazon API Gateway. A Cognito authorizer on the API validates the token's signature and expiry and rejects invalid requests. The Lambda function behind the API then reads the user's claims, such as user ID and groups, from the request context and uses them for authorisation.

Application Load Balancers offer the same protection through an authentication action, which signs users in through Cognito before forwarding requests. AWS WAF can protect user pool endpoints against credential stuffing and other automated attacks.

The following diagram shows the serverless flow described above.

Cognito as an API authorizer. A user signs in to a Cognito user pool and receives a JWT. The application sends it with each request to API Gateway, whose Cognito authorizer validates the signature and expiry and rejects invalid requests. The Lambda function behind the API reads the user's claims, such as user ID and groups, from the request context.
Invalid requests never reach the Lambda function, so the function does not need to validate tokens itself.
True or false

Cognito as an authorizer

Exam skills 1.2.3 · 2.1.5

Check how Cognito tokens protect APIs and load balancers.

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

Moving users from a legacy login system

An application is moving its 200,000 users from a self-built login database to Cognito, without forcing everyone to reset their password, and must add MFA for risky sign-ins.

RequirementCognito feature
Move users as they sign in, keeping their passwordsUser migration Lambda trigger, which checks the old database and creates the user
Add the customer's plan to tokensPre token generation trigger
Require MFA only when a sign-in looks riskyThreat protection (adaptive authentication)
Sign in with Google as wellGoogle as a federated identity provider

Users who never sign in again are not migrated; after a set period the old database is retired and those users reset their passwords.

Check yourself

Which Cognito feature adds sign-in with Google or Apple to a web application?

Show answer

Federation with social identity providers in a user pool. Users sign in through the provider, and the user pool issues its own tokens to the application.

Exam Tips

Remember for the exam

  • Sign-up and sign-in for application users, with social login, points to a Cognito user pool.
  • To authenticate users at the ALB without application code, use ALB authentication with Cognito.
  • To migrate users from an old database without forcing a password reset, use the user migration Lambda trigger.
  • To require MFA only for risky sign-ins, use Cognito threat protection (adaptive authentication).
  • To secure an API Gateway API for signed-in users without code, use a Cognito user pool authorizer.