Lambda函数报错:用户未被授权执行操作:cognito-idp:ListUsers

9

我在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",

我的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)
        }
    })
});
};

我尝试在IAM中添加内联策略,但仍然出现相同的错误: enter image description here

Lambda IAM角色 enter image description here

我知道我应该更新策略的json文件,但有人能提供详细的步骤来更新json策略吗?

3个回答

16

您的错误 cognito-idp:ListUsersConginto用户池有关,而不是Cognito用户身份。因此,您的策略应该是:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "cognito-idp:ListUsers",
            "Resource": "*"
        }
    ]
}

3
对于遇到相同错误的其他人,请注意应该为用户添加策略而不是角色,因为问题是由于用户权限而不是 Lambda 函数的角色导致的。 - CCCC

10

这只是一个权限问题。按照以下步骤执行:

I. 创建权限策略

  1. 进入 IAM 控制台 -> 策略 -> 创建策略。
  2. 选择 "Cognito 用户池" 服务。
  3. 指定所需的操作,需要获得哪些权限(列出、读取等)。
  4. 指定资源。
  5. 选择请求条件(可选)。
  6. 添加标签(可选)。
  7. 给策略命名并描述。
  8. 点击 "创建策略" 按钮。

权限策略已创建。

II. 将策略添加到用户:

  1. 进入 IAM 控制台 -> 用户。
  2. 选择所需的用户。
  3. 在权限选项卡中,单击添加权限。
  4. 单击 "直接附加现有策略"。
  5. 搜索刚刚创建的策略。
  6. 单击 "添加权限"。

问题已解决。


0

解决方案对我有用:

步骤:1 我从IAM控制台创建了一个新的策略,使用以下JSON:

{
"Version": "2012-10-17",
"Statement": [
    {
        "Sid": "VisualEditor0",
        "Effect": "Allow",
        "Action": [
            "cognito-identity:MergeDeveloperIdentities",
            "cognito-identity:DescribeIdentityPool",
            "cognito-identity:ListIdentityPools",
            "cognito-identity:CreateIdentityPool",
            "cognito-identity:ListIdentities",
            "cognito-identity:GetOpenIdTokenForDeveloperIdentity",
            "cognito-identity:GetOpenIdToken",
            "cognito-identity:GetIdentityPoolRoles",
            "cognito-identity:GetPrincipalTagAttributeMap",
            "cognito-identity:GetId",
            "cognito-identity:LookupDeveloperIdentity",
            "cognito-identity:UnlinkDeveloperIdentity",
            "cognito-identity:ListTagsForResource",
            "cognito-identity:UpdateIdentityPool",
            "cognito-identity:UnlinkIdentity",
            "cognito-identity:DescribeIdentity",
            "cognito-identity:GetCredentialsForIdentity"
        ],
        "Resource": "*"
    }
]

}

步骤:2已将策略添加到ecsInstanceRole


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接