Skip to content

Instantly share code, notes, and snippets.

@thiskevinwang
Last active July 12, 2024 17:15
Show Gist options
  • Save thiskevinwang/901025cfdabb3770f804e8534ae9aa64 to your computer and use it in GitHub Desktop.
Save thiskevinwang/901025cfdabb3770f804e8534ae9aa64 to your computer and use it in GitHub Desktop.
cognito_test

Description

A .ts script to output information about a Cognito user pool. This uses the V3 JS client to call describe-user-pool

This is simply one means, amongst many, to fetch user pool info.

Setup

user@~: $ mkdir cognito_test
user@~: $ cd cognito_test
user@~/cognito_test: $ npm init -y
user@~/cognito_test: $ npm i @aws-sdk/client-cognito-identity-provider
user@~/cognito_test: $ npm i dotenv

user@~/cognito_test: $ touch .env.local

Reference the sample .env.local in the gist below

user@~/cognito_test: $ touch describe-user-pool.ts

Feel free to use the snippet in the gist below

Running

user@~/cognito_test: $ npx tsx describe-user-pool.ts

{
  "$metadata": {
    "httpStatusCode": 200,
    "requestId": "25ebbdaf-ad6b-44fb-83a6-2cce27756660",
    "attempts": 1,
    "totalRetryDelay": 0
  },
  "UserPool": {
    "AccountRecoverySetting": {
      "RecoveryMechanisms": [
      ... etc.
AWS_ACCESS_KEY_ID=
AWS_SECRET_ACCESS_KEY=
AWS_REGION=us-east-1
COGNITO_USER_POOL_ID=
import * as IDP from "@aws-sdk/client-cognito-identity-provider";
import { config } from "dotenv";
config({ path: __dirname + "/.env.local", debug: true, encoding: "UTF-8" });
// AWS IAM Permissions
// This assumes [AmazonCognitoPowerUser] but should be narrowed down if possible.
const { AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_REGION } = process.env;
const { COGNITO_USER_POOL_ID } = process.env;
const idpClient = new IDP.CognitoIdentityProviderClient({
region: AWS_REGION,
credentials: {
accessKeyId: AWS_ACCESS_KEY_ID,
secretAccessKey: AWS_SECRET_ACCESS_KEY,
},
});
async function main() {
await idpClient
.send(
new IDP.DescribeUserPoolCommand({
UserPoolId: COGNITO_USER_POOL_ID,
})
)
.then((data) => {
console.log(JSON.stringify(data));
});
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment