Created
September 5, 2019 17:21
-
-
Save craftdelivery/5ec1abadd25f1af97c84dbc25337e1c2 to your computer and use it in GitHub Desktop.
Automate cloudfront distribution for s3 bucket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# usage ./create.sh your-bucket-name | |
# $1=your-bucket-name | |
# create a public bucket for images (your-bucket-name) | |
# create a logging bucket (s3-your-bucket-name) | |
# create bucket policy json: your-bucket-name.json | |
# set bucket policy | |
# create cloudfront config json: your-bucket-name-cf.json | |
# create a cloudfront distribution for the bucket using the logging bucket for logs | |
REGION=ca-central-1 | |
POLICY=AWS_POLICY | |
SID=AWS_SID | |
echo "CREATING $1" | |
aws s3api create-bucket --bucket $1 --acl public-read --region $REGION --create-bucket-configuration LocationConstraint=$REGION | |
tee "$1.json" <<EOF | |
{ | |
"Version": "2012-10-17", | |
"Id": "Policy${POLICY}", | |
"Statement": [ | |
{ | |
"Sid": "Stmt${SID}", | |
"Effect": "Allow", | |
"Principal": { | |
"AWS": "*" | |
}, | |
"Action": "s3:GetObject", | |
"Resource": "arn:aws:s3:::${1}/*" | |
} | |
] | |
} | |
EOF | |
echo "PUTTING POLICY" | |
aws s3api put-bucket-policy --bucket $1 --policy file://$1.json | |
echo "CREATING LOGGING BUCKET s3-$1" | |
aws s3api create-bucket --bucket "s3-$1" --region $REGION --create-bucket-configuration LocationConstraint=$REGION | |
echo "CREATING CLOUDFRONT DISTRIBUTION" | |
tee "$1-cf.json" <<EOF | |
{ | |
"CallerReference": "${1}", | |
"Comment": "", | |
"CacheBehaviors": { | |
"Quantity": 0 | |
}, | |
"IsIPV6Enabled": true, | |
"Logging": { | |
"Bucket": "s3-${1}.s3.amazonaws.com", | |
"Prefix": "", | |
"Enabled": true, | |
"IncludeCookies": true | |
}, | |
"WebACLId": "", | |
"Origins": { | |
"Items": [ | |
{ | |
"S3OriginConfig": { | |
"OriginAccessIdentity": "" | |
}, | |
"OriginPath": "", | |
"CustomHeaders": { | |
"Quantity": 0 | |
}, | |
"Id": "${1}", | |
"DomainName": "${1}.s3.amazonaws.com" | |
} | |
], | |
"Quantity": 1 | |
}, | |
"DefaultRootObject": "", | |
"PriceClass": "PriceClass_100", | |
"Enabled": true, | |
"DefaultCacheBehavior": { | |
"TrustedSigners": { | |
"Enabled": false, | |
"Quantity": 0 | |
}, | |
"LambdaFunctionAssociations": { | |
"Quantity": 0 | |
}, | |
"TargetOriginId": "${1}", | |
"ViewerProtocolPolicy": "https-only", | |
"ForwardedValues": { | |
"Headers": { | |
"Quantity": 0 | |
}, | |
"Cookies": { | |
"Forward": "none" | |
}, | |
"QueryString": false | |
}, | |
"MaxTTL": 31536000, | |
"SmoothStreaming": false, | |
"DefaultTTL": 86400, | |
"AllowedMethods": { | |
"Items": [ | |
"GET", | |
"HEAD" | |
], | |
"Quantity": 2 | |
}, | |
"MinTTL": 0, | |
"Compress": false | |
}, | |
"ViewerCertificate": { | |
"CloudFrontDefaultCertificate": true, | |
"MinimumProtocolVersion": "TLSv1", | |
"CertificateSource": "cloudfront" | |
}, | |
"CustomErrorResponses": { | |
"Quantity": 0 | |
}, | |
"HttpVersion": "http2", | |
"Restrictions": { | |
"GeoRestriction": { | |
"RestrictionType": "none", | |
"Quantity": 0 | |
} | |
}, | |
"Aliases": { | |
"Quantity": 0 | |
} | |
} | |
EOF | |
aws cloudfront create-distribution --distribution-config file://$1-cf.json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment