{
  "AWSTemplateFormatVersion" : "2010-09-09",

  "Description" : "AWS CloudFormation Sample Template S3_Website_With_CloudFront_Distribution: Sample template showing how to create a website with a custom DNS name, hosted on Amazon S3 and served via Amazone CloudFront. It assumes you already have a Hosted Zone registered with Amazon Route 53. **WARNING** This template creates an Amazon Route 53 DNS record, an S3 bucket and a CloudFront distribution. You will be billed for the AWS resources used if you create a stack from this template.",

  "Parameters" : {
    "HostedZone" : {
      "Type" : "String",
      "Description" : "The DNS name of an existing Amazon Route 53 hosted zone",
      "AllowedPattern" : "(?!-)[a-zA-Z0-9-.]{1,63}(?<!-)",
      "ConstraintDescription" : "must be a valid DNS zone name."
    }
  },

  "Mappings" : {
    "Region2S3WebsiteSuffix": {
      "af-south-1"     : { "Suffix": ".s3-website.af-south-1.amazonaws.com" },
      "ap-east-1"      : { "Suffix": ".s3-website.ap-east-1.amazonaws.com" },
      "ap-northeast-1" : { "Suffix": ".s3-website-ap-northeast-1.amazonaws.com" },
      "ap-northeast-2" : { "Suffix": ".s3-website.ap-northeast-2.amazonaws.com" },
      "ap-northeast-3" : { "Suffix": ".s3-website.ap-northeast-3.amazonaws.com" },
      "ap-south-1"     : { "Suffix": ".s3-website.ap-south-1.amazonaws.com" },
      "ap-south-2"     : { "Suffix": ".s3-website.ap-south-2.amazonaws.com" },
      "ap-southeast-1" : { "Suffix": ".s3-website-ap-southeast-1.amazonaws.com" },
      "ap-southeast-2" : { "Suffix": ".s3-website-ap-southeast-2.amazonaws.com" },
      "ap-southeast-3" : { "Suffix": ".s3-website-ap-southeast-3.amazonaws.com" },
      "ap-southeast-4" : { "Suffix": ".s3-website-ap-southeast-4.amazonaws.com" },
      "il-central-1"   : { "Suffix": ".s3-website-il-central-1.amazonaws.com" },
      "ca-central-1"   : { "Suffix": ".s3-website.ca-central-1.amazonaws.com" },
      "cn-north-1"     : { "Suffix": ".s3-website.cn-north-1.amazonaws.com.cn" },
      "cn-northwest-1" : { "Suffix": ".s3-website.cn-northwest-1.amazonaws.com.cn" },
      "eu-central-1"   : { "Suffix": ".s3-website.eu-central-1.amazonaws.com" },
      "eu-north-1"     : { "Suffix": ".s3-website.eu-north-1.amazonaws.com" },
      "eu-south-1"     : { "Suffix": ".s3-website.eu-south-1.amazonaws.com" },
      "eu-west-1"      : { "Suffix": ".s3-website-eu-west-1.amazonaws.com" },
      "eu-west-2"      : { "Suffix": ".s3-website.eu-west-2.amazonaws.com" },
      "eu-west-3"      : { "Suffix": ".s3-website.eu-west-3.amazonaws.com" },
      "me-south-1"     : { "Suffix": ".s3-website.me-south-1.amazonaws.com" },
      "me-central-1"   : { "Suffix": ".s3-website-me-central-1.amazonaws.com" },
      "eu-south-2"     : { "Suffix": ".s3-website-eu-south-2.amazonaws.com" },
      "eu-central-2"   : { "Suffix": ".s3-website-eu-central-2.amazonaws.com" },
      "sa-east-1"      : { "Suffix": ".s3-website-sa-east-1.amazonaws.com" },
      "us-east-1"      : { "Suffix": ".s3-website-us-east-1.amazonaws.com" },
      "us-east-2"      : { "Suffix": ".s3-website.us-east-2.amazonaws.com" },
      "us-west-1"      : { "Suffix": ".s3-website-us-west-1.amazonaws.com" },
      "us-west-2"      : { "Suffix": ".s3-website-us-west-2.amazonaws.com" }
    }

  },

  "Resources" : {
    "S3BucketForWebsiteContent" : {
      "Type" : "AWS::S3::Bucket",
      "Properties" : {
        "AccessControl" : "PublicRead",
        "WebsiteConfiguration" : {
           "IndexDocument" : "index.html",
           "ErrorDocument" : "error.html"
        }
      }
    },

    "WebsiteCDN" : {
      "Type" : "AWS::CloudFront::Distribution",
      "Properties" : {
         "DistributionConfig" : {
           "Comment" : "CDN for S3-backed website",
           "Aliases" : [{ "Fn::Join" : [ "", [{"Ref" : "AWS::StackName"}, {"Ref" : "AWS::AccountId"}, ".", {"Ref" : "AWS::Region"}, ".", { "Ref" : "HostedZone" }]]}],
           "Enabled" : "true",
	   "DefaultCacheBehavior" : {
	     "ForwardedValues" : { "QueryString" : "true" },
	     "TargetOriginId" : "only-origin",
	     "ViewerProtocolPolicy" : "allow-all"
	   },
           "DefaultRootObject" : "index.html",
	   "Origins" : [
	     { "CustomOriginConfig" : 
                 {
                   "HTTPPort" : "80",
                   "HTTPSPort" : "443",
                   "OriginProtocolPolicy" : "http-only"
                 },
               "DomainName" : { "Fn::Join" : ["", [{"Ref" : "S3BucketForWebsiteContent"},
                                                   {"Fn::FindInMap" : [ "Region2S3WebsiteSuffix", {"Ref" : "AWS::Region"}, "Suffix" ]}]]},
	       "Id" : "only-origin"
             }]
         }
      }
    },

    "WebsiteDNSName" : {
      "Type" : "AWS::Route53::RecordSet",
      "Properties" : {
        "HostedZoneName" : { "Fn::Join" : [ "", [{ "Ref" : "HostedZone" }, "."]]},
        "Comment" : "CNAME redirect custom name to CloudFront distribution",
        "Name" : { "Fn::Join" : [ "", [{"Ref" : "AWS::StackName"}, {"Ref" : "AWS::AccountId"}, ".", {"Ref" : "AWS::Region"}, ".", { "Ref" : "HostedZone" }]]},
        "Type" : "CNAME",
        "TTL" : "900",
        "ResourceRecords" : [{ "Fn::Join" : [ "", ["http://", {"Fn::GetAtt" : ["WebsiteCDN", "DomainName"]} ]]}]
      }
    }
  },

  "Outputs" : {
    "WebsiteURL" : {
      "Value" : {"Fn::Join" : [ "", ["http://", {"Ref" : "WebsiteDNSName"} ]] },
      "Description" : "The URL of the newly created website"
    },
    "BucketName" : {
      "Value" : { "Ref" : "S3BucketForWebsiteContent" },
      "Description" : "Name of S3 bucket to hold website content"
    }
  }
}
