Skip to Content
Asterrr's Handbook
4 · Identity and access management

Cognito and API Gateway authorizers

Amazon Cognito user pools and identity pools, guest access and token types, and the API Gateway authorizers that check them.

Exam tasks: 4.1 (Skills 4.1.1 identity for application users with Amazon Cognito, 4.1.3 troubleshooting Cognito)

The decision: does your application's user need to sign in (a user pool) or call AWS directly with credentials (an identity pool), and where is each token checked?

Cognito user pools sign application users in, identity pools swap a token for temporary AWS credentials, and API Gateway authorizers validate tokens in front of your API. Fine-grained rules over your own resources belong to Amazon Verified Permissions.

sign inID + access tokensaccess tokenauthorizerID tokentemporary AWS creds (IAMrole)App userCognito userpool or social /SAML / OIDC IdPWeb or mobile appAPI GatewayVerifiedPermissions(Cedar policies)Lambda /containersCognito identitypoolS3, DynamoDBdirectly
Diagram of the moving parts

User pools vs identity pools

User poolIdentity pool
JobAuthentication: a user directory and OIDC/OAuth 2.0 providerCredential vending: swaps a token for temporary AWS credentials
OutputJWTs: ID, access and refresh tokensAn access key, secret and session token for an IAM role
Federates withSocial IdPs, SAML, OIDCUser pools, social IdPs, SAML, OIDC, your own backend (developer authenticated)
Guest usersNoYes, unauthenticated identities with their own role
Use whenUsers sign in to your app or APIThe app calls S3, DynamoDB, IoT and so on directly
  • Token types: the ID token carries user attributes (who the user is). The access token carries scopes and groups (what the app may call), and is the one to send to APIs. The refresh token gets new ones. You can revoke refresh tokens, and access tokens issued from them stop working at Cognito endpoints.
  • Machine-to-machine: the client credentials grant with resource server scopes issues access tokens to services, no user involved.
  • Feature plans: Lite, Essentials (the default for new pools) and Plus. Threat protection (compromised credential checks, adaptive authentication that scores risky sign-ins and can require MFA or block) is in Plus. Passkeys, email MFA and access token customization need Essentials or Plus.

Legacy: use User pool threat protection (Plus plan) instead

"Advanced security features" in Cognito were folded into the Essentials and Plus feature plans. Compromised credential detection and adaptive authentication now come with the Plus plan.

Identity pools in detail

  • Each identity is authenticated or unauthenticated (guest), and each type maps to an IAM role. Keep the guest role tiny, for example PutObject to one upload prefix.
  • Role selection: a default role, rules that map a token claim to a role, or the cognito:preferred_role claim from user pool groups.
  • Attributes for access control: map token claims to principal tags, then write ABAC policies such as "only objects under users/${aws:PrincipalTag/sub}/".
  • Role trust policies must trust cognito-identity.amazonaws.com and pin cognito-identity.amazonaws.com:aud to your identity pool ID and amr to authenticated or unauthenticated.
  • The enhanced flow is the default. The basic (classic) flow lets the client call AssumeRoleWithWebIdentity itself and bypasses role rules, so leave it off unless you need it.

A guest role with broad access

Unauthenticated identities are available to anyone who has the identity pool ID, which ships in your app. A guest role with s3:* on a bucket is a public bucket. Scope it to the minimum, or turn off guest access.

API Gateway authorizers

AuthorizerChecksGood for
IAM (SigV4)The caller signed with AWS credentials allowed to execute-api:InvokeService-to-service, identity pool users
Cognito user pool (REST)A valid user pool token, optional scopesStraightforward sign-in-then-call
JWT authorizer (HTTP APIs)Any OIDC issuer's JWT, audience and scopesCognito or third-party IdPs on HTTP APIs
Lambda authorizerWhatever your code decides, returning an IAM policyCustom tokens, calls to Verified Permissions, legacy auth
Resource policy (REST)Source account, IP, VPC endpointPrivate APIs, IP allow-lists, cross-account
  • Mutual TLS on a custom domain authenticates clients by certificate, in addition to any authorizer.
  • Cache Lambda authorizer results carefully: a cached allow for one path can be reused for others with the same token if the returned policy is too broad.

Scenarios

Scenario
A mobile photo app lets users browse public galleries without signing in, and upload photos to their own folder after signing in. Uploads go straight from the app to S3. Which design follows least privilege?
Scenario
Users of a web app report that sign-in works but calls to the HTTP API return 401 Unauthorized. The API uses a JWT authorizer configured with the user pool's issuer and the app client ID as audience, and each route requires the orders/read scope. The front end sends the ID token in the Authorization header. Logs show the token is valid. What is the most likely issue?

Further reading

On this page