Resources:
  {{#with cloudfront.primary}}
  CloudFrontPrimary:
    Type: "AWS::CloudFront::Distribution"
    Properties:
      DistributionConfig:
        Aliases:
          - {{hostname}}
        Comment: Static site deployed by Haiku9
        DefaultCacheBehavior:
          AllowedMethods: ["GET", "HEAD", "OPTIONS"]
          CachedMethods: ["GET", "HEAD", "OPTIONS"]
          Compress: false
          MinTTL: 0
          MaxTTL: 31536000
          DefaultTTL: {{expires}}
          ForwardedValues:
            Cookies:
              Forward: "all"
            {{#if headers}}
            Headers:
              {{#each headers}}
              - "{{.}}"
              {{/each}}
            {{/if}}
            QueryString: true
            QueryStringCacheKeys: ["*"]
          SmoothStreaming: false
          TargetOriginId: HaikuS3Origin
          ViewerProtocolPolicy: redirect-to-https
          LambdaFunctionAssociations:
            {{#each lambdas}}
            - EventType: {{type}}
              LambdaFunctionARN: {{arn}}
            {{/each}}
        DefaultRootObject: ""
        Enabled: true
        HttpVersion: {{httpVersion}}
        IPV6Enabled: false
        Origins:
          - Id: HaikuS3Origin
            DomainName: {{bucketURL}}
            OriginPath: ""
            S3OriginConfig:
              OriginAccessIdentity: "origin-access-identity/cloudfront/{{originAccessID}}"
        PriceClass: PriceClass_{{priceClass}}
        ViewerCertificate:
          AcmCertificateArn: {{certificate}}
          SslSupportMethod: "sni-only"
          MinimumProtocolVersion: {{protocolVersion}}
  {{/with}}

  {{#each cloudfront.secondaries}}
  CloudFront{{@index}}:
    Type: "AWS::CloudFront::Distribution"
    Properties:
      DistributionConfig:
        Aliases:
          - {{hostname}}
        Comment: Static site deployed by Haiku9
        DefaultCacheBehavior:
          AllowedMethods: ["GET", "HEAD", "OPTIONS"]
          CachedMethods: ["GET", "HEAD", "OPTIONS"]
          Compress: false
          MinTTL: 31536000
          MaxTTL: 31536000
          DefaultTTL: 31536000
          ForwardedValues:
            Cookies:
              Forward: none
            Headers: []
            QueryString: false
          SmoothStreaming: false
          TargetOriginId: HaikuS3Origin
          ViewerProtocolPolicy: redirect-to-https
          LambdaFunctionAssociations:
            {{#each lambdas}}
            - EventType: {{type}}
              LambdaFunctionARN: {{arn}}
            {{/each}}
        DefaultRootObject: ""
        Enabled: true
        HttpVersion: {{httpVersion}}
        IPV6Enabled: false
        Origins:
          - Id: HaikuS3Origin
            DomainName: {{bucketURL}}
            OriginPath: ""
            S3OriginConfig:
              OriginAccessIdentity: ""
        PriceClass: PriceClass_{{priceClass}}
        ViewerCertificate:
          AcmCertificateArn: {{certificate}}
          SslSupportMethod: "sni-only"
          MinimumProtocolVersion: {{protocolVersion}}
  {{/each}}

  DNS:
    Type: AWS::Route53::RecordSetGroup
    DependsOn:
      - CloudFrontPrimary
      {{#each cloudfront.secondaries}}
      - CloudFront{{@index}}
      {{/each}}
    Properties:
      Comment: Haiku9 static site
      HostedZoneId: {{route53.hostedZoneID}}
      RecordSets:
        {{#with route53.primary}}
        - Name: {{name}}
          Type: A
          AliasTarget:
            DNSName: !GetAtt [ CloudFrontPrimary, DomainName ]
            EvaluateTargetHealth: false
            HostedZoneId: Z2FDTNDATAQYW2
        {{/with}}
        {{#each route53.secondaries}}
        - Name: {{name}}
          Type: A
          AliasTarget:
            DNSName: !GetAtt [ CloudFront{{@index}}, DomainName ]
            EvaluateTargetHealth: false
            HostedZoneId: Z2FDTNDATAQYW2
        {{/each}}
