是否有定义了多个 CacheBehaviors 的示例 cloudformation 模板?

Posted

技术标签:

【中文标题】是否有定义了多个 CacheBehaviors 的示例 cloudformation 模板?【英文标题】:Is there an example cloudformation template with multiple CacheBehaviors defined? 【发布时间】:2020-05-02 14:03:13 【问题描述】:

如何在模板中使用它我没有找到示例: * https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-distributionconfig.html#cfn-cloudfront-distribution-distributionconfig-cachebehaviors * https://docs.amazonaws.cn/en_us/AWSCloudFormation/latest/UserGuide/aws-properties-cloudfront-distribution-cachebehavior.html

我定义了一个 DefaultCacheBehavior,现在我想添加其他行为。

我试过了,但我得到一个错误:

  .....
  CFDistribution:
    Type: AWS::CloudFront::Distribution
    Properties:

      DistributionConfig:
        Enabled: 'true'
        Comment: 'sdlkfjsdlkfjsldkfj'
        PriceClass: 'PriceClass_All'
        IPV6Enabled: true
        DefaultRootObject: index.html
        Origins:
          -
            Id: Origin01 # arbitrary identifier
            S3OriginConfig: 
            DomainName: 'mydomain.blhblhb.blhb.com'

        ViewerCertificate:
           AcmCertificateArn: 'my-cer-id'
           SslSupportMethod: sni-only
        DefaultCacheBehavior:
          TargetOriginId: Origin01
          ViewerProtocolPolicy: redirect-to-https
          Compress: true
          MinTTL: 0
          DefaultTTL: 3600
          MaxTTL: 31536000
          AllowedMethods:
            - GET
            - HEAD
          CachedMethods:
            - GET
            - HEAD
          ForwardedValues:
            QueryString: 'false'
            Headers:
              - Origin
              - CloudFront-Viewer-Country
            Cookies:
              Forward: none

        # Tried adding this but no workie
        CacheBehaviors:
          CacheBehavior:
            PathPattern: '/somepath/*'
            TargetOriginId: Origin01
            ViewerProtocolPolicy: redirect-to-https
            Compress: true
            MinTTL: 0
            DefaultTTL: 3600
            MaxTTL: 31536000
            AllowedMethods:
              - GET
              - HEAD
            CachedMethods:
              - GET
              - HEAD
            ForwardedValues:
              QueryString: 'false'
              Headers:
                - Origin
                - CloudFront-Viewer-Country
              Cookies:
                Forward: none
            LambdaFunctionAssociations:
              -
                EventType: origin-request
                LambdaFunctionARN: !Ref MyLambda.Version

我得到错误:

Property validation failure: [Value of property /DistributionConfig/CacheBehaviors does not match type Array]

【问题讨论】:

【参考方案1】:

有多个 CacheBehaviors 的示例here。

我认为您的问题是在 YAML 中制定一组对象。请参阅以下问题:

YAML equivalent of array of objects in JSON

【讨论】:

【参考方案2】:

好吧,我的谷歌搜索不够努力:https://github.com/aws-samples/aws-refarch-wordpress/blob/master/templates/aws-refarch-wordpress-04-cloudfront.yaml

        ---
        AWSTemplateFormatVersion: 2010-09-09

        Description: Reference Architecture to host WordPress on AWS - Creates CloudFront distribution (if selected)

        Metadata:

          Authors:
            Description: Darryl Osborne (darrylo@amazon.com)
          License:
            Description: 'Copyright 2018 Amazon.com, Inc. and its affiliates. All Rights Reserved.
                          SPDX-License-Identifier: MIT-0'

          AWS::CloudFormation::Interface:
            ParameterGroups:
            - Label:
                default: AWS Parameters
              Parameters:
                - CloudFrontAcmCertificate
                - WPDomainName
                - PublicAlbDnsName
            ParameterLabels:
              CloudFrontAcmCertificate:
                default: CloudFront Certificate ARN
              PublicAlbDnsName:
                default: Public ALB DNS Name
              WPDomainName:
                default: Domain name of the WordPress site

        Parameters:

          CloudFrontAcmCertificate:
            AllowedPattern: ^$|(arn:aws:acm:)([a-z0-9/:-])*([a-z0-9])$
            Description: '[ Optional ] The AWS Certification Manager certificate ARN for the CloudFront distribution certificate - this certificate should be created in the us-east-1 (N. Virginia) region and must reference the WordPress domain name you use below.'
            Type: String
          PublicAlbDnsName:
            Description: The public application load balancer dns name.
            Type: String
          WPDomainName:
            AllowedPattern: ^$|(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\-]*[a-zA-Z0-9])\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\-]*[A-Za-z0-9])$
            Description: '[ Optional ] The main domain name of the WordPress site (e.g. example.com).'
            Type: String

        Conditions:

          SslCertificate:
            !Not [ !Equals [ '', !Ref CloudFrontAcmCertificate ] ]
          NoSslCertificate:
            !Equals [ '', !Ref CloudFrontAcmCertificate ]
          WPDomainName:
            !Not [ !Equals [ '', !Ref WPDomainName ] ]  
          NoWPDomainName:
            !Equals [ '', !Ref WPDomainName ]

        Resources:

          CloudFrontDistributionNoSslCertificate:
            Type: AWS::CloudFront::Distribution
            Condition: NoSslCertificate
            Properties:
              DistributionConfig:
                Aliases:
                - !If [ WPDomainName, !Join [ '', [ '*.', !Ref WPDomainName ] ], !Ref 'AWS::NoValue' ]
                CacheBehaviors:
                - PathPattern: wp-includes/*
                  AllowedMethods:
                  - DELETE
                  - GET
                  - HEAD
                  - OPTIONS
                  - PATCH
                  - POST
                  - PUT
                  DefaultTTL: 900
                  MaxTTL: 900
                  MinTTL: 900
                  ForwardedValues:
                    QueryString: true
                    Headers:
                    - Host
                  TargetOriginId: elb
                  ViewerProtocolPolicy: allow-all
                  Compress: true
                - PathPattern: wp-content/*
                  AllowedMethods:
                  - DELETE
                  - GET
                  - HEAD
                  - OPTIONS
                  - PATCH
                  - POST
                  - PUT
                  DefaultTTL: 900
                  MaxTTL: 900
                  MinTTL: 900
                  ForwardedValues:
                    QueryString: true
                    Headers:
                    - Host
                  TargetOriginId: elb
                  ViewerProtocolPolicy: allow-all
                  Compress: true
                Comment: !Ref 'AWS::StackName'
                DefaultCacheBehavior:
                  AllowedMethods:
                  - DELETE
                  - GET
                  - HEAD
                  - OPTIONS
                  - PATCH
                  - POST
                  - PUT
                  DefaultTTL: 0
                  MaxTTL: 0
                  MinTTL: 0
                  ForwardedValues:
                    QueryString: true
                    Headers:
                    - '*'
                    Cookies:
                      Forward: all
                  TargetOriginId: elb
                  ViewerProtocolPolicy: allow-all
                  Compress: true
                Enabled: true
                Origins:
                - DomainName: !Ref PublicAlbDnsName
                  Id: elb
                  CustomOriginConfig:
                    OriginProtocolPolicy: http-only
                PriceClass: PriceClass_100
          CloudFrontDistributionSslCertificate:
            Type: AWS::CloudFront::Distribution
            Condition: SslCertificate
            Properties:
              DistributionConfig:
                Aliases:
                - !If [ WPDomainName, !Join [ '', [ '*.', !Ref WPDomainName ] ], !Ref 'AWS::NoValue' ]



    ########################################################################
    ############# this was throwing me. docs make it seems like CacheBehavior: is a sub node under CacheBehaviors`enter code here`
                CacheBehaviors:
                - PathPattern: wp-includes/*
                  AllowedMethods:
                  - DELETE
                  - GET
                  - HEAD
                  - OPTIONS
                  - PATCH
                  - POST
                  - PUT
                  DefaultTTL: 900
                  MaxTTL: 900
                  MinTTL: 900
                  ForwardedValues:
                    QueryString: true
                    Headers:
                    - Host
                  TargetOriginId: elb
                  ViewerProtocolPolicy: redirect-to-https
                  Compress: true
                - PathPattern: wp-content/*
                  AllowedMethods:
                  - DELETE
                  - GET
                  - HEAD
                  - OPTIONS
                  - PATCH
                  - POST
                  - PUT
                  DefaultTTL: 900
                  MaxTTL: 900
                  MinTTL: 900
                  ForwardedValues:
                    QueryString: true
                    Headers:
                    - Host
                  TargetOriginId: elb
                  ViewerProtocolPolicy: redirect-to-https
                  Compress: true
                Comment: !Ref 'AWS::StackName'
########################################################################       

                DefaultCacheBehavior:
                  AllowedMethods:
                  - DELETE
                  - GET
                  - HEAD
                  - OPTIONS
                  - PATCH
                  - POST
                  - PUT
                  DefaultTTL: 0
                  MaxTTL: 0
                  MinTTL: 0
                  ForwardedValues:
                    QueryString: true
                    Headers:
                    - '*'
                    Cookies:
                      Forward: all
                  TargetOriginId: elb
                  ViewerProtocolPolicy: redirect-to-https
                  Compress: true
                Enabled: true
                Origins:
                - DomainName: !Ref PublicAlbDnsName
                  Id: elb
                  CustomOriginConfig:
                    OriginProtocolPolicy: https-only
                PriceClass: PriceClass_100
                ViewerCertificate:
                  AcmCertificateArn: !Ref CloudFrontAcmCertificate
                  SslSupportMethod: sni-only
                  MinimumProtocolVersion: TLSv1

        Outputs:

          DnsEndpoint:
            Value: !If [ NoSslCertificate, !GetAtt CloudFrontDistributionNoSslCertificate.DomainName, !GetAtt CloudFrontDistributionSslCertificate.DomainName ]
          DnsHostname:
            Value: !If [ NoSslCertificate, !Join [ '', [ 'http://', !GetAtt CloudFrontDistributionNoSslCertificate.DomainName ] ], !Join [ '', [ 'https://', !GetAtt CloudFrontDistributionSslCertificate.DomainName ] ] ]

【讨论】:

以上是关于是否有定义了多个 CacheBehaviors 的示例 cloudformation 模板?的主要内容,如果未能解决你的问题,请参考以下文章

是否有条件格式化自定义公式,在该公式中,我可以检查两个或多个单元格(来自该范围)是否总和成一个值?

是否有一种简洁的方法可以为同一个扩展定义两个或多个 bash 别名?

是否有可能以及如何使用 git-flow 管理多个版本变体(即自定义应用程序)?

fxcop 自定义规则 - 避免每个文件有多个类

在 Matlab 中的单个文件中定义多个枚举

是否可以在 EDMX 文件 C# DB 优先方法中更新多个过程和函数定义