如何在AWS SAM模板中授予S3存储桶的公开只读访问权限

huangapple go评论117阅读模式
英文:

How to grant public read only access to S3 Bucket in AWS SAM template

问题

  1. 我正在创建一个SAM模板中的S3存储桶,并希望为其提供公共读取权限。到目前为止,我想到了以下内容:
  2. ProductBucket:
  3. Type: AWS::S3::Bucket
  4. Properties:
  5. BucketName: !Sub ${ProductBucketName}${Stage}
  6. ProductBucketPolicy:
  7. Type: AWS::S3::BucketPolicy
  8. Properties:
  9. Bucket: !Ref ProductBucket
  10. PolicyDocument:
  11. Id: PublicReadPolicy
  12. Version: 2012-10-17
  13. Statement:
  14. - Sid: PublicReadForGetBucketObjects
  15. Effect: Allow
  16. Principal: '*'
  17. Action: 's3:GetObject'
  18. Resource: !Join
  19. - ''
  20. - - 'arn:aws:s3:::'
  21. - !Ref ProductBucket
  22. 这导致了一个格式不正确的异常。
英文:

I am creating an S3 bucket in a SAM template and would like to give it public read access. This is what I came up with so far:

  1. ProductBucket:
  2. Type: AWS::S3::Bucket
  3. Properties:
  4. BucketName: !Sub ${ProductBucketName}${Stage}
  5. ProductBucketPolicy:
  6. Type: AWS::S3::BucketPolicy
  7. Properties:
  8. Bucket: !Ref ProductBucket
  9. PolicyDocument:
  10. Id: PublicReadPolicy
  11. Version: 2012-10-17
  12. Statement:
  13. - Sid: PublicReadForGetBucketObjects
  14. Effect: Allow
  15. Principal: '*'
  16. Action: 's3:GetObject'
  17. Resource: !Join
  18. - ''
  19. - - 'arn:aws:s3:::'
  20. - !Ref ProductBucket

This fails with a malformed exception.

答案1

得分: 3

Your template is fine except the line that says - - 'arn:aws:s3:::' where its malformed. Just a suggestion that you can use Fn::Sub over Fn::Join when delimiter is an empty string.

AWSTemplateFormatVersion: "2010-09-09"

Parameters:
ProductBucketName:
Type: String

Resources:
ProductBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Sub ${ProductBucketName}-${AWS::StackName}

ProductBucketPolicy:
Type: AWS::S3::BucketPolicy
Properties:
Bucket: !Ref ProductBucket
PolicyDocument:
Id: PublicReadPolicy
Version: 2012-10-17
Statement:
- Sid: PublicReadForGetBucketObjects
Effect: Allow
Principal: ''
Action: 's3:GetObject'
Resource: !Sub arn:aws:s3:::${ProductBucket}/

Hope this helps you move forward.

英文:

Your template is fine except the line that says - - 'arn:aws:s3:::' where its malformed. Just a suggestion that you can use Fn::Sub over Fn::Join when delimiter is an empty string.

  1. AWSTemplateFormatVersion: "2010-09-09"
  2. Parameters:
  3. ProductBucketName:
  4. Type: String
  5. Resources:
  6. ProductBucket:
  7. Type: AWS::S3::Bucket
  8. Properties:
  9. BucketName: !Sub ${ProductBucketName}-${AWS::StackName}
  10. ProductBucketPolicy:
  11. Type: AWS::S3::BucketPolicy
  12. Properties:
  13. Bucket: !Ref ProductBucket
  14. PolicyDocument:
  15. Id: PublicReadPolicy
  16. Version: 2012-10-17
  17. Statement:
  18. - Sid: PublicReadForGetBucketObjects
  19. Effect: Allow
  20. Principal: '*'
  21. Action: 's3:GetObject'
  22. Resource: !Sub arn:aws:s3:::${ProductBucket}/*

Hope this help you move forward.

答案2

得分: 1

我发现AWS文档的YAML示例看起来有点奇怪,但我按照它的示例为我的CF堆栈授予了IAM s3:PutBucketPolicy权限,它运行正常。以下是我的SAM。

  1. ExampleBucket:
  2. Type: AWS::S3::Bucket
  3. Properties:
  4. BucketName: !Sub <bucket-name>
  5. ExampleBucketPolicy:
  6. Type: AWS::S3::BucketPolicy
  7. Properties:
  8. Bucket:
  9. Ref: "ExampleBucket"
  10. PolicyDocument:
  11. Statement:
  12. - Action:
  13. - "s3:GetObject"
  14. Effect: "Allow"
  15. Resource:
  16. Fn::Join:
  17. - ""
  18. - - "arn:aws:s3:::"
  19. - Ref: "ExampleBucket"
  20. - "/*"
  21. Principal: "*"

这是您可能已经查阅的AWS文档页面链接:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-policy.html.

英文:

I found the AWS documentation's yaml example to look a bit funny, but I followed it and gave my CF stack IAM s3:PutBucketPolicy permissions, and it worked. Below is my SAM.

  1. ExampleBucket:
  2. Type: AWS::S3::Bucket
  3. Properties:
  4. BucketName: !Sub &lt;bucket-name&gt;
  5. ExampleBucketPolicy:
  6. Type: AWS::S3::BucketPolicy
  7. Properties:
  8. Bucket:
  9. Ref: &quot;ExampleBucket&quot;
  10. PolicyDocument:
  11. Statement:
  12. - Action:
  13. - &quot;s3:GetObject&quot;
  14. Effect: &quot;Allow&quot;
  15. Resource:
  16. Fn::Join:
  17. - &quot;&quot;
  18. - - &quot;arn:aws:s3:::&quot;
  19. - Ref: &quot;ExampleBucket&quot;
  20. - &quot;/*&quot;
  21. Principal: &quot;*&quot;

Here is the AWS documentation page you've probably already consulted: https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-policy.html.

huangapple
  • 本文由 发表于 2020年1月3日 23:18:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581050.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定