Amazon Forecast 的 create_dataset_import_job S3 角色需要对 S3 资源进行星级访问

Amazon Forecast's create_dataset_import_job S3 Role Requires Star-Access to S3-Resources

我编写了一个创建数据集导入作业的 Lambda 函数 (link to API). The Datasource property of that request requires an S3 Config item, which in turn, contains an IAM Role "that Amazon Forecast can assume to access the Amazon S3 bucket or files".

为了遵循最少访问原则,我想为该角色(下面的 Cloud Formation 定义)提供尽可能少的权限。我能够将其操作限制为仅列出和获取;但是,除非我授予它访问 Resource: * 的权限,否则它不会工作。我更愿意让它访问 Resource: arn:aws:s3:::my-bucket/*(或者更好的是 Resource: arn:aws:s3:::my-bucket/path/to/my_file.csv。我得到的错误消息(当 not 使用 Resource: * 时)是

An error occurred (404) when calling the HeadObject operation: Not Found

An error occurred (403) when calling the HeadObject operation: Forbidden

取决于我是 运行 本地(通过 SAM CLI)还是在 LAMBDA 控制台中。

¿有没有人遇到过在没有 只写 访问 ALL 的情况下创建数据集导入作业时 Forecast 会出错的原因s3,而不是只有一个桶,或者更好的一个文件?

  CreateDatasetImportJobS3Role:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          -
            Effect: Allow
            Principal:
              Service:
                - forecast.amazonaws.com
            Action: sts:AssumeRole
      Policies:
        -
          PolicyName: ReadFromBucketPolicy
          PolicyDocument:
            Version: 2012-10-17
            Statement:
              -
                Effect: Allow
                Action:
                  - s3:ListBucket
                  - s3:GetObject
                # I'd like to be able to do this without a STAR resource;
                # however, it doesn't seem to work without
                Resource: "*"

s3://my-bucket/* 格式不正确。正确的格式必须是有效的 ARN,例如 arn:aws:s3:::my-bucket/*。此外,当您有像 ListBucket 这样的 API 操作时,您需要列出存储桶 ARN 而不是 该存储桶中的标签。所以你真的应该将资源指定为 arn:aws:s3:::my-bucket.

为了安全起见,我通常会把两者都放在一起,制定你的最终政策:

Resource: 
   - "arn:aws:s3:::my-bucket"
   - "arn:aws:s3:::my-bucket/*"

第一个资源 arn:aws:s3:::my-bucket 涵盖了 ListBucket 等操作。第二个资源 arn:aws:s3:::my-bucket/* 涵盖诸如 GetObject 之类的操作(因为对象本身将位于通配符所涵盖的路径下)。