Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save amosshapira/fdb05a9d48848cff036005f8dd548f8f to your computer and use it in GitHub Desktop.
Save amosshapira/fdb05a9d48848cff036005f8dd548f8f to your computer and use it in GitHub Desktop.
CloudFormation template to redirect "company.co.uk" to "company.com"
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Redirect {www.,}company.co.uk web site to company.com, including SSL",
"Resources": {
"BucketCompanyCoUk": {
"Properties": {
"AccessControl": "PublicRead",
"BucketName": "company.co.uk",
"CorsConfiguration": {
"CorsRules": [
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET"
],
"AllowedOrigins": [
"*"
],
"ExposedHeaders": [
"Date"
],
"MaxAge": 3600
}
]
},
"WebsiteConfiguration": {
"RedirectAllRequestsTo": {
"HostName": "company.com",
"Protocol": "https"
}
}
},
"Type": "AWS::S3::Bucket"
},
"BucketPolicyCompanyCoUk": {
"DependsOn": "BucketCompanyCoUk",
"Properties": {
"Bucket": "company.co.uk",
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Principal": "*",
"Resource": "arn:aws:s3:::company.co.uk/*"
}
]
}
},
"Type": "AWS::S3::BucketPolicy"
},
"BucketPolicyWwwCompanyCoUk": {
"DependsOn": "BucketWwwCompanyCoUk",
"Properties": {
"Bucket": "www.company.co.uk",
"PolicyDocument": {
"Statement": [
{
"Action": [
"s3:GetObject"
],
"Effect": "Allow",
"Principal": "*",
"Resource": "arn:aws:s3:::www.company.co.uk/*"
}
]
}
},
"Type": "AWS::S3::BucketPolicy"
},
"BucketWwwCompanyCoUk": {
"Properties": {
"AccessControl": "PublicRead",
"BucketName": "www.company.co.uk",
"CorsConfiguration": {
"CorsRules": [
{
"AllowedHeaders": [
"*"
],
"AllowedMethods": [
"GET"
],
"AllowedOrigins": [
"*"
],
"ExposedHeaders": [
"Date"
],
"MaxAge": 3600
}
]
},
"WebsiteConfiguration": {
"RedirectAllRequestsTo": {
"HostName": "company.com",
"Protocol": "https"
}
}
},
"Type": "AWS::S3::Bucket"
},
"DNSRecord": {
"Properties": {
"Comment": "Records for the root of company.co.uk",
"HostedZoneName": "company.co.uk.",
"RecordSets": [
{
"AliasTarget": {
"DNSName": {
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"Distribution",
"DomainName"
]
},
"."
]
]
},
"HostedZoneId": "Z............2"
},
"Name": "company.co.uk.",
"Type": "A"
},
{
"AliasTarget": {
"DNSName": {
"Fn::Join": [
"",
[
{
"Fn::GetAtt": [
"Distribution",
"DomainName"
]
},
"."
]
]
},
"HostedZoneId": "Z............2"
},
"Name": "www.company.co.uk.",
"Type": "A"
}
]
},
"Type": "AWS::Route53::RecordSetGroup"
},
"Distribution": {
"DependsOn": [
"BucketCompanyCoUk",
"BucketWwwCompanyCoUk"
],
"Properties": {
"DistributionConfig": {
"Aliases": [
"company.co.uk",
"www.company.co.uk"
],
"DefaultCacheBehavior": {
"ForwardedValues": {
"QueryString": "false"
},
"TargetOriginId": "bucketOriginCompanyCoUk",
"ViewerProtocolPolicy": "allow-all"
},
"Enabled": "true",
"Origins": [
{
"CustomOriginConfig": {
"OriginProtocolPolicy": "http-only"
},
"DomainName": "company.co.uk.s3-website-us-east-1.amazonaws.com",
"Id": "bucketOriginCompanyCoUk"
},
{
"CustomOriginConfig": {
"OriginProtocolPolicy": "http-only"
},
"DomainName": "www.company.co.uk.s3-website-us-east-1.amazonaws.com",
"Id": "bucketOriginWwwCompanyCoUk"
}
],
"PriceClass": "PriceClass_All",
"ViewerCertificate": {
"AcmCertificateArn": "arn:aws:acm:us-east-1:aws-account:certificate/ACM-issued-ssl-certificate-id",
"SslSupportMethod": "sni-only"
}
}
},
"Type": "AWS::CloudFront::Distribution"
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment