List Users in the API

  1. At IAM Interface
  • Choose Create policy
  1. Choose JSON
  • Copy policy and paste it
{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "VisualEditor0",
      "Effect": "Allow",
      "Action": "cognito-idp:ListUsers",
      "Resource": "arn:aws:cognito-idp:<your region>:<account id>:userpool/<user pool id>"
    }
  ]
}

3. Policy name: Lambda-User-Pool-ChatApp 4. Choose Create policy 5. At IAM Interface

  • Find Role: Lambda-Role-ChatApp
  • Choose Add permissions
  • Choose Attach policies
  • Find policy: Lambda-User-Pool-ChatApp
  • Choose: Add permissions
  1. At IAM Interface
  • Choose Create Role
  • Choose Service: Lambda
  • Choose Next
  • Add permission: Lamda-User-Pool-ChatApp
  • Add permission: AWSLambdaBasicExecutionRole
  • Role-name: Lambda-Cognito
  • Choose Create Role
  1. At Lambda function
  • Choose Create function
  • Function name: Chat-User-GET
  • Runtime: Node.js 18.x
  1. Using an existing role: Lambda-Cognito
  • Create function
  1. Copy this code and paste it
import {CognitoIdentityProviderClient, paginateListUsers} from '@aws-sdk/client-cognito-identity-provider';

const cognito = new CognitoIdentityProviderClient({});

export const handler = async function () {
    const paginator = paginateListUsers({client: cognito}, {
        UserPoolId: '<user pool id>',
        AttributesToGet: [],
        Filter: '',
        Limit: 60
    });

    let logins = [];

    for await (const page of paginator) {
        for (const user of page.Users) {
            logins.push(user.Username);
        }
    }

    return logins;
};
  • Choose Deploy
  1. At ChatPool
  • Check User pool ID
  • Check ARN
  1. At API interface
  • Choose Models
  • Choose create model
  • Name: userList
  • Content type: application/json
  • Model schema
{
  "type":"array",
  "items": {
    "type":"string"
  }
}
  • Choose Create
  1. At API interface
  • Choose Create resource
  • Resource path: /
  • Resource name: users
  • Choose Create resource
  1. Choose Create Method
  • Method: GET
  • Lambda function
  • Lambda function: Chat-User-GET
  • Choose Create method
  1. Choose Create Method request
  • Choose Edit
  1. At Response body
  • Content type: application/json
  • Model: userList
  1. Choose Test
  2. Test
  3. Check the results
  4. At Users of ChatPool
  • Choose Create user
  • Username: frank
  • Choose Generate password
  • Choose Create user
  1. Do the same with brian
  • Username: frank
  • Choose Generate password
  • Choose Create user
  1. Test again and see the results
  2. At API Gateway interface
  • Choose Deploy API
  • Stages: prod
  • Choose Deploy
  1. Copy link of prod stage
  2. Check with browser