Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lambda - user is not authorized to perform: cognito-idp:ListUsers

I have encountered below error when I am trying to get all users in my user pool during testing in Lambda.

"errorType": "AccessDeniedException",
"errorMessage": "User: arn:aws:iam::123456789:user/xxxxx is not authorized to perform: cognito-idp:ListUsers on resource: arn:aws:cognito-idp:us-west-2:123456789:userpool/us-west-2_abcdefg",

My code in lambda:

var AWS = require('aws-sdk');

exports.handler = () => {
var params = {
  UserPoolId: 'us-west-2_abcdefg',
}

return new Promise((resolve, reject) => {
    AWS.config.update({ region: 'us-west-2', 'accessKeyId': 'accesskey', 'secretAccessKey': 'secretkey' });
    var cognitoidentityserviceprovider = new AWS.CognitoIdentityServiceProvider();
    cognitoidentityserviceprovider.listUsers(params, (err, data) => {
        if (err) {
            console.log(err);
            reject(err)
        }
        else {
            console.log("data", data);
            resolve(data)
        }
    })
});
};

I tried to add inline policy in IAM but still same error: enter image description here

Lambda IAM Role enter image description here

I knew I should update json for the policy, but Can someone provide detailed step to update the json policy?

like image 752
CCCC Avatar asked Dec 11 '22 00:12

CCCC


1 Answers

Your error cognito-idp:ListUsers is about Conginto User Pools, not Cognito User Identities. So your policy should be:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "cognito-idp:ListUsers",
            "Resource": "*"
        }
    ]
}
like image 95
Marcin Avatar answered Mar 02 '23 23:03

Marcin