-
Notifications
You must be signed in to change notification settings - Fork 4
/
cloudfront-s3-sync.sh
123 lines (113 loc) · 3.96 KB
/
cloudfront-s3-sync.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/bin/bash
# Change the region
region="eu-west-1"
# Change the bucket with a unique name
bucket_name="instance-scheduler-2022"
website_directory="./html"
# Create a new bucket
aws s3 mb --region $region "s3://$bucket_name"
# Enable public access to the bucket
aws s3api put-public-access-block \
--bucket $bucket_name \
--public-access-block-configuration "BlockPublicAcls=false,IgnorePublicAcls=false,BlockPublicPolicy=false,RestrictPublicBuckets=false"
# Update the bucket policy for public read access:
aws s3api put-bucket-policy --bucket $bucket_name --policy "{
\"Version\": \"2012-10-17\",
\"Statement\": [
{
\"Sid\": \"PublicReadGetObject\",
\"Effect\": \"Allow\",
\"Principal\": \"*\",
\"Action\": \"s3:GetObject\",
\"Resource\": \"arn:aws:s3:::$bucket_name/*\"
}
]
}"
# Enable the s3 bucket to host an `index` and `error` html page
aws s3 website "s3://$bucket_name" --index-document index.html --error-document index.html
# Upload you website
aws s3 sync $website_directory "s3://$bucket_name/"
# Bucket URL
echo "curl http://$bucket_name.s3-website.$region.amazonaws.com"
# CloudFront
aws cloudfront create-distribution --cli-input-json '
{
"DistributionConfig": {
"Comment": "",
"CacheBehaviors": {
"Quantity": 0
},
"Logging": {
"Bucket": "",
"Prefix": "",
"Enabled": false,
"IncludeCookies": false
},
"Origins": {
"Items": [
{
"OriginPath": "",
"CustomOriginConfig": {
"OriginProtocolPolicy": "http-only",
"HTTPPort": 80,
"HTTPSPort": 443
},
"Id": "'$bucket_name.s3-website.$region.amazonaws.com'",
"DomainName": "'$bucket_name.s3-website.$region.amazonaws.com'"
}
],
"Quantity": 1
},
"DefaultRootObject": "index.html",
"PriceClass": "PriceClass_All",
"Enabled": true,
"DefaultCacheBehavior": {
"TrustedSigners": {
"Enabled": false,
"Quantity": 0
},
"TargetOriginId": "'$bucket_name.s3-website.$region.amazonaws.com'",
"ViewerProtocolPolicy": "redirect-to-https",
"ForwardedValues": {
"Headers": {
"Quantity": 0
},
"Cookies": {
"Forward": "none"
},
"QueryString": false
},
"SmoothStreaming": false,
"AllowedMethods": {
"Items": [
"GET",
"HEAD"
],
"CachedMethods": {
"Items": [
"GET",
"HEAD"
],
"Quantity": 2
},
"Quantity": 2
},
"MinTTL": 0
},
"CallerReference": "Mon May 25 21:39:53 CEST 2015",
"CustomErrorResponses": {
"Quantity": 0
},
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Quantity": 0
}
}
}
}'
cloudfrontDomainName="$(aws cloudfront list-distributions --query "DistributionList.Items[*].{domainname:DomainName,origin:Origins.Items[0].DomainName}[?origin=='$bucket_name.s3-website.$region.amazonaws.com'].domainname" --output text)"
echo "########################################################################"
echo "Click on https://$cloudfrontDomainName"
echo "CloudFront takes around 5 minutes to be deployed"
echo "########################################################################"