-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathcloudfront-gettingstarted.sh
More file actions
397 lines (337 loc) · 13.2 KB
/
cloudfront-gettingstarted.sh
File metadata and controls
397 lines (337 loc) · 13.2 KB
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#!/bin/bash
# CloudFront Getting Started Tutorial Script
# This script creates an S3 bucket, uploads sample content, creates a CloudFront distribution with OAC,
# and demonstrates how to access content through CloudFront.
set -euo pipefail
# Set up logging
LOG_FILE="cloudfront-tutorial.log"
exec > >(tee -a "$LOG_FILE") 2>&1
echo "Starting CloudFront Getting Started Tutorial at $(date)"
# Function to handle errors
handle_error() {
echo "ERROR: $1" >&2
echo "Resources created before error:"
if [ -n "${BUCKET_NAME:-}" ]; then
echo "- S3 Bucket: $BUCKET_NAME"
fi
if [ -n "${OAC_ID:-}" ]; then
echo "- CloudFront Origin Access Control: $OAC_ID"
fi
if [ -n "${DISTRIBUTION_ID:-}" ]; then
echo "- CloudFront Distribution: $DISTRIBUTION_ID"
fi
echo "Attempting to clean up resources..."
cleanup
exit 1
}
# Function to clean up resources
cleanup() {
echo "Cleaning up resources..."
if [ -n "${DISTRIBUTION_ID:-}" ]; then
echo "Disabling CloudFront distribution $DISTRIBUTION_ID..."
# Get the current configuration and ETag in one call
DIST_CONFIG=$(aws cloudfront get-distribution-config --id "$DISTRIBUTION_ID" 2>/dev/null) || {
echo "Failed to get distribution config. Continuing with cleanup..."
DIST_CONFIG=""
}
if [ -n "$DIST_CONFIG" ]; then
ETAG=$(echo "$DIST_CONFIG" | jq -r '.ETag')
# Modify and update distribution in one pipeline
if echo "$DIST_CONFIG" | jq '.DistributionConfig.Enabled = false' | \
aws cloudfront update-distribution \
--id "$DISTRIBUTION_ID" \
--distribution-config "$(cat)" \
--if-match "$ETAG" 2>/dev/null; then
echo "Waiting for distribution to be disabled (this may take several minutes)..."
aws cloudfront wait distribution-deployed --id "$DISTRIBUTION_ID" 2>/dev/null || {
echo "Distribution deployment wait timed out. Proceeding with deletion..."
}
# Get fresh ETag for deletion
DIST_CONFIG=$(aws cloudfront get-distribution-config --id "$DISTRIBUTION_ID" 2>/dev/null) || {
echo "Failed to get updated config. Skipping distribution deletion..."
DIST_CONFIG=""
}
if [ -n "$DIST_CONFIG" ]; then
ETAG=$(echo "$DIST_CONFIG" | jq -r '.ETag')
aws cloudfront delete-distribution --id "$DISTRIBUTION_ID" --if-match "$ETAG" 2>/dev/null && \
echo "CloudFront distribution deleted." || \
echo "Failed to delete distribution. You may need to delete it manually."
fi
else
echo "Failed to disable distribution. Continuing with cleanup..."
fi
fi
fi
if [ -n "${OAC_ID:-}" ]; then
echo "Deleting Origin Access Control $OAC_ID..."
OAC_DATA=$(aws cloudfront get-origin-access-control --id "$OAC_ID" 2>/dev/null) || {
echo "Failed to get Origin Access Control. You may need to delete it manually."
OAC_DATA=""
}
if [ -n "$OAC_DATA" ]; then
OAC_ETAG=$(echo "$OAC_DATA" | jq -r '.ETag')
aws cloudfront delete-origin-access-control --id "$OAC_ID" --if-match "$OAC_ETAG" 2>/dev/null && \
echo "Origin Access Control deleted." || \
echo "Failed to delete Origin Access Control. You may need to delete it manually."
fi
fi
if [ -n "${BUCKET_NAME:-}" ] && [ "$BUCKET_IS_SHARED" != "true" ]; then
echo "Deleting S3 bucket $BUCKET_NAME and its contents..."
aws s3 rm "s3://$BUCKET_NAME" --recursive 2>/dev/null || {
echo "Failed to remove bucket contents. Continuing with bucket deletion..."
}
aws s3 rb "s3://$BUCKET_NAME" 2>/dev/null && \
echo "S3 bucket deleted." || \
echo "Failed to delete bucket. You may need to delete it manually."
fi
# Clean up temporary files
rm -f temp_disabled_config.json distribution-config.json bucket-policy.json 2>/dev/null || true
rm -rf temp_content 2>/dev/null || true
}
# Trap errors and cleanup
trap 'handle_error "Script interrupted"' INT TERM
# Validate AWS CLI is available
if ! command -v aws &> /dev/null; then
handle_error "AWS CLI is not installed or not in PATH"
fi
# Validate jq is available
if ! command -v jq &> /dev/null; then
handle_error "jq is not installed or not in PATH"
fi
# Validate AWS credentials are configured
if ! aws sts get-caller-identity &> /dev/null; then
handle_error "AWS credentials are not configured or invalid"
fi
# Initialize variables
BUCKET_NAME=""
OAC_ID=""
DISTRIBUTION_ID=""
BUCKET_IS_SHARED=false
# Generate a random identifier for the bucket name using secure random
RANDOM_ID=$(openssl rand -hex 6)
if [ -z "$RANDOM_ID" ]; then
handle_error "Failed to generate random identifier"
fi
# Check for shared prereq bucket and get account ID in parallel calls
PREREQ_BUCKET=$(aws cloudformation describe-stacks --stack-name tutorial-prereqs-bucket \
--query 'Stacks[0].Outputs[?OutputKey==`BucketName`].OutputValue' --output text 2>/dev/null) || PREREQ_BUCKET=""
if [ -n "$PREREQ_BUCKET" ] && [ "$PREREQ_BUCKET" != "None" ]; then
BUCKET_NAME="$PREREQ_BUCKET"
BUCKET_IS_SHARED=true
echo "Using shared bucket: $BUCKET_NAME"
else
BUCKET_IS_SHARED=false
BUCKET_NAME="cloudfront-${RANDOM_ID}"
fi
echo "Using bucket name: $BUCKET_NAME"
# Get AWS account ID early
ACCOUNT_ID=$(aws sts get-caller-identity --query 'Account' --output text)
if [ $? -ne 0 ]; then
handle_error "Failed to get AWS account ID"
fi
# Validate account ID format
if ! [[ "$ACCOUNT_ID" =~ ^[0-9]{12}$ ]]; then
handle_error "Invalid AWS account ID format: $ACCOUNT_ID"
fi
# Create a temporary directory for content with restrictive permissions
TEMP_DIR="temp_content"
mkdir -p "$TEMP_DIR/css"
chmod 700 "$TEMP_DIR"
if [ $? -ne 0 ]; then
handle_error "Failed to create temporary directory"
fi
# Step 1: Create an S3 bucket (only if not shared)
if [ "$BUCKET_IS_SHARED" != "true" ]; then
echo "Creating S3 bucket: $BUCKET_NAME"
aws s3 mb "s3://$BUCKET_NAME" --region us-east-1
if [ $? -ne 0 ]; then
handle_error "Failed to create S3 bucket"
fi
aws s3api put-bucket-tagging --bucket "$BUCKET_NAME" --tagging 'TagSet=[{Key=project,Value=doc-smith},{Key=tutorial,Value=cloudfront-gettingstarted}]'
# Batch bucket configuration calls for efficiency
aws s3api put-bucket-versioning --bucket "$BUCKET_NAME" --versioning-configuration Status=Enabled &
aws s3api put-public-access-block \
--bucket "$BUCKET_NAME" \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true" &
aws s3api put-bucket-encryption \
--bucket "$BUCKET_NAME" \
--server-side-encryption-configuration '{
"Rules": [
{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
}
}
]
}' &
wait
fi
# Step 2: Create sample content
echo "Creating sample content..."
cat > "$TEMP_DIR/index.html" << 'EOF'
<!DOCTYPE html>
<html>
<head>
<title>Hello World</title>
<link rel="stylesheet" type="text/css" href="css/styles.css">
</head>
<body>
<h1>Hello world!</h1>
</body>
</html>
EOF
cat > "$TEMP_DIR/css/styles.css" << 'EOF'
body {
font-family: Arial, sans-serif;
margin: 40px;
background-color: #f5f5f5;
}
h1 {
color: #333;
text-align: center;
}
EOF
# Set restrictive permissions on content files
chmod 600 "$TEMP_DIR/index.html" "$TEMP_DIR/css/styles.css"
# Step 3: Upload content to the S3 bucket with encryption and metadata
echo "Uploading content to S3 bucket..."
aws s3 cp "$TEMP_DIR/" "s3://$BUCKET_NAME/" --recursive \
--sse AES256 \
--metadata "Source=CloudFrontTutorial"
if [ $? -ne 0 ]; then
handle_error "Failed to upload content to S3 bucket"
fi
# Step 4: Create Origin Access Control
echo "Creating Origin Access Control..."
OAC_RESPONSE=$(aws cloudfront create-origin-access-control \
--origin-access-control-config Name="oac-for-$BUCKET_NAME",SigningProtocol=sigv4,SigningBehavior=always,OriginAccessControlOriginType=s3)
if [ $? -ne 0 ]; then
handle_error "Failed to create Origin Access Control"
fi
OAC_ID=$(echo "$OAC_RESPONSE" | jq -r '.OriginAccessControl.Id')
if [ -z "$OAC_ID" ] || [ "$OAC_ID" = "null" ]; then
handle_error "Failed to extract OAC ID from response"
fi
# Validate OAC ID format (alphanumeric and hyphens)
if ! [[ "$OAC_ID" =~ ^[A-Z0-9]+$ ]]; then
handle_error "Invalid OAC ID format: $OAC_ID"
fi
echo "Created Origin Access Control with ID: $OAC_ID"
# Step 5: Create CloudFront distribution
echo "Creating CloudFront distribution..."
# Validate bucket name format
if ! [[ "$BUCKET_NAME" =~ ^[a-z0-9][a-z0-9.-]*[a-z0-9]$ ]]; then
handle_error "Invalid S3 bucket name format: $BUCKET_NAME"
fi
# Create distribution configuration with improved security settings
cat > distribution-config.json << EOF
{
"CallerReference": "cli-tutorial-$(date +%s)",
"Origins": {
"Quantity": 1,
"Items": [
{
"Id": "S3-$BUCKET_NAME",
"DomainName": "$BUCKET_NAME.s3.amazonaws.com",
"S3OriginConfig": {
"OriginAccessIdentity": ""
},
"OriginAccessControlId": "$OAC_ID"
}
]
},
"DefaultCacheBehavior": {
"TargetOriginId": "S3-$BUCKET_NAME",
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"],
"CachedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"]
}
},
"DefaultTTL": 86400,
"MinTTL": 0,
"MaxTTL": 31536000,
"Compress": true,
"ForwardedValues": {
"QueryString": false,
"Cookies": {
"Forward": "none"
}
}
},
"Comment": "CloudFront distribution for tutorial",
"Enabled": true,
"WebACLId": "",
"HttpVersion": "http2and3"
}
EOF
# Set restrictive permissions on config file before passing credentials
chmod 600 distribution-config.json
DIST_RESPONSE=$(aws cloudfront create-distribution --distribution-config file://distribution-config.json)
if [ $? -ne 0 ]; then
handle_error "Failed to create CloudFront distribution"
fi
DISTRIBUTION_ID=$(echo "$DIST_RESPONSE" | jq -r '.Distribution.Id')
DOMAIN_NAME=$(echo "$DIST_RESPONSE" | jq -r '.Distribution.DomainName')
if [ -z "$DISTRIBUTION_ID" ] || [ "$DISTRIBUTION_ID" = "null" ] || [ -z "$DOMAIN_NAME" ] || [ "$DOMAIN_NAME" = "null" ]; then
handle_error "Failed to extract distribution ID or domain name from response"
fi
# Validate distribution ID format
if ! [[ "$DISTRIBUTION_ID" =~ ^[A-Z0-9]+$ ]]; then
handle_error "Invalid distribution ID format: $DISTRIBUTION_ID"
fi
echo "Created CloudFront distribution with ID: $DISTRIBUTION_ID"
echo "CloudFront domain name: $DOMAIN_NAME"
# Tag the CloudFront distribution
aws cloudfront tag-resource --resource "arn:aws:cloudfront::$ACCOUNT_ID:distribution/$DISTRIBUTION_ID" --tags 'Items=[{Key=project,Value=doc-smith},{Key=tutorial,Value=cloudfront-gettingstarted}]'
# Step 6: Update S3 bucket policy
echo "Updating S3 bucket policy..."
cat > bucket-policy.json << EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipal",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::$BUCKET_NAME/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::$ACCOUNT_ID:distribution/$DISTRIBUTION_ID"
}
}
}
]
}
EOF
# Set restrictive permissions on policy file
chmod 600 bucket-policy.json
aws s3api put-bucket-policy --bucket "$BUCKET_NAME" --policy file://bucket-policy.json
if [ $? -ne 0 ]; then
handle_error "Failed to update S3 bucket policy"
fi
# Step 7: Wait for distribution to deploy
echo "Waiting for CloudFront distribution to deploy (this may take 5-10 minutes)..."
aws cloudfront wait distribution-deployed --id "$DISTRIBUTION_ID" 2>/dev/null || {
echo "Warning: Distribution deployment wait timed out. The distribution may still be deploying."
}
echo "CloudFront distribution is now deployed."
# Step 8: Display access information
echo ""
echo "===== CloudFront Distribution Setup Complete ====="
echo "You can access your content at: https://$DOMAIN_NAME/index.html"
echo ""
echo "Resources created:"
echo "- S3 Bucket: $BUCKET_NAME"
echo "- CloudFront Origin Access Control: $OAC_ID"
echo "- CloudFront Distribution: $DISTRIBUTION_ID"
echo ""
echo "To clean up resources, run: cleanup"
echo ""
echo "Tutorial completed at $(date)"