Have you ever implemented a user database and authentication layer yourself? There are many things to get right: Hashing and salting passwords, multi-factor authentication, brute force attacks, and many more. That’s why I recommend using a production-ready service instead of building authentication yourself. That’s what Amazon Cognito is all about.
[wpcc-element _tag=”source” type=”image/webp” srcset=”/images/2021/03/login@730w.webp 730w, /images/2021/03/login@730w2x.webp 1460w, /images/2021/03/login@610w.webp 610w, /images/2021/03/login@610w2x.webp 1220w, /images/2021/03/login@450w.webp 450w, /images/2021/03/login@450w2x.webp 900w, /images/2021/03/login@330w.webp 330w, /images/2021/03/login@330w2x.webp 660w, /images/2021/03/login@545w.webp 545w, /images/2021/03/login@545w2x.webp 1090w” sizes=”(min-width: 1200px) 730px, (min-width: 992px) 610px, (min-width: 768px) 450px, (min-width: 576px) 330px, 545px” _close=”0″]
Let me start with typical use cases for Cognito:
Serverless app with API Gateway
A typical Serverless application uses Cognito User Pools for authentication and authorization.
[wpcc-element _tag=”source” type=”image/webp” srcset=”/images/2021/03/api-gateway-user-pool@730w.webp 730w, /images/2021/03/api-gateway-user-pool@730w2x.webp 1460w, /images/2021/03/api-gateway-user-pool@610w.webp 610w, /images/2021/03/api-gateway-user-pool@610w2x.webp 1220w, /images/2021/03/api-gateway-user-pool@450w.webp 450w, /images/2021/03/api-gateway-user-pool@450w2x.webp 900w, /images/2021/03/api-gateway-user-pool@330w.webp 330w, /images/2021/03/api-gateway-user-pool@330w2x.webp 660w, /images/2021/03/api-gateway-user-pool@545w.webp 545w, /images/2021/03/api-gateway-user-pool@545w2x.webp 1090w” sizes=”(min-width: 1200px) 730px, (min-width: 992px) 610px, (min-width: 768px) 450px, (min-width: 576px) 330px, 545px” _close=”0″]
The client authenticates with the User Pool.
The client sends the identity or access token as a header to the API Gateway.
The API Gateway verifies the token and grants access.
The API Gateway adds information from the identity/access token to the context when invoking a Lambda function.
Obtain temporary AWS credentials
Cognito Identity Pool allows federated identities to access AWS resources by issuing temporary AWS access credentials. Users can use those credentials to interact with AWS APIs directly (e.g., AppSync, S3, DynamoDB, …). That allows clients to access data stores without a backend.
[wpcc-element _tag=”source” type=”image/webp” srcset=”/images/2021/03/temp-aws-credentials@730w.webp 730w, /images/2021/03/temp-aws-credentials@730w2x.webp 1460w, /images/2021/03/temp-aws-credentials@610w.webp 610w, /images/2021/03/temp-aws-credentials@610w2x.webp 1220w, /images/2021/03/temp-aws-credentials@450w.webp 450w, /images/2021/03/temp-aws-credentials@450w2x.webp 900w, /images/2021/03/temp-aws-credentials@330w.webp 330w, /images/2021/03/temp-aws-credentials@330w2x.webp 660w, /images/2021/03/temp-aws-credentials@545w.webp 545w, /images/2021/03/temp-aws-credentials@545w2x.webp 1090w” sizes=”(min-width: 1200px) 730px, (min-width: 992px) 610px, (min-width: 768px) 450px, (min-width: 576px) 330px, 545px” _close=”0″]
Cloud-native app with ALB
You can add authentication to any cloud-native app that sits behind an ALB. ALBs support authentication/authorization based on Cognito User Pools.
[wpcc-element _tag=”source” type=”image/webp” srcset=”/images/2021/03/alb-user-pool@730w.webp 730w, /images/2021/03/alb-user-pool@730w2x.webp 1460w, /images/2021/03/alb-user-pool@610w.webp 610w, /images/2021/03/alb-user-pool@610w2x.webp 1220w, /images/2021/03/alb-user-pool@450w.webp 450w, /images/2021/03/alb-user-pool@450w2x.webp 900w, /images/2021/03/alb-user-pool@330w.webp 330w, /images/2021/03/alb-user-pool@330w2x.webp 660w, /images/2021/03/alb-user-pool@545w.webp 545w, /images/2021/03/alb-user-pool@545w2x.webp 1090w” sizes=”(min-width: 1200px) 730px, (min-width: 992px) 610px, (min-width: 768px) 450px, (min-width: 576px) 330px, 545px” _close=”0″]
We use it to add a layer of security for traditional tools like Jenkins, phpMyAdmin, etc. You can find a working example here: https://github.com/cfn-modules/docs/tree/master/examples/fargate-alb-auth-cognito
The following video covers:
When to use AWS Cognito?
Demo: ALB + Cognito User Pool
User Pool vs. Identity Pool
Pitfall: Backup your most valuable asset!
[wpcc-iframe class=”embed-responsive-item lozad” data-src=”https://www.youtube-nocookie.com/embed/XWrzRwSEXl4″ allow=”accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture” allowfullscreen=””]
Summary
The following table helps you to decide which option to choose.
User Pool
Identity Pool
Built-in User Database
✅ Yes
❌ No
Grant access to AWS resources directly
✅ Yes in combination with an Identity Pool
✅ Yes
Social Login
Google, Facebook, Amazon, Apple
Google, Facebook, Amazon, Apple
SAML
✅ Yes
✅ Yes
OpenID Connect
✅ Yes
✅ Yes
Hosted UI
✅ Yes
❌ No
Costs
$0.0055 per Monthly Active User (MAU)
Free