gpt4 book ai didi

amazon-web-services - 通过 CloudFormation 将多个存储桶附加到 CloudFront

转载 作者:行者123 更新时间:2023-12-03 07:14:20 25 4
gpt4 key购买 nike

我创建了一个 yaml 文档来创建 S3 存储桶并将其附加到发行版。



Resources:

S3BucketContent:
DeletionPolicy: 'Delete'
Metadata:
Comment: 'Bucket to store Content'
Properties:
AccessControl: 'Private'
BucketName: !Sub '${AWS::StackName}-content-bucket'
Type: 'AWS::S3::Bucket'

S3BucketPolicy:
Metadata:
Comment: 'Bucket policy to allow cloudfront to access the data'
Properties:
Bucket: !Ref S3BucketContent
PolicyDocument:
Statement:
- Action:
- 's3:GetObject'
Effect: 'Allow'
Principal:
CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
Resource:
- !Sub 'arn:aws:s3:::${S3BucketContent}/*'
Type: 'AWS::S3::BucketPolicy'

CfDistribution:
Metadata:
Comment: 'A simple CloudFront distribution with an S3 origin'
Properties:
DistributionConfig:
Comment: 'A simple distribution with an S3 origin'
DefaultCacheBehavior:
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
Compress: false
DefaultTTL: 86400
ForwardedValues:
Cookies:
Forward: 'none'
Headers:
- 'Origin'
QueryString: false
MaxTTL: 31536000
MinTTL: 86400
TargetOriginId: !Sub 's3-origin-${S3BucketContent}'
TrustedSigners:
- !Ref AWS::AccountId
ViewerProtocolPolicy: 'allow-all'
DefaultRootObject: 'index.html'
Enabled: true
HttpVersion: 'http1.1'
IPV6Enabled: false
Origins:
- DomainName: !GetAtt S3BucketContent.RegionalDomainName # NOTE: you may want to replace this with !GetAtt S3Bucket.DomainName (the RegionalDomainName is just to get around the initial DNS propagation issue), more details here: https://stackoverflow.com/questions/38735306/aws-cloudfront-redirecting-to-s3-bucket
Id: !Sub 's3-origin-${S3BucketContent}'
OriginPath: ''
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'
PriceClass: 'PriceClass_All'
Type: 'AWS::CloudFront::Distribution'

CfOriginAccessIdentity:
Metadata:
Comment: 'Access S3 bucket content only through CloudFront'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'Access S3 bucket content only through CloudFront'
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'

Outputs:
S3BucketName:
Description: 'Bucket name'
Value: !Ref S3BucketContent
CfDistributionId:
Description: 'Id for our cloudfront distribution'
Value: !Ref CfDistribution
CfDistributionDomainName:
Description: 'Domain name for our cloudfront distribution'
Value: !GetAtt CfDistribution.DomainName

POC 工作后,我想创建多个可以添加到同一 Cloudfront 发行版的存储桶,但我收到一条错误消息,提示 模板格式错误:模板的资源 block 中 Unresolved 资源依赖项 [S3BucketDWC] ,没有指定确切的错误。这是 2 个存储桶的更新后的 yaml。有人可以告诉我该怎么做吗?



Resources:

S3BucketContent:
DeletionPolicy: 'Delete'
Metadata:
Comment: 'Bucket to store Content'
Properties:
AccessControl: 'Private'
BucketName: !Sub '${AWS::StackName}-content-bucket'
Type: 'AWS::S3::Bucket'

S3BucketPolicy:
Metadata:
Comment: 'Bucket policy to allow cloudfront to access the data'
Properties:
Bucket: !Ref S3BucketContent
PolicyDocument:
Statement:
- Action:
- 's3:GetObject'
Effect: 'Allow'
Principal:
CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
Resource:
- !Sub 'arn:aws:s3:::${S3BucketContent}/*'
Type: 'AWS::S3::BucketPolicy'

S3BucketDWC:
DeletionPolicy: 'Delete'
Metadata:
Comment: 'Bucket to store dwc'
Properties:
AccessControl: 'Private'
BucketName: !Sub '${AWS::StackName}-dwc-bucket'
Type: 'AWS::S3::Bucket'

S3BucketPolicy:
Metadata:
Comment: 'Bucket policy to allow cloudfront to access the data'
Properties:
Bucket: !Ref S3BucketDWC
PolicyDocument:
Statement:
- Action:
- 's3:GetObject'
Effect: 'Allow'
Principal:
CanonicalUser: !GetAtt CfOriginAccessIdentity.S3CanonicalUserId
Resource:
- !Sub 'arn:aws:s3:::${S3BucketDWC}/*'
Type: 'AWS::S3::BucketPolicy'

CfDistribution:
Metadata:
Comment: 'A simple CloudFront distribution with an S3 origin'
Properties:
DistributionConfig:
Comment: 'A simple distribution with an S3 origin'
DefaultCacheBehavior:
AllowedMethods:
- 'HEAD'
- 'GET'
CachedMethods:
- 'HEAD'
- 'GET'
Compress: false
DefaultTTL: 86400
ForwardedValues:
Cookies:
Forward: 'none'
Headers:
- 'Origin'
QueryString: false
MaxTTL: 31536000
MinTTL: 86400
TargetOriginId: !Sub 's3-origin-${S3BucketContent}'
TrustedSigners:
- !Ref AWS::AccountId
ViewerProtocolPolicy: 'allow-all'
DefaultRootObject: 'index.html'
Enabled: true
HttpVersion: 'http1.1'
IPV6Enabled: false
Origins:
- DomainName: !GetAtt S3BucketContent.RegionalDomainName
Id: !Sub 's3-origin-${S3BucketContent}'
OriginPath: ''
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'

DomainName: !GetAtt S3BucketContent.RegionalDomainName
Id: !Sub 's3-origin-${S3BucketDWC}'
OriginPath: ''
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'

PriceClass: 'PriceClass_All'
Type: 'AWS::CloudFront::Distribution'

CfOriginAccessIdentity:
Metadata:
Comment: 'Access S3 bucket content only through CloudFront'
Properties:
CloudFrontOriginAccessIdentityConfig:
Comment: 'Access S3 bucket content only through CloudFront'
Type: 'AWS::CloudFront::CloudFrontOriginAccessIdentity'

Outputs:
S3BucketName:
Description: 'Bucket name'
Value: !Ref S3BucketContent
CfDistributionId:
Description: 'Id for our cloudfront distribution'
Value: !Ref CfDistribution
CfDistributionDomainName:
Description: 'Domain name for our cloudfront distribution'
Value: !GetAtt CfDistribution.DomainName

最佳答案

至少一个明显的问题是您使用了错误的缩进:

    S3BucketDWC:
DeletionPolicy: 'Delete'
Metadata:
Comment: 'Bucket to store dwc'
Properties:
AccessControl: 'Private'
BucketName: !Sub '${AWS::StackName}-dwc-bucket'
Type: 'AWS::S3::Bucket'

这应该是:

  S3BucketDWC:
DeletionPolicy: 'Delete'
Metadata:
Comment: 'Bucket to store dwc'
Properties:
AccessControl: 'Private'
BucketName: !Sub '${AWS::StackName}-dwc-bucket'
Type: 'AWS::S3::Bucket'

第二个问题是 Origins 应该是一个列表(您缺少 -):

        Origins:
- DomainName: !GetAtt S3BucketContent.RegionalDomainName
Id: !Sub 's3-origin-${S3BucketContent}'
OriginPath: ''
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'

- DomainName: !GetAtt S3BucketContent.RegionalDomainName
Id: !Sub 's3-origin-${S3BucketDWC}'
OriginPath: ''
S3OriginConfig:
OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CfOriginAccessIdentity}'

关于amazon-web-services - 通过 CloudFormation 将多个存储桶附加到 CloudFront,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63420298/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com