<commit_before>---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Static Website: combining S3, CloudFront and Route53, a cloudonaut.io template'
Metadata:
  'AWS::CloudFormation::Interface':
    ParameterGroups:
    - Label:
        default: 'DNS Parameters'
      Parameters:
      - DomainName
      - RedirectDomainName
      - HostedZoneId
    - Label:
        default: 'SSL/TLS Parameters'
      Parameters:
      - CertificateType
      - ExistingCertificate
Parameters:
  DomainName:
    Description: 'The primary domain name of the static website (e.g. widdix.de).'
    Type: String
  RedirectDomainName:
    Description: 'Optional domain name redirecting to DomainName (e.g. www.widdix.de).'
    Type: String
    Default: ''
  CertificateType:
    Description: 'Indicates whether to use the certificate from the AWS Certificate Manager or AWS Identity and Access Management. CreateAcmCertificate only works in the us-east-1 region.'
    Type: String
    Default: IamCertificateId
    AllowedValues:
    - AcmCertificateArn
    - IamCertificateId
    - CreateAcmCertificate
  ExistingCertificate:
    Description: 'Optional ACM Certificate ARN or IAM Certificate ID. Must be created in the us-east-1 region!'
    Type: String
    Default: ''
  HostedZoneId:
    Description: 'The Id of the hosted zone managed by AWS Route53.'
    Type: String
Conditions:
  HasRedirectDomainName: !Not [!Equals [!Ref RedirectDomainName, '']]
  HasAcmCertificateArn: !Equals [!Ref CertificateType, 'AcmCertificateArn']
  HasIamCertificateId: !Equals [!Ref CertificateType, 'IamCertificateId']
  HasCreateAcmCertificate: !Equals [!Ref CertificateType, 'CreateAcmCertificate']
Resources:
  Certificate:
    Condition: HasCreateAcmCertificate
    Type: 'AWS::CertificateManager::Certificate'
    Properties: 
      DomainName: !Ref DomainName
      DomainValidationOptions: !If
      - HasRedirectDomainName
      - - DomainName: !Ref DomainName
          ValidationDomain: !Ref DomainName
        - DomainName: !Ref RedirectDomainName
          ValidationDomain: !Ref DomainName
      - - DomainName: !Ref DomainName
          ValidationDomain: !Ref DomainName
      SubjectAlternativeNames: !If [HasRedirectDomainName, [!Ref RedirectDomainName], []]
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref DomainName
      WebsiteConfiguration:
        IndexDocument: 'index.html'
  S3BucketPolicy:
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref S3Bucket
      PolicyDocument:
        Statement:
        - Action:
          - 's3:GetObject'
          Effect: Allow
          Resource:
          - !Sub 'arn:aws:s3:::${S3Bucket}/*'
          Principal: '*'
  CloudFrontDistribution:
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        Aliases:
        - !Ref DomainName
        Comment: !Ref DomainName
        Origins:
        - DomainName: !Sub '${S3Bucket}.s3-website-${AWS::Region}.amazonaws.com'
          Id: s3origin
          CustomOriginConfig:
            OriginProtocolPolicy: 'http-only'
        DefaultCacheBehavior:
          AllowedMethods:
          - DELETE
          - GET
          - HEAD
          - OPTIONS
          - PATCH
          - POST
          - PUT
          ForwardedValues:
            QueryString: false
            Cookies:
              Forward: none
          TargetOriginId: s3origin
          ViewerProtocolPolicy: 'redirect-to-https'
        Enabled: true
        HttpVersion: http2
        PriceClass: 'PriceClass_All'
        ViewerCertificate:
          AcmCertificateArn: !If [HasCreateAcmCertificate, !Ref Certificate, !If [HasAcmCertificateArn, !Ref ExistingCertificate, !Ref 'AWS::NoValue']] 
          IamCertificateId: !If [HasIamCertificateId, !Ref ExistingCertificate, !Ref 'AWS::NoValue'] 
          SslSupportMethod: 'sni-only'
          MinimumProtocolVersion: TLSv1
  Route53Record:
    Type: 'AWS::Route53::RecordSetGroup'
    Properties:
      HostedZoneId: !Ref HostedZoneId
      RecordSets:
      - Name: !Ref DomainName
        Type: A
        AliasTarget:
          # Magic AWS number:  For CloudFront, use Z2FDTNDATAQYW2.
          HostedZoneId: Z2FDTNDATAQYW2
          DNSName: !GetAtt 'CloudFrontDistribution.DomainName'
  S3BucketRedirect:
    Condition: HasRedirectDomainName
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref RedirectDomainName
      WebsiteConfiguration:
        RedirectAllRequestsTo:
          HostName: !Ref DomainName
          Protocol: https
  CloudFrontDistributionRedirect:
    Condition: HasRedirectDomainName
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        Aliases:
        - !Ref RedirectDomainName
        Comment: !Ref RedirectDomainName
        Origins:
        - DomainName: !Sub '${S3BucketRedirect}.s3-website-${AWS::Region}.amazonaws.com'
          Id: s3origin
          CustomOriginConfig:
            OriginProtocolPolicy: 'http-only'
        DefaultCacheBehavior:
          AllowedMethods:
          - DELETE
          - GET
          - HEAD
          - OPTIONS
          - PATCH
          - POST
          - PUT
          ForwardedValues:
            QueryString: false
            Cookies:
              Forward: none
          TargetOriginId: s3origin
          ViewerProtocolPolicy: 'redirect-to-https'
        Enabled: true
        HttpVersion: http2
        PriceClass: 'PriceClass_All'
        ViewerCertificate:
          AcmCertificateArn: !If [HasCreateAcmCertificate, !Ref Certificate, !If [HasAcmCertificateArn, !Ref ExistingCertificate, !Ref 'AWS::NoValue']] 
          IamCertificateId: !If [HasIamCertificateId, !Ref ExistingCertificate, !Ref 'AWS::NoValue'] 
          SslSupportMethod: 'sni-only'
          MinimumProtocolVersion: TLSv1
  Route53RecordRedirect:
    Condition: HasRedirectDomainName
    Type: 'AWS::Route53::RecordSetGroup'
    Properties:
      HostedZoneId: !Ref HostedZoneId
      RecordSets:
      - Name: !Ref RedirectDomainName
        Type: A
        AliasTarget:
          # Magic AWS number:  For CloudFront, use Z2FDTNDATAQYW2.
          HostedZoneId: Z2FDTNDATAQYW2
          DNSName: !GetAtt 'CloudFrontDistributionRedirect.DomainName'
Outputs:
  TemplateID:
    Description: 'cloudonaut.io template id'
    Value: 'static-website/static-website'
  URL:
    Description: 'URL to static website.'
    Value: !Sub 'https://${DomainName}'
    Export:
      Name: !Sub '${AWS::StackName}-URL'
<commit_msg>support both dot region and dash region s3 website endpoints (#97)<commit_after>---
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Static Website: combining S3, CloudFront and Route53, a cloudonaut.io template'
Metadata:
  'AWS::CloudFormation::Interface':
    ParameterGroups:
    - Label:
        default: 'DNS Parameters'
      Parameters:
      - DomainName
      - RedirectDomainName
      - HostedZoneId
    - Label:
        default: 'SSL/TLS Parameters'
      Parameters:
      - CertificateType
      - ExistingCertificate
Parameters:
  DomainName:
    Description: 'The primary domain name of the static website (e.g. widdix.de).'
    Type: String
  RedirectDomainName:
    Description: 'Optional domain name redirecting to DomainName (e.g. www.widdix.de).'
    Type: String
    Default: ''
  CertificateType:
    Description: 'Indicates whether to use the certificate from the AWS Certificate Manager or AWS Identity and Access Management. CreateAcmCertificate only works in the us-east-1 region.'
    Type: String
    Default: IamCertificateId
    AllowedValues:
    - AcmCertificateArn
    - IamCertificateId
    - CreateAcmCertificate
  ExistingCertificate:
    Description: 'Optional ACM Certificate ARN or IAM Certificate ID. Must be created in the us-east-1 region!'
    Type: String
    Default: ''
  HostedZoneId:
    Description: 'The Id of the hosted zone managed by AWS Route53.'
    Type: String
Conditions:
  HasRedirectDomainName: !Not [!Equals [!Ref RedirectDomainName, '']]
  HasAcmCertificateArn: !Equals [!Ref CertificateType, 'AcmCertificateArn']
  HasIamCertificateId: !Equals [!Ref CertificateType, 'IamCertificateId']
  HasCreateAcmCertificate: !Equals [!Ref CertificateType, 'CreateAcmCertificate']
Resources:
  Certificate:
    Condition: HasCreateAcmCertificate
    Type: 'AWS::CertificateManager::Certificate'
    Properties: 
      DomainName: !Ref DomainName
      DomainValidationOptions: !If
      - HasRedirectDomainName
      - - DomainName: !Ref DomainName
          ValidationDomain: !Ref DomainName
        - DomainName: !Ref RedirectDomainName
          ValidationDomain: !Ref DomainName
      - - DomainName: !Ref DomainName
          ValidationDomain: !Ref DomainName
      SubjectAlternativeNames: !If [HasRedirectDomainName, [!Ref RedirectDomainName], []]
  S3Bucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref DomainName
      WebsiteConfiguration:
        IndexDocument: 'index.html'
  S3BucketPolicy:
    Type: 'AWS::S3::BucketPolicy'
    Properties:
      Bucket: !Ref S3Bucket
      PolicyDocument:
        Statement:
        - Action:
          - 's3:GetObject'
          Effect: Allow
          Resource:
          - !Sub 'arn:aws:s3:::${S3Bucket}/*'
          Principal: '*'
  CloudFrontDistribution:
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        Aliases:
        - !Ref DomainName
        Comment: !Ref DomainName
        Origins:
        - DomainName: !Select [ 2, !Split [ /, !GetAtt S3Bucket.WebsiteURL ] ]
          Id: s3origin
          CustomOriginConfig:
            OriginProtocolPolicy: 'http-only'
        DefaultCacheBehavior:
          AllowedMethods:
          - DELETE
          - GET
          - HEAD
          - OPTIONS
          - PATCH
          - POST
          - PUT
          ForwardedValues:
            QueryString: false
            Cookies:
              Forward: none
          TargetOriginId: s3origin
          ViewerProtocolPolicy: 'redirect-to-https'
        Enabled: true
        HttpVersion: http2
        PriceClass: 'PriceClass_All'
        ViewerCertificate:
          AcmCertificateArn: !If [HasCreateAcmCertificate, !Ref Certificate, !If [HasAcmCertificateArn, !Ref ExistingCertificate, !Ref 'AWS::NoValue']] 
          IamCertificateId: !If [HasIamCertificateId, !Ref ExistingCertificate, !Ref 'AWS::NoValue'] 
          SslSupportMethod: 'sni-only'
          MinimumProtocolVersion: TLSv1
  Route53Record:
    Type: 'AWS::Route53::RecordSetGroup'
    Properties:
      HostedZoneId: !Ref HostedZoneId
      RecordSets:
      - Name: !Ref DomainName
        Type: A
        AliasTarget:
          # Magic AWS number:  For CloudFront, use Z2FDTNDATAQYW2.
          HostedZoneId: Z2FDTNDATAQYW2
          DNSName: !GetAtt 'CloudFrontDistribution.DomainName'
  S3BucketRedirect:
    Condition: HasRedirectDomainName
    Type: 'AWS::S3::Bucket'
    Properties:
      BucketName: !Ref RedirectDomainName
      WebsiteConfiguration:
        RedirectAllRequestsTo:
          HostName: !Ref DomainName
          Protocol: https
  CloudFrontDistributionRedirect:
    Condition: HasRedirectDomainName
    Type: 'AWS::CloudFront::Distribution'
    Properties:
      DistributionConfig:
        Aliases:
        - !Ref RedirectDomainName
        Comment: !Ref RedirectDomainName
        Origins:
        - DomainName: !Select [ 2, !Split [ /, !GetAtt S3BucketRedirect.WebsiteURL ] ]
          Id: s3origin
          CustomOriginConfig:
            OriginProtocolPolicy: 'http-only'
        DefaultCacheBehavior:
          AllowedMethods:
          - DELETE
          - GET
          - HEAD
          - OPTIONS
          - PATCH
          - POST
          - PUT
          ForwardedValues:
            QueryString: false
            Cookies:
              Forward: none
          TargetOriginId: s3origin
          ViewerProtocolPolicy: 'redirect-to-https'
        Enabled: true
        HttpVersion: http2
        PriceClass: 'PriceClass_All'
        ViewerCertificate:
          AcmCertificateArn: !If [HasCreateAcmCertificate, !Ref Certificate, !If [HasAcmCertificateArn, !Ref ExistingCertificate, !Ref 'AWS::NoValue']] 
          IamCertificateId: !If [HasIamCertificateId, !Ref ExistingCertificate, !Ref 'AWS::NoValue'] 
          SslSupportMethod: 'sni-only'
          MinimumProtocolVersion: TLSv1
  Route53RecordRedirect:
    Condition: HasRedirectDomainName
    Type: 'AWS::Route53::RecordSetGroup'
    Properties:
      HostedZoneId: !Ref HostedZoneId
      RecordSets:
      - Name: !Ref RedirectDomainName
        Type: A
        AliasTarget:
          # Magic AWS number:  For CloudFront, use Z2FDTNDATAQYW2.
          HostedZoneId: Z2FDTNDATAQYW2
          DNSName: !GetAtt 'CloudFrontDistributionRedirect.DomainName'
Outputs:
  TemplateID:
    Description: 'cloudonaut.io template id'
    Value: 'static-website/static-website'
  URL:
    Description: 'URL to static website.'
    Value: !Sub 'https://${DomainName}'
    Export:
      Name: !Sub '${AWS::StackName}-URL'

{'fraction_non_alphanumeric': 0.08382544673671294, 'fraction_numerical': 0.008359536774292508, 'mean_word_length': 2.5521656224462, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5610101', 'n_tokens_mistral': 3835, 'n_tokens_neox': 3640, 'n_words': 898}