Skip to content

AWS Cognito โ€” Authentication & Authorization

Amazon Cognito is AWSโ€™s managed authentication and user management service. It provides user registration, login, social federation, and JWT-based access control for web and mobile applications.

In Azure terms: Cognito = Azure AD B2C + aspects of Microsoft Entra External ID

A User Pool is a user directory that provides:

  • User registration and sign-in (username/password, email, phone)
  • Email/SMS verification and MFA
  • Social identity federation (Google, Facebook, Apple, SAML, OIDC)
  • Customizable UI (Hosted UI โ€” pre-built sign-in pages)
  • JWT tokens: ID token, Access token, Refresh token
  • Password policies, account recovery flows
  • Lambda triggers for custom auth flows

An Identity Pool (Federated Identities) provides:

  • Exchange a User Pool token (or any OIDC/SAML token) for temporary AWS credentials (STS)
  • Allows mobile/web apps to access AWS services directly (S3, DynamoDB)
  • Supports guest (unauthenticated) access
  • Different IAM roles for authenticated vs unauthenticated users
User signs in โ†’ User Pool (JWT tokens)
โ†“
Identity Pool โ†’ STS assume role
โ†“
AWS Credentials (Access Key + Secret + Session)
โ†“
App calls S3 / DynamoDB directly

After successful authentication, Cognito returns three JWTs:

TokenLifetimePurpose
ID Token1 hourContains user identity claims (email, name, custom attributes)
Access Token1 hourUsed to authorize API calls; contains groups/scopes
Refresh Token30 days (default)Refresh ID and Access tokens without re-login

Cognito provides a pre-built, customizable login page โ€” no auth UI to build:

https://your-domain.auth.us-east-1.amazoncognito.com/login
?client_id=abc123
&response_type=code
&scope=openid+email+profile
&redirect_uri=https://myapp.com/callback

Customize authentication flows with Lambda:

TriggerWhenUse Case
Pre sign-upBefore user createdBlock sign-ups from certain domains
Post confirmationAfter email verifiedCreate user record in DynamoDB
Pre authenticationBefore loginCheck if user is allowed to log in
Post authenticationAfter successful loginLog sign-in event
Pre token generationBefore JWT issuedAdd custom claims to tokens
Custom authCustom challenge flowPasswordless (OTP) login

Protect API Gateway endpoints using a Cognito Authorizer:

Client โ†’ signs in via Cognito โ†’ gets JWT Access Token
Client โ†’ calls API Gateway with Authorization: Bearer <token>
API Gateway โ†’ validates token with Cognito User Pool
API Gateway โ†’ routes request to Lambda (if valid)
FeatureCognito User PoolsAzure AD B2C
User registrationYesYes
Social loginGoogle, Facebook, Apple, SAML, OIDCGoogle, Facebook, Amazon, GitHub, etc.
Custom UIHosted UI (customizable CSS)Custom policies + page templates
MFATOTP, SMSTOTP, Phone, Email
TokensJWT (ID, Access, Refresh)JWT (ID, Access, Refresh)
User attributesPreset + customCustom via user flows
Lambda integrationLambda triggersCustom policies (XML-based)
OIDC/OAuth2YesYes
PricingFirst 50,000 MAUs freeFirst 50,000 MAUs free
Terminal window
# Create a User Pool
aws cognito-idp create-user-pool \
--pool-name my-user-pool \
--auto-verified-attributes email \
--policies PasswordPolicy="{MinimumLength=8,RequireUppercase=true}"
# Create a User Pool Client (app)
aws cognito-idp create-user-pool-client \
--user-pool-id us-east-1_abc123 \
--client-name my-web-app \
--generate-secret
# Sign up a user
aws cognito-idp sign-up \
--client-id abc123 \
--username alice@example.com \
--password MyPassword123!
# Confirm user (admin)
aws cognito-idp admin-confirm-sign-up \
--user-pool-id us-east-1_abc123 \
--username alice@example.com
# Admin log in (get tokens)
aws cognito-idp initiate-auth \
--auth-flow USER_PASSWORD_AUTH \
--client-id abc123 \
--auth-parameters USERNAME=alice@example.com,PASSWORD=MyPassword123!