【问题标题】:AWS Lambda - (AccessDeniedException) when calling the Scan operation User is not authorized to perform: dynamodb: ScanAWS Lambda - (AccessDeniedException) 调用扫描操作时用户无权执行:dynamodb: Scan
【发布时间】:2021-12-09 08:52:46
【问题描述】:

我尝试通过 AWS 中的 boto3 (Python) 访问 dynamodb。让它在我的本地机器上工作。据我了解,在 AWS 运行中,它只使用 IAM 角色来获取访问权限。但它不起作用。

  Lambda execution failed with status 200 due to customer function error: An error occurred (AccessDeniedException) when calling the Scan operation: 

  User: arn:aws:sts::021517822274:assumed-role/CodeStar-tt-api-subjects-Execution/

  awscodestar-tt-api-subjects-lambda-HelloWorld is not authorized to perform: dynamodb:

  Scan on resource: arn:aws:dynamodb:us-east-1:021517822274:table/tt-subjects. 

这里发送了完全相同的问题:

How to solve (AccessDeniedException) when calling the Scan operation: User: arn:aws:sts... is not authorized to perform: dynamodb:Scan on resource.."?

我应用了建议的 AmazonDynamoDBFullAccess 政策。也尝试过:

https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_dynamodb_specific-table.html

https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_examples_lambda-access-dynamodb.html

我自己添加的策略(另外)是:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "ListAndDescribe",
            "Effect": "Allow",
            "Action": [
                "dynamodb:List*",
                "dynamodb:DescribeReservedCapacity*",
                "dynamodb:DescribeLimits",
                "dynamodb:DescribeTimeToLive"
            ],
            "Resource": "*"
        },
        {
            "Sid": "SpecificTable",
            "Effect": "Allow",
            "Action": [
                "dynamodb:BatchGet*",
                "dynamodb:DescribeStream",
                "dynamodb:DescribeTable",
                "dynamodb:Get*",
                "dynamodb:Query",
                "dynamodb:Scan"
            ],
            "Resource": "arn:aws:dynamodb:*:*:table/tt-subjects"
        }
    ]
}

但我仍然遇到同样的错误。

应用这些政策是否需要很长时间,或者仍然是什么原因造成的?

【问题讨论】:

  • 从外观上看,您附加到 lambda 的角色似乎没有所需的权限。考虑到错误,如果您还发布了附加到 lambda 的角色的政策,那就太好了
  • 如前所述,我还添加了此策略:AmazonDynamoDBFullAccess 和我自己的。我尝试在上面添加它。
  • 明白,但这是一个模板策略……它指的是示例表,您需要将其替换为您的表名。需要查看实际使用的策略。
  • 我是否正确假设上述策略适用于角色 awscodestar-tt-api-subjects-lambda-HelloWorld
  • Boundaries 描述了您可以达到的限制 - 如果您尝试访问的内容在外部或未定义为该边界的一部分,则您不能去那里 - 所以如果 Lambda 执行角色有一个不包括发电机的束缚,它将无法访问它。至于您的格式错误,响应 - 代理 lambda 必须返回带有 { statusCode:200, body:payload} 的类似 json 的对象

标签: aws-lambda amazon-dynamodb boto3


【解决方案1】:

现在我找到了答案。当我使用 codestar 创建我的 lambda 时,它还创建了一个 permission boundary

如何解决这个问题:

  • 移除边界(不推荐)
  • 延长边界,如下所示:

编辑 lambda 的边界:

  1. 为 Lambda 打开控制台
  2. 转到标签配置
  3. 执行角色中,打开指向您的角色的链接
  4. 现在您处于 IAM 角色编辑器中。向下滚动到权限边界
  5. 复制该名称(没有链接)
  6. 在 IAM 菜单中转到策略
  7. 搜索复制的名称
  8. 编辑(扩展)政策。

就我的 dynamodb 而言,我向下滚动到 sid 6(您可能会有所不同)。它是一个允许块,包含许多简单的条目和一个 * 作为资源。

所以我用 dynamodb 条目扩展了这个块。现在看起来像这样:

...
{
            "Sid": "6",
            "Effect": "Allow",
            "Action": [
                "apigateway:GET",
                "cloudtrail:CreateTrail",
                "cloudtrail:StartLogging",
                "ec2:Describe*",
                "lambda:ListFunctions",
                "logs:CreateLogGroup",
                "logs:CreateLogStream",
                "logs:DescribeLogGroups",
                "logs:PutLogEvents",
                "sns:Get*",
                "sns:List*",
                "sns:Publish",
                "sns:Subscribe",
                "xray:Put*",

                "dynamodb:BatchGet*",
                "dynamodb:DescribeStream",
                "dynamodb:DescribeTable",
                "dynamodb:Get*",
                "dynamodb:Query",
                "dynamodb:Scan",
                "dynamodb:BatchWrite*",
                "dynamodb:CreateTable",
                "dynamodb:Delete*",
                "dynamodb:Update*",
                "dynamodb:PutItem",

                "dynamodb:List*",
                "dynamodb:DescribeReservedCapacity*",
                "dynamodb:DescribeLimits",
                "dynamodb:DescribeTimeToLive"
            ],
            "Resource": [
                "*"
            ]
        },
...

非常感谢帮助我的贡献者!

【讨论】:

    猜你喜欢
    • 2019-08-25
    • 2016-10-15
    • 1970-01-01
    • 2018-08-22
    • 2016-09-26
    • 1970-01-01
    • 1970-01-01
    • 2020-07-23
    • 1970-01-01
    相关资源
    最近更新 更多