【问题标题】:lambda - user is not authorized to perform: cognito-idp:ListUserslambda - 用户无权执行:cognito-idp:ListUsers
【发布时间】:2021-02-08 11:46:08
【问题描述】:

当我在 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 中添加内联策略,但仍然出现同样的错误:

Lambda IAM 角色

我知道我应该为该策略更新 json,但是有人可以提供更新 json 策略的详细步骤吗?

【问题讨论】:

    标签: amazon-web-services aws-lambda amazon-iam


    【解决方案1】:

    您的错误 cognito-idp:ListUsers 是关于 Conginto 用户池,而不是 Cognito 用户身份。所以你的政策应该是:

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

    【讨论】:

    • 对于遇到同样错误的其他人,请注意您应该为 USER 添加策略而不是角色,因为问题是由于用户权限而不是您的 lambda 函数的角色
    【解决方案2】:

    这只是权限问题。请按照以下步骤操作:

    我。创建策略(以获得许可)

    1. 转到 IAM 控制台 -> 策略 -> 创建策略。
    2. 选择“Cognito 用户池”服务。
    3. 指定您需要权限的所需操作(列表、读取等)
    4. 指定资源。
    5. 选择请求条件(可选)。
    6. 添加标签(可选)。
    7. 提供政策的名称和说明。
    8. 点击“创建策略”按钮。

    已创建策略。

    二。向用户添加策略:

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

    问题已解决。

    【讨论】:

      【解决方案3】:

      解决方案对我有用:

      步骤: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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-06-29
        • 1970-01-01
        • 2021-05-20
        • 2022-11-10
        相关资源
        最近更新 更多