'List Users in a Group Cognito - Amplify - Lambda - API REST

I am struggle with AWS amplify to get the users list of a specific cognito group. I always get this issue : localhost/:1 Access to XMLHttpRequest at 'https://ixa37ulou3.execute-api.eu-central-1.amazonaws.com/dev/users?groupName=xxx' from origin 'http://localhost:3000' has been blocked by CORS policy: Response to preflight request doesn't pass access control check: It does not have HTTP ok status. I tried a lot of things, see my last try below :

First Step: creatation of my Lambda function using "amplify add function":

const AWS = require('aws-sdk');
const cognito = new AWS.CognitoIdentityServiceProvider({region:"eu-central-1"});

exports.handler = async (event) => {

    const params = {
        GroupName: event.groupName, 
        UserPoolId: 'eu-central-1_xqIZx0wkT', 
    };

    const cognitoResponse = await cognito.listUsersInGroup(params).promise()

    const response = {
        statusCode:200,
        headers:{
            "Access-Control-Allow-Origin":"*",
            "Access-Control-Allow-Headers":"Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token,X-Amz-User-Agent",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Credentials": true
        },
        body: JSON.stringify(cognitoResponse),
    };
    return response;
};

Second Step: Creation of my REST Api using "amplify add api" (pathname: /users)

Third Step: create in API Gateway a new authorizer "using cognito type and link to my user pool, and for the token source : Authorization. And for /users - ANY - Method Request => i added my authorizer in Authorization field.

Fourth Step: creation of my Calling API Function:

async function callApi(){

      const user = await Auth.currentAuthenticatedUser()
      const token = user.signInUserSession.idToken.jwtToken
      console.log({token})

      const requestInfo = {
        headers:{
          Authorization: token
        },
        queryStringParameters:{
          groupName:"MyuserGroup"
        }
      }

      const data = await API.get('apilistusers','/users',requestInfo)
      console.log({data})
    }

I would very much appreciate any help on this topic, thank you very much.



Solution 1:[1]

the current version of amplify has an option to create a lambda function for administrating cognito users, see "amplify add auth" and this page https://docs.amplify.aws/cli/auth/admin/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Martijn Scheffer