AWS Cognito vs Keycloak: User Pools, App Clients and When a Managed Identity Provider Wins
Cognito is AWS's identity service, Keycloak as a service. Its three pieces (user pool, app client, hosted UI), the tokens it issues, a side-by-side with Keycloak on running costs and lock-in, and why the rest of the subtitle app doesn't change when the door is swapped.
What you will learn
- Describe Cognito's user pool, app client and hosted UI, and map each to Keycloak
- List the tokens and features Cognito gives you
- Compare Keycloak and Cognito on who runs it, cost and where the users live
- See why only one box changes when an app moves from Keycloak to Cognito
Before you start
- How an identity provider login works
So far the subtitle app's identity provider has been Keycloak, running in Docker next to the backend. Now it moves to AWS Cognito. Before any code, here's what Cognito is and why it's the better fit for this app.
The migration itself was handed to a coding agent, the Codex app from OpenAI, set to auto-review permissions and high intelligence, with a plain-English prompt: scan the project (backend, front end, Keycloak as the identity provider), migrate everything to AWS Cognito including the controllers and business logic, and create a sample .env with the details you need. While it worked, this is what there was to learn.
What Cognito is
Cognito is the identity service of AWS. It handles sign up, sign in and the tokens your app checks, which is the same job Keycloak does. The difference is who runs it. With Keycloak you run the container, its database and its upgrades. With Cognito, AWS runs all of that. So you can think of Cognito as Keycloak as a service. It speaks the same standard protocols, OAuth 2 and OpenID Connect, and issues signed JWT tokens, just like Keycloak.
Three pieces
- User pool. The directory of your users, where they sign up, sign in and keep their passwords and details. Close to a Keycloak realm.
- App client. How one application talks to the user pool, with its own client ID. The React front end uses one. In Keycloak, that's the client.
- Hosted UI. A ready-made sign in and sign up page that Cognito serves on its own domain, like the Keycloak login page.
The user signs in on the hosted UI, Cognito checks them against the user pool, and sends the tokens back to the app.
What you get
- An ID token that says who the user is, and an access token that your API checks. Both are signed JWTs. A refresh token gets new ones when they expire.
- Multi-factor authentication built in.
- Social login with Google, Facebook, Apple or Amazon in a few settings.
- Single sign on with a company's identity provider through SAML or OpenID Connect.
- Scaling and patching by AWS.
Side by side
| Keycloak | Cognito | |
|---|---|---|
| Who runs it | You | AWS |
| Protocols | OAuth 2, OpenID Connect, JWT | OAuth 2, OpenID Connect, JWT |
| Social login and SSO | Yes, more for you to configure and maintain | Yes, a few settings |
| Cost | The server it runs on, all the time | By monthly active users, with a free tier |
| Where users live | Wherever you host it, no vendor lock-in | Your AWS account, in the region you choose; moving out later is harder |
Microsoft Azure and Google Cloud have similar managed services. If your app runs on their cloud, they're worth a look.
Why switch?
Keycloak is excellent, but for a small team building an AI subtitle app, Cognito wins on three things. Zero operations: there's nothing to babysit. Social login in a few clicks. And it stays cheap as you grow. At a company where I worked as a machine learning engineer, we ran Keycloak, and the patches and upgrades kept coming back. Once we moved to Cognito, life got much easier. It's the best fit for apps already on AWS, and this app already keeps its videos in S3.
Here's the Keycloak work that simply goes away: no container to run and no database behind it, no realm file to import, no TLS certificates (every Cognito endpoint is already HTTPS), no patches, no scaling worries and no backups.
The cost model
Cognito charges by monthly active users. If a hundred people sign up but only ten use the app this month, you pay for those ten. While you develop and test, a small app stays inside the free tier. Prices change, so check the Cognito pricing page for current numbers. Even as a product grows, it's usually cheaper than running a server for Keycloak with its database and backups.
Only one box changes
In the subtitle app's architecture, the Keycloak box goes out and Cognito comes in. The React front end still sends the user to sign in, and FastAPI still checks the token on every call. The Celery worker, Redis, Whisper and S3 don't change at all. The code side of the move is in migrating to Cognito and verifying its tokens.
Quick recap
- Cognito is the managed identity service of AWS: Keycloak as a service.
- A user pool holds the users, an app client connects your app, the hosted UI gives sign in pages.
- Same protocols and signed JWTs, so the design stays the same.
- You trade running costs and upgrades for a per-active-user bill and users stored in your AWS account.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
The Python Architect Masterclass: Full Stack to AI Engineer
Python, Django, FastAPI, React, Docker, AWS, LLMs, RAG, agents and MCP, from first steps to AI engineer.View on Udemy ↗: The Python Architect Masterclass: Full Stack to AI Engineer (paid course, opens in a new tab)Related posts
How a Request and a Login Flow Through a FastAPI Blog: Depends, bcrypt, a Signed Session Cookie and async def
Follow GET / through the FastAPI blog to two SQL statements and an HTML page, then a login: one SELECT, a bcrypt check, a 303 with a signed session cookie that decodes to user_id 1. A changed cookie gets 401, a plain user 403, and three slow requests on async def with a blocking session finish at 1.92 s while a ping waits 1.83 s.
An Auth Starter Kit with Keycloak: Build Sign In Once, Put the Backend Between React and the Identity Provider
Almost every project needs sign in, sign out and a token check, so build them once in a starter kit. Tour the kit's four pillars, then finish it: a fix prompt, a backend that sits between React and Keycloak with register, login, me and logout routes, and the limits of a stateless access token.
Protecting a Python API with Keycloak: Check the JWT Signature, Issuer and Expiry in FastAPI, Django and Flask
Keycloak signs the access token; your API only checks it. Run Keycloak in Docker with a realm file, fetch its public keys once, check RS256, the issuer and the expiry, and protect /api/me with one Depends. Then the same check as a Django REST framework class and a Flask decorator.
How an Identity Provider Login Works: The OpenID Connect Code Flow vs a Backend Password Grant, with Keycloak
What an identity provider does, the four Keycloak words you see everywhere, the standard OpenID Connect sign in (redirect, code, token), and the custom-page version where FastAPI sends a password grant. Then one token followed from login to the check, plus refresh, logout and dev mode.
