AWS Builder Center
Amazon Cognito: Securing User Authentication in Your Apps

Amazon Cognito: Securing User Authentication in Your Apps

How Cognito handles sign up, sign in and user security for your applications.

Series: AWS Security 30 Days, 30 Articles Challenge (31 articles)

  1. …
  2. 26
    Amazon Cognito: Securing User Authentication in Your Apps This article
Welcome to Day 25 of the 30 Days AWS Security Challenge. Everything we've covered on IAM so far has been about controlling access for people and systems managing your AWS account. Today we shift to a different audience entirely: the actual end users of your application, and how Amazon Cognito handles authentication for them.

Why IAM Isn't the Right Tool Here

It might seem natural to reuse IAM for your application's users, but IAM is built for managing access to AWS resources, not for handling potentially millions of end users signing up for your app with an email and password. Building your own authentication system from scratch means handling password hashing, account recovery, session management, and a dozen other details correctly, and getting any one of them wrong creates a real vulnerability. Cognito exists to give you a secure, ready made system for exactly this problem.

The Two Halves of Cognito

Cognito is really two related but distinct services, and it helps to know the difference:
  • User Pools handle sign up, sign in, and user directory management. This is where your users' accounts actually live, along with their profile attributes and authentication settings.
  • Identity Pools handle giving users temporary AWS credentials once they're authenticated, so your application can let a logged in user directly access specific AWS resources, like uploading a file to S3, without routing that request through your own backend.
Most applications start with just a User Pool, and add an Identity Pool later if they need users to interact directly with AWS resources from a client application.

What User Pools Handle for You

Once you set up a User Pool, Cognito manages the security sensitive parts of authentication that are easy to get wrong when built manually:
  • Secure password storage, using proper hashing, so you never handle or store raw passwords yourself
  • Account verification through email or SMS during sign up
  • Password reset and account recovery flows
  • Support for MFA for your application's own users, the same concept from Day 5, now applied to your customers rather than your AWS account
  • Protection against common credential attacks, like automatically throttling repeated failed login attempts

Setting Up a Basic User Pool

To create one, go to Cognito > User Pools > Create user pool, choose how users will sign in, like email or username, and configure your password policy and MFA requirements. Cognito lets you require MFA for all users, make it optional, or skip it entirely, though for anything handling sensitive user data, requiring it is worth the small amount of added friction during sign up.

Federation: Letting Users Sign In With Existing Accounts

Cognito also supports federated identity, letting users sign in with an existing account from a provider like Google, Facebook, or Apple, or through a corporate identity provider using SAML. This means you don't need to store and protect a password for every user, since authentication for federated users happens with the external provider instead, and Cognito just manages the resulting session in your application.

Connecting Users to AWS Resources Safely

This is where Identity Pools come back in. Once a user authenticates through a User Pool, an Identity Pool can exchange that authentication for temporary AWS credentials, following the same idea we discussed with IAM roles on Day 3. This lets your mobile or web application allow a signed in user to upload a photo directly to a specific S3 folder scoped to that user, without needing your backend to broker every single request, while still keeping tightly scoped permissions defined through an IAM role tied to the Identity Pool.

A Practical Example

Say you're building a mobile app where users upload photos. You set up a Cognito User Pool for sign up and sign in, requiring MFA for account security, and enable Google as a federated sign in option to reduce friction. Once authenticated, an Identity Pool issues the user temporary credentials scoped through an IAM role that only allows uploading to their own specific folder within an S3 bucket. Your application never handles raw passwords, and the user never gets broader access to your S3 bucket than their own uploads.

Key Takeaways

  • Cognito is purpose built for application user authentication, distinct from IAM which manages access to AWS resources themselves
  • User Pools handle sign up, sign in, password security, and MFA for your application's users
  • Identity Pools exchange authenticated sessions for temporary, scoped AWS credentials
  • Federated sign in lets users authenticate with existing accounts like Google or a corporate provider, reducing password related risk
  • Require MFA for applications handling sensitive user data, the same reasoning from Day 5 applies to your customers too
Tomorrow we zoom all the way out and look at the AWS Well-Architected Framework's Security Pillar, which ties together nearly everything we've covered this month into a single set of design principles.

Series: AWS Security 30 Days, 30 Articles Challenge (31 articles)

  1. …
  2. 26
    Amazon Cognito: Securing User Authentication in Your Apps This article
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article