【发布时间】:2021-02-26 06:27:07
【问题描述】:
对一个存储桶使用多个 CloudFront 效果不佳,因为我可以在 CloudFormation 中授予访问权限的唯一方法是在模板中添加 BucketPolicy。对于多个应用程序/模板,这将始终覆盖整个存储桶策略以添加S3 Origin Access Identity 的权限。
S3OriginIdentity:
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: S3 Origin Identity
S3OriginIdentityS3ReadPolicy:
Type: "AWS::S3::BucketPolicy"
Properties:
Bucket: my-bucket
PolicyDocument:
Version: '2012-10-17'
Statement:
- Sid: my-cloudfront-read-access
Action:
- s3:GetObject
Effect: Allow
Principal:
CanonicalUser:
Fn::GetAtt: S3OriginIdentity.S3CanonicalUserId
Resource: arn:aws:s3:::my-bucket/path/*
为了解决这个问题,我想到可以使用存储桶策略中的条件来授予对任何 Cloudfront 身份的读取访问权限。从the documentation 看来,aws:SourceArn 或 aws:PrincipalArn 应该可以解决问题。这是我尝试的示例策略。
{
"Version": "2012-10-17",
"Statement": [{
"Sid": 1,
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Resource": "arn:aws:s3:::my-bucket/*",
"Condition": {
"ArnLike": {
"aws:SourceArn": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity*"
}
},
"Principal": "*"
}]
}
当我使用aws:PrincipalArn 时,控制台甚至不允许我保存它(通用的Access Denied),但我尝试使用上面的aws:SourceArn 版本,它没有提供访问权限。我究竟做错了什么?还是有更好的方法?
【问题讨论】:
标签: amazon-web-services amazon-s3 amazon-cloudformation amazon-cloudfront