Moving users from Amazon Cognito to Keycloak
A Cognito user pool holds users with a username, a set of standard attributes taken from OpenID Connect (email, given_name, family_name, phone_number and others), any custom attributes you added with the custom: prefix, a status and an enabled flag. Keycloak holds the same facts as a username, email, first and last name, attributes and an enabled flag.
This converter reads Cognito users in either of the two shapes people have: the JSON of aws cognito-idp list-users, or a CSV whose header uses Cognito’s attribute names, the same names as Cognito’s own import template. It writes a Keycloak partial import or realm import file, in your browser.
For the whole move, with the plan, the cut-over and what each of our plans covers, see moving from Amazon Cognito.
Getting users out of Cognito
Cognito has an import job for CSV files but no export job. To get users out, you list them through the API. list-users returns up to 60 users per page with a pagination token; the AWS CLI follows the token for you.
aws cognito-idp list-users --user-pool-id <pool-id> --output json > users.jsonEach user comes back with Username, Attributes as a list of name and value pairs, Enabled, UserStatus, UserCreateDate, UserLastModifiedDate and, for older pools, MFAOptions. There is no password, no hash and no group list.
Groups are listed per user with admin-list-groups-for-user, or per group with list-users-in-group. If you want groups in Keycloak, collect them with a short script and add a groups column to a CSV, with several names separated by semicolons. The tool turns them into group paths.
AWS publishes a reference architecture for exporting Cognito user profiles and groups. Its own limits say it does not export sensitive information such as passwords, and that it does not support pools with MFA turned on.
Why passwords do not move
Cognito does not give out passwords or password hashes. The list-users response has no field for them, and AWS’s own export guidance names passwords as data it does not export. Cognito can import hashes from other systems, but nothing comes back the other way.
So every Cognito user who signs in with a password sets a new one after the move. The tool marks them with Keycloak’s UPDATE_PASSWORD required action. After the import, an admin or a script sends each of them a set-password link with Keycloak’s execute-actions-email endpoint.
Users who sign in through a social or SAML provider are different. Cognito names them after the provider, such as Google_1000…, and gives them the status EXTERNAL_PROVIDER. The tool treats them as federated: no password step, and a link to their account by email at their first sign-in through the same provider in Keycloak.
What the tool does with each field
| Cognito | Keycloak user | Notes |
|---|---|---|
email | email, in lower case | Also the username by default |
email_verified | emailVerified | TRUE or true both work |
given_name, family_name | firstName, lastName | From name when both are empty |
cognito:username or Username | username when you choose the id | |
sub | The cognito_sub attribute | The id your apps saw in tokens |
Enabled | enabled | Missing means enabled |
UserCreateDate | createdTimestamp | Seconds become milliseconds |
custom:<name> | The <name> attribute | Without the prefix |
phone_number, locale, zoneinfo and other standard ones | Attributes of the same name | |
A groups column you add | groups, as paths |
Read and left out: cognito:mfa_enabled and MFAOptions, phone_number_verified (Keycloak has no verified flag for phone numbers), identities, and the modification dates.
The CSV format
Cognito’s import CSV does not quote values. A comma inside a value is written as a backslash and a comma, so 1 Sample Street\, Sample Town is one address. The tool reads that, and quoted fields as spreadsheets write them. Column order does not matter, and empty cells are skipped.
The sub and your app data
Apps often store Cognito’s sub as the key for a user’s data, because it is the subject of Cognito’s tokens. Keycloak gives each user its own id, so the subject changes after the move. Keep cognito_sub as an attribute, and either look users up by it on their first sign-in, or add a token mapper so the token carries it under a claim your app reads.
The output
{
"users": [
{
"username": "[email protected]",
"email": "[email protected]",
"emailVerified": true,
"firstName": "Sample",
"lastName": "One",
"enabled": true,
"attributes": {
"phone_number": ["+15555550101"],
"department": ["Sales"],
"cognito_sub": ["00000000-0000-4000-8000-000000000001"]
},
"groups": ["/staff"],
"requiredActions": ["UPDATE_PASSWORD"]
}
]
}The preview shows the first 20 users; the saved file has every user. With groups on, the file lists the group tree too, so partial import creates the groups first.
Lambda triggers and app clients
Cognito pools often carry logic in Lambda triggers: pre sign-up checks, custom messages, token claims added in a pre token generation trigger, or a user migration trigger. None of that moves on its own. List each trigger and decide its Keycloak counterpart: a required action, a client scope mapper for token claims, or a change in your app.
App clients become Keycloak clients. Re-create each one with its callback URLs, allowed scopes and whether it has a secret. Public clients that used the Cognito hosted UI move to Keycloak’s authorization code flow with PKCE. The PKCE generator and the client JSON builder help with that part.
Adding groups to the export
Because Cognito lists groups apart from users, most teams build the CSV in two passes: first the users from list-users, then one call per user for their groups. A small script does it; the one below uses the AWS CLI and jq, and writes the columns the converter reads.
POOL=<pool-id>
echo "cognito:username,sub,email,email_verified,given_name,family_name,groups"
aws cognito-idp list-users --user-pool-id "$POOL" --output json \
| jq -c '.Users[] | {u: .Username, a: (.Attributes | from_entries)}' \
| while read -r row; do
user=$(echo "$row" | jq -r .u)
groups=$(aws cognito-idp admin-list-groups-for-user --user-pool-id "$POOL" \
--username "$user" --query 'Groups[].GroupName' --output text | tr '\t' ';')
echo "$row" | jq -r --arg g "$groups" \
'[.u, .a.sub, .a.email, .a.email_verified, .a.given_name, .a.family_name, $g] | @csv'
doneRun it from a machine with credentials that may read the pool, and keep the output file as private as the export itself.
One call per user is slow for a large pool, and Cognito limits how fast admin calls may run. For large pools, list each group’s members with list-users-in-group instead: one call per group and page, then join the two lists by username.
What your users see
Password users get one email from Keycloak with a link. It opens Keycloak’s Update Password page; they choose a password and are signed in. If the link has expired, Forgot password on the sign-in page sends a fresh one, as long as the realm allows it.
Users from Google or another provider see the provider button on Keycloak’s sign-in page. The first time, Keycloak tells them an account with their email exists and asks them to confirm the link, by email or by signing in. After that, sign-in is one click as before.
Everyone signs in once after the switch, because Cognito sessions and refresh tokens do not carry over. Apps that keep a Cognito refresh token for weeks should handle a failed refresh by sending the user to sign in.
The sign-in page changes too. Cognito’s hosted UI gives way to Keycloak’s own pages, in Keycloak’s standard theme, at your realm’s address. Custom themes are not supported on our clusters yet, so tell users the page will look different.
Mistakes to avoid
- Waiting for a hash export from AWS. There is none: plan the set-password emails from the start.
- Dropping
suband losing the link between users and your app’s data. - Treating
Google_users as password users. They would get a set-password email they do not need. - Forgetting the custom attributes in the realm’s user profile. Keycloak drops undeclared attributes.
- Skipping the dry run. Import into a staging realm and sign in through each app first.
Where we come in
On Premium and Enterprise, our team does the move with you within the plan’s 10 service hours a month. On Starter, you use these tools and guides, with written support. Very large pools are scoped as a Custom plan through the contact form. The readiness checklist has every step for a Cognito move, and the email templates help you prepare your users.