【问题标题】:Permission denied for IAM policy to S3 bucketIAM 策略对 S3 存储桶的权限被拒绝
【发布时间】:2019-11-09 13:12:38
【问题描述】:

我正在尝试使用 Resize Images on the Fly with Amazon S3, AWS Lambda, and Amazon API Gateway | AWS Compute Blog 设置调整大小的图像 lambda。

但是,IAM 政策不起作用。它无权访问 S3 存储桶。

我在 IAM Policy Simulator(测试 S3 PutObject)中对其进行了测试,显示为 Implicitly denied (not matching statements)。

我根据Grant a Lambda Execution Role Access to an Amazon S3 Bucket 编辑了策略,但它给了我同样的错误。

这是我的存储桶策略(经过编辑以将其更改为下面的角色而不是 root,仍然通过 IAM 策略刺激器拒绝):

{
    "Version": "2008-10-17",
    "Statement": [
        {
            "Sid": "AllowPublicRead",
            "Effect": "Allow",
            "Principal": {
                "AWS": "*"
            },
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::mybucketname/*"
        },
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<my-account-number>::role/<my-role-name>"
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::mybucketname",
                "arn:aws:s3:::mybucketname/*"
            ]
        }
    ]
}

这是我的 IAM 角色政策:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": [
                "logs:CreateLogStream",
                "logs:PutLogEvents"
            ],
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Sid": "VisualEditor1",
            "Effect": "Allow",
            "Action": [
                "s3:ListAllMyBuckets",
                "s3:GetBucketLocation"
            ],
            "Resource": "*"
        },
        {
            "Sid": "VisualEditor2",
            "Effect": "Allow",
            "Action": "logs:CreateLogGroup",
            "Resource": "arn:aws:logs:*:*:*"
        },
        {
            "Sid": "VisualEditor3",
            "Effect": "Allow",
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::mybucketname/*",
                "arn:aws:s3:::mybucketname"
            ]
        }
    ]
}

这是我的 S3 重定向规则:

<RoutingRules>
  <RoutingRule>
    <Condition>
      <KeyPrefixEquals/>
      <HttpErrorCodeReturnedEquals>404</HttpErrorCodeReturnedEquals>
    </Condition>
    <Redirect>
      <Protocol>https</Protocol>
      <HostName>MYAPIENDPOINT.eu-west-1.amazonaws.com</HostName>
      <ReplaceKeyPrefixWith>default/resize?key=</ReplaceKeyPrefixWith>
      <HttpRedirectCode>307</HttpRedirectCode>
    </Redirect>
  </RoutingRule>
</RoutingRules>

【问题讨论】:

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


    【解决方案1】:

    您确定您的 Lambda 函数以 root 用户身份运行吗? (我什至不知道这是否可能,但你可能不想这样做)

    您可以使用以下命令找出您的 Lambda 函数的角色:

    aws lambda get-function-configuration --function-name YOUR_FUNCTION_NAME | grep Role
    

    您看到的值是您应该在存储桶策略中使用的值:

        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<my-account-number>:role/service-role/foo-bar-baz"
            },
            "Action": "s3:*",
            "Resource": [
                "arn:aws:s3:::mybucketname",
                "arn:aws:s3:::mybucketname/*"
            ]
        }
    

    【讨论】:

    • 我在根之前有一个策略,但它在 IAM 策略刺激器中也不起作用...我试图刺激该策略以验证它可以在 S3 上“PutObject”,但我得到了错误被拒绝。编辑以在 s3 json 中显示角色 - 主要问题是角色本身在策略刺激器中测试时无权访问 s3 存储桶,我认为这就是整个设置不起作用的原因?
    • 您编辑的存储桶策略在角色 ARN 中有两个 : 字符,而应该只有一个。您的存储桶策略是否也有这个错字?
    • 如果我在 iam 之后删除双精度 :,则 aws 表示它无效。您的示例也有两个 : 字符?还是你的意思是别的地方?
    猜你喜欢
    • 2017-12-04
    • 2023-03-29
    • 2018-01-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-20
    • 2019-02-26
    • 2017-02-14
    • 2018-10-20
    相关资源
    最近更新 更多