Export Cognito Users in AWS
As of today, there is no way to directly export users from Cognito in AWS.
But we can use AWS CLI or AWS SDK to get the list of users.
- First step is to install AWS CLI on your machine
Click here to download and install AWS CLI
- Next, step is to configure AWS on your machine. To do this, open cmd (command prompt) and do the following –
$ aws configure
AWS Access Key ID [None]: YourAccessKeyId
AWS Secret Access Key [None]: YourSecretAccessKey
Default region name [None]: YourRegion
Default output format [None]: json
Replace the above with your values. For more info, click here.
- To get the list of all users in Cognito, run the following command
aws cognito-idp list-users --region <region> --user-pool-id <userPoolId>
--output json > users.json
The above will return the list of users in a json format. If you want to get the result in a table format, run the following command
aws cognito-idp list-users --region <region> --user-pool-id <userPoolId> --output table > users.txt
- Now if you want to convert the result json to csv. Use the following code snippet.
private static void ConvertJsonToCsv()
{
using (var csv = new ChoCSVWriter("users.csv").WithFirstLineHeader())
{
using (var json = new ChoJSONReader("CognitoUsers.json")
.WithField("Username")
.WithField("Email", jsonPath: "$.Attributes[?(@.Name ==
'email')].Value", isArray: true)
.WithField("UserStatus")
)
{
csv.Write(json);
}
}
}
Like this:
Like Loading...