AWSTemplateFormatVersion: '2010-09-09'

Parameters:
    BucketName: # Ask for bucket name
        Type: String
    RegionName: # Ask for region
        Type: String
        Default: 'us-east-1'

# Resources to create
Resources:
    # Create s3 bucket, allowing
    WebBucket:
        Type: AWS::S3::Bucket
        Properties:
            AccessControl: PublicRead
            BucketName: !Ref BucketName
            PublicAccessBlockConfiguration:
                BlockPublicAcls: false
                BlockPublicPolicy: false
                IgnorePublicAcls: false
                RestrictPublicBuckets: false

    # Create Origin Identity
    CloudFrontOriginIdentity:
        Type: AWS::CloudFront::CloudFrontOriginAccessIdentity
        Properties:
            CloudFrontOriginAccessIdentityConfig:
                Comment: 'origin identity'
    # Create Bucket Policy
    BucketPolicy:
        Type: AWS::S3::BucketPolicy
        Properties:
            Bucket: !Ref WebBucket
            PolicyDocument:
                Version: '2012-10-17'
                Id: 'PolicyForCloudFront'
                Statement:
                    - Effect: Allow
                      Sid: '1'
                      Principal:
                          AWS: !Sub 'arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity ${CloudFrontOriginIdentity}'
                      Action: 's3:GetObject'
                      Resource: !Sub 'arn:aws:s3:::${WebBucket}/*'
    # Create Policy for cache
    CachePolicy:
        Type: AWS::CloudFront::CachePolicy
        Properties:
            CachePolicyConfig:
                Name: !Sub '${BucketName}CachePolicy'
                DefaultTTL: 86400
                MaxTTL: 31536000
                MinTTL: 1
                ParametersInCacheKeyAndForwardedToOrigin:
                    CookiesConfig:
                        CookieBehavior: none
                    EnableAcceptEncodingBrotli: true
                    EnableAcceptEncodingGzip: true
                    HeadersConfig:
                        HeaderBehavior: none
                    QueryStringsConfig:
                        QueryStringBehavior: none
    # Create CloudFront distro
    PublicDistribution:
        Type: AWS::CloudFront::Distribution
        Properties:
            DistributionConfig:
                Origins:
                    - DomainName: !Sub '${BucketName}.s3.${RegionName}.amazonaws.com'
                      Id: MyCustomOriginId
                      S3OriginConfig:
                          OriginAccessIdentity: !Sub 'origin-access-identity/cloudfront/${CloudFrontOriginIdentity}'
                Enabled: true
                IPV6Enabled: true
                Comment: Some comment
                CacheBehaviors:
                    - CachePolicyId: !Ref CachePolicy
                      PathPattern: /*
                      TargetOriginId: MyCustomOriginId
                      ViewerProtocolPolicy: allow-all
                DefaultCacheBehavior:
                    CachePolicyId: !Ref CachePolicy
                    AllowedMethods:
                        - GET
                        - HEAD
                    TargetOriginId: MyCustomOriginId
                    ViewerProtocolPolicy: allow-all
        DependsOn:
            - WebBucket
