Skip to content

Instantly share code, notes, and snippets.

@andresbravog
Last active September 19, 2017 02:29
Show Gist options
  • Save andresbravog/002ed6da80ce9a23216c02e6a6b857da to your computer and use it in GitHub Desktop.
Save andresbravog/002ed6da80ce9a23216c02e6a6b857da to your computer and use it in GitHub Desktop.
ServerLess Article
aws cloudfront create-invalidation --cli-input-json '{"DistributionId":"'"$AWS_CLOUDFRONT_ID"'","InvalidationBatch":{"Paths":{"Quantity":2,"Items":["/index.html","/*"]},"CallerReference": "'$(date "+%s")'"}}'
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Cloud Formation Template for S3 based static site",
"Mappings": {
"RegionMap": {
"us-east-1": {
"S3hostedzoneID": "Z3AQBSTGFYJSTF",
"websiteendpoint": "s3-website-us-east-1.amazonaws.com"
},
"us-west-1": {
"S3hostedzoneID": "Z2F56UZL2M1ACD",
"websiteendpoint": "s3-website-us-west-1.amazonaws.com"
},
"us-west-2": {
"S3hostedzoneID": "Z3BJ6K6RIION7M",
"websiteendpoint": "s3-website-us-west-2.amazonaws.com"
},
"eu-west-1": {
"S3hostedzoneID": "Z1BKCTXD74EZPE",
"websiteendpoint": "s3-website-eu-west-1.amazonaws.com"
},
"ap-southeast-1": {
"S3hostedzoneID": "Z3O0J2DXBE1FTB",
"websiteendpoint": "s3-website-ap-southeast-1.amazonaws.com"
},
"ap-southeast-2": {
"S3hostedzoneID": "Z1WCIGYICN2BYD",
"websiteendpoint": "s3-website-ap-southeast-2.amazonaws.com"
},
"ap-northeast-1": {
"S3hostedzoneID": "Z2M4EHUR26P7ZW",
"websiteendpoint": "s3-website-ap-northeast-1.amazonaws.com"
},
"sa-east-1": {
"S3hostedzoneID": "Z31GFT0UA1I2HV",
"websiteendpoint": "s3-website-sa-east-1.amazonaws.com"
}
}
},
"Resources": {
"RootBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": "${domain}",
"AccessControl": "PublicRead",
"WebsiteConfiguration": {
"IndexDocument": "index.html",
"ErrorDocument": "404.html"
}
}
},
"WWWBucket": {
"Type": "AWS::S3::Bucket",
"Properties": {
"BucketName": {
"Fn::Join": [
"",
[
"www.",
"${domain}"
]
]
},
"AccessControl": "BucketOwnerFullControl",
"WebsiteConfiguration": {
"RedirectAllRequestsTo": {
"HostName": {
"Ref": "RootBucket"
}
}
}
}
},
"myDNS": {
"Type": "AWS::Route53::RecordSetGroup",
"Properties": {
"HostedZoneName": "${root_domain}.",
"Comment": "Zone apex alias.",
"RecordSets": [
{
"Name": "${domain}.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": {
"Fn::FindInMap": [
"RegionMap",
"${region}",
"S3hostedzoneID"
]
},
"DNSName": {
"Fn::FindInMap": [
"RegionMap",
"${region}",
"websiteendpoint"
]
}
}
}
]
}
},
"myDistribution": {
"Type": "AWS::CloudFront::Distribution",
"Properties": {
"DistributionConfig": {
"Origins": [
{
"DomainName": {
"Fn::GetAtt": [
"WWWBucket",
"DomainName"
]
},
"Id": "S3.${domain}.Distribution",
"S3OriginConfig": {}
}
],
"DefaultCacheBehavior": {
"AllowedMethods": [
"DELETE",
"GET",
"HEAD",
"OPTIONS",
"PATCH",
"POST",
"PUT"
],
"TargetOriginId": "S3.${domain}.Distribution",
"ForwardedValues": {
"QueryString": "false",
"Cookies": {
"Forward": "none"
}
},
"ViewerProtocolPolicy": "allow-all"
},
"Enabled": "true",
"DefaultRootObject": "index.html",
"Aliases": [
"${domain}"
],
"ViewerCertificate": {
"CloudFrontDefaultCertificate": "true"
}
}
}
}
},
"Outputs": {
"WebsiteURL": {
"Value": {
"Fn::GetAtt": [
"RootBucket",
"WebsiteURL"
]
},
"Description": "URL for website hosted on S3"
},
"RootBucketName": {
"Description": "ARN of the Root S3 Bucket",
"Value": {
"Ref": "RootBucket"
}
},
"RootBucketDomain": {
"Description": "Domain of the Root S3 Bucket website",
"Value": {
"Fn::GetAtt": [
"RootBucket",
"DomainName"
]
}
},
"WWWBucketDomain": {
"Description": "Domain of the WWW S3 Bucket website",
"Value": {
"Fn::GetAtt": [
"WWWBucket",
"DomainName"
]
}
},
"WWWBucketName": {
"Description": "ARN of the WWW S3 Bucket",
"Value": {
"Ref": "WWWBucket"
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment