-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquickpage.sh
More file actions
executable file
·701 lines (625 loc) · 25.6 KB
/
quickpage.sh
File metadata and controls
executable file
·701 lines (625 loc) · 25.6 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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
#!/bin/bash
# quickpage.sh - Secure S3/CloudFront/ACM deployment script
# Usage: ./quickpage.sh yourdomain.com [options]
set -e
# Parse command line arguments
DOMAIN=""
WEBSITE_DIR="./website"
SKIP_CERT=false
SKIP_CERT_VALIDATION=false
SKIP_S3=false
SKIP_UPLOAD=false
SKIP_CLOUDFRONT=false
SKIP_CUSTOM_DOMAIN=false
REGION="us-east-1"
BUCKET_REGION="eu-central-1"
CERT_ARN=""
DEBUG=false
function show_usage {
echo "Usage: ./quickpage.sh [options] yourdomain.com"
echo ""
echo "Options:"
echo " --website-dir DIR Directory containing website files (default: ./website)"
echo " --skip-cert Skip certificate creation"
echo " --skip-cert-validation Skip waiting for certificate validation"
echo " --skip-custom-domain Create CloudFront without custom domain (use if cert not validated)"
echo " --cert-arn ARN Use existing certificate ARN"
echo " --skip-s3 Skip S3 bucket creation"
echo " --skip-upload Skip uploading files to S3"
echo " --skip-cloudfront Skip CloudFront distribution creation"
echo " --region REGION Region for ACM and CloudFront (default: us-east-1)"
echo " --bucket-region REGION Region for S3 bucket (default: eu-central-1)"
echo " --debug Run debug script to analyze AWS resources"
echo " --help Show this help message"
exit 1
}
# Parse arguments
while [[ $# -gt 0 ]]; do
case $1 in
--website-dir)
WEBSITE_DIR="$2"
shift 2
;;
--skip-cert)
SKIP_CERT=true
shift
;;
--skip-cert-validation)
SKIP_CERT_VALIDATION=true
shift
;;
--skip-custom-domain)
SKIP_CUSTOM_DOMAIN=true
shift
;;
--cert-arn)
CERT_ARN="$2"
SKIP_CERT=true
shift 2
;;
--skip-s3)
SKIP_S3=true
shift
;;
--skip-upload)
SKIP_UPLOAD=true
shift
;;
--skip-cloudfront)
SKIP_CLOUDFRONT=true
shift
;;
--region)
REGION="$2"
shift 2
;;
--bucket-region)
BUCKET_REGION="$2"
shift 2
;;
--debug)
DEBUG=true
shift
;;
--help)
show_usage
;;
*)
if [[ -z $DOMAIN ]]; then
DOMAIN="$1"
shift
else
echo "ERROR: Unknown option $1"
show_usage
fi
;;
esac
done
# Check dependencies
function check_dependencies {
echo "🔍 Checking dependencies..."
# Check AWS CLI
if ! command -v aws &> /dev/null; then
echo "❌ AWS CLI is not installed. Please install it to use this script."
exit 1
fi
# Check jq
if ! command -v jq &> /dev/null; then
echo "❌ jq is not installed. Please install it to use this script."
exit 1
fi
echo "✅ All dependencies are installed."
}
# Run debug script if --debug flag is provided
if [[ $DEBUG == true ]]; then
if [[ -f ./scripts/debug_aws_resources.sh ]]; then
./scripts/debug_aws_resources.sh
else
echo "❌ Debug script not found at ./scripts/debug_aws_resources.sh"
fi
exit 0
fi
# Check if domain is provided
if [[ -z $DOMAIN ]]; then
echo "ERROR: Domain name is required"
show_usage
fi
# Check dependencies
check_dependencies
echo "========================================================"
echo "🌍 QuickPage: Deploying $DOMAIN"
echo "========================================================"
# Step 1: Create or use existing SSL Certificate
if [[ $SKIP_CERT == false ]]; then
echo "🔒 Step 1/4: Checking for existing certificates for $DOMAIN..."
# Check if there's an existing certificate for this domain
EXISTING_CERT_ARN=$(aws acm list-certificates --region $REGION \
--query "CertificateSummaryList[?DomainName=='$DOMAIN' || DomainName=='*.$DOMAIN'].CertificateArn" \
--output text)
if [[ -n $EXISTING_CERT_ARN ]]; then
# Check if the certificate is valid and issued
CERT_STATUS=$(aws acm describe-certificate --certificate-arn "$EXISTING_CERT_ARN" \
--region $REGION --query "Certificate.Status" --output text)
if [[ $CERT_STATUS == "ISSUED" ]]; then
echo "✅ Found existing valid certificate for $DOMAIN"
echo "Certificate ARN: $EXISTING_CERT_ARN"
CERT_ARN=$EXISTING_CERT_ARN
elif [[ $CERT_STATUS == "PENDING_VALIDATION" ]]; then
echo "⏳ Found pending certificate for $DOMAIN that needs validation"
echo "Certificate ARN: $EXISTING_CERT_ARN"
CERT_ARN=$EXISTING_CERT_ARN
# Get validation info for the pending certificate
VALIDATION_INFO=$(aws acm describe-certificate \
--certificate-arn "$CERT_ARN" \
--region $REGION \
--query "Certificate.DomainValidationOptions[0].ResourceRecord")
VALIDATION_NAME=$(echo $VALIDATION_INFO | jq -r '.Name')
VALIDATION_VALUE=$(echo $VALIDATION_INFO | jq -r '.Value')
echo "📝 DNS Validation Required - Create this CNAME record:"
echo "Name: $VALIDATION_NAME"
echo "Value: $VALIDATION_VALUE"
echo ""
echo "⚠️ ADD THIS TO YOUR DNS PROVIDER BEFORE CONTINUING"
echo ""
if [[ $SKIP_CERT_VALIDATION == true ]]; then
echo "⚠️ Skipping validation wait as requested. CloudFront distribution will be created but won't work until certificate is validated."
echo "ℹ️ You can check certificate status later with: aws acm describe-certificate --certificate-arn $CERT_ARN --region $REGION --query Certificate.Status"
else
read -p "Press Enter once you've added the CNAME record (or type 'skip' to proceed without waiting for validation)... " RESPONSE
if [[ "${RESPONSE,,}" == "skip" ]]; then
echo "⚠️ Skipping validation wait. CloudFront distribution will be created but won't work until certificate is validated."
echo "ℹ️ You can check certificate status later with: aws acm describe-certificate --certificate-arn $CERT_ARN --region $REGION --query Certificate.Status"
else
echo "⏳ Waiting for certificate validation (this may take several minutes)..."
aws acm wait certificate-validated --certificate-arn "$CERT_ARN" --region $REGION
echo "✅ Certificate validated successfully!"
fi
fi
else
echo "⚠️ Found existing certificate for $DOMAIN but status is: $CERT_STATUS"
echo "Creating a new certificate instead..."
EXISTING_CERT_ARN=""
fi
fi
if [[ -z $EXISTING_CERT_ARN ]]; then
echo "🔒 Creating new SSL Certificate in ACM..."
CERT_ARN=$(aws acm request-certificate \
--domain-name $DOMAIN \
--validation-method DNS \
--subject-alternative-names "*.$DOMAIN" \
--region $REGION \
--query CertificateArn --output text)
echo "✅ Certificate requested: $CERT_ARN"
echo ""
echo "📝 DNS Validation Required - Create this CNAME record:"
# Wait briefly for the certificate to be created and validation info available
sleep 5
VALIDATION_INFO=$(aws acm describe-certificate \
--certificate-arn "$CERT_ARN" \
--region $REGION \
--query "Certificate.DomainValidationOptions[0].ResourceRecord")
VALIDATION_NAME=$(echo $VALIDATION_INFO | jq -r '.Name')
VALIDATION_VALUE=$(echo $VALIDATION_INFO | jq -r '.Value')
echo "Name: $VALIDATION_NAME"
echo "Value: $VALIDATION_VALUE"
echo ""
echo "⚠️ ADD THIS TO YOUR DNS PROVIDER BEFORE CONTINUING"
echo ""
if [[ $SKIP_CERT_VALIDATION == true ]]; then
echo "⚠️ Skipping validation wait as requested."
else
read -p "Press Enter once you've added the CNAME record (or type 'skip' to proceed without waiting for validation)... " RESPONSE
if [[ "${RESPONSE,,}" == "skip" ]]; then
echo "⚠️ Skipping validation wait. CloudFront distribution will be created but won't work until certificate is validated."
else
echo "⏳ Waiting for certificate validation (this may take several minutes)..."
aws acm wait certificate-validated --certificate-arn "$CERT_ARN" --region $REGION
echo "✅ Certificate validated successfully!"
fi
fi
fi
elif [[ -z $CERT_ARN ]]; then
echo "❓ No certificate ARN provided. Checking for existing certificates..."
# Check if there's an existing certificate for this domain
EXISTING_CERT_ARN=$(aws acm list-certificates --region $REGION \
--query "CertificateSummaryList[?DomainName=='$DOMAIN' || DomainName=='*.$DOMAIN'].CertificateArn" \
--output text)
if [[ -n $EXISTING_CERT_ARN ]]; then
# Check if the certificate is valid and issued
CERT_STATUS=$(aws acm describe-certificate --certificate-arn "$EXISTING_CERT_ARN" \
--region $REGION --query "Certificate.Status" --output text)
if [[ $CERT_STATUS == "ISSUED" ]]; then
echo "✅ Found existing valid certificate for $DOMAIN"
echo "Certificate ARN: $EXISTING_CERT_ARN"
CERT_ARN=$EXISTING_CERT_ARN
elif [[ $CERT_STATUS == "PENDING_VALIDATION" ]]; then
echo "⏳ Found pending certificate for $DOMAIN that needs validation"
echo "Certificate ARN: $EXISTING_CERT_ARN"
CERT_ARN=$EXISTING_CERT_ARN
# Get validation info for the pending certificate
VALIDATION_INFO=$(aws acm describe-certificate \
--certificate-arn "$CERT_ARN" \
--region $REGION \
--query "Certificate.DomainValidationOptions[0].ResourceRecord")
VALIDATION_NAME=$(echo $VALIDATION_INFO | jq -r '.Name')
VALIDATION_VALUE=$(echo $VALIDATION_INFO | jq -r '.Value')
echo "📝 DNS Validation Required - Create this CNAME record:"
echo "Name: $VALIDATION_NAME"
echo "Value: $VALIDATION_VALUE"
echo ""
echo "⚠️ ADD THIS TO YOUR DNS PROVIDER BEFORE CONTINUING"
echo ""
if [[ $SKIP_CERT_VALIDATION == true ]]; then
echo "⚠️ Skipping validation wait as requested."
else
read -p "Press Enter once you've added the CNAME record (or type 'skip' to proceed without waiting for validation)... " RESPONSE
if [[ "${RESPONSE,,}" == "skip" ]]; then
echo "⚠️ Skipping validation wait. CloudFront distribution will be created but won't work until certificate is validated."
else
echo "⏳ Waiting for certificate validation (this may take several minutes)..."
aws acm wait certificate-validated --certificate-arn "$CERT_ARN" --region $REGION
echo "✅ Certificate validated successfully!"
fi
fi
else
echo "⚠️ Found existing certificate for $DOMAIN but status is: $CERT_STATUS"
echo "Please provide a valid certificate with --cert-arn or remove --skip-cert"
exit 1
fi
else
echo "❓ No existing certificates found for $DOMAIN"
echo "Please provide a certificate ARN with --cert-arn or remove --skip-cert"
exit 1
fi
else
echo "🔄 Using provided certificate: $CERT_ARN"
fi
# Step 2: Create S3 Bucket
if [[ $SKIP_S3 == false ]]; then
echo ""
echo "🪣 Step 2/4: Creating S3 Bucket..."
# Check if bucket exists
if aws s3api head-bucket --bucket $DOMAIN 2>/dev/null; then
echo "⚠️ Bucket already exists, skipping creation"
else
if [[ $BUCKET_REGION == "us-east-1" ]]; then
aws s3api create-bucket \
--bucket $DOMAIN \
--region $BUCKET_REGION
else
aws s3api create-bucket \
--bucket $DOMAIN \
--region $BUCKET_REGION \
--create-bucket-configuration LocationConstraint=$BUCKET_REGION
fi
echo "✅ S3 bucket created: $DOMAIN"
fi
# Block public access (recommended for security)
aws s3api put-public-access-block \
--bucket $DOMAIN \
--public-access-block-configuration "BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=true,RestrictPublicBuckets=true"
echo "✅ Public access blocked for security"
# Enable server-side encryption
aws s3api put-bucket-encryption \
--bucket $DOMAIN \
--server-side-encryption-configuration '{
"Rules": [
{
"ApplyServerSideEncryptionByDefault": {
"SSEAlgorithm": "AES256"
},
"BucketKeyEnabled": true
}
]
}'
echo "✅ Default encryption enabled"
# Configure bucket for website hosting
aws s3 website s3://$DOMAIN --index-document index.html --error-document error.html
echo "✅ Configured as a static website"
# Set up CORS
aws s3api put-bucket-cors \
--bucket $DOMAIN \
--cors-configuration '{
"CORSRules": [
{
"AllowedHeaders": ["Authorization", "Content-Length"],
"AllowedMethods": ["GET"],
"AllowedOrigins": ["https://'$DOMAIN'", "https://*.'$DOMAIN'"],
"ExposeHeaders": [],
"MaxAgeSeconds": 3000
}
]
}'
echo "✅ CORS configuration set"
# Create Origin Access Control for CloudFront
echo "🔑 Creating CloudFront Origin Access Control..."
OAC_NAME="OAC-$DOMAIN-$(date +%s)"
# Check if OAC already exists
EXISTING_OAC=$(aws cloudfront list-origin-access-controls --query "OriginAccessControlList.Items[?Name=='$OAC_NAME'].Id" --output text)
if [[ -z $EXISTING_OAC ]]; then
OAC_CONFIG='{
"OriginAccessControlConfig": {
"Name": "'$OAC_NAME'",
"OriginAccessControlOriginType": "s3",
"SigningBehavior": "always",
"SigningProtocol": "sigv4"
}
}'
echo "$OAC_CONFIG" > /tmp/oac_config.json
OAC_RESULT=$(aws cloudfront create-origin-access-control --cli-input-json file:///tmp/oac_config.json)
OAC_ID=$(echo "$OAC_RESULT" | jq -r '.OriginAccessControl.Id')
echo "✅ Created OAC: $OAC_ID"
else
OAC_ID=$EXISTING_OAC
echo "⚠️ Using existing OAC: $OAC_ID"
fi
# Set bucket policy for CloudFront OAC
BUCKET_POLICY='{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudFrontServicePrincipalReadOnly",
"Effect": "Allow",
"Principal": {
"Service": "cloudfront.amazonaws.com"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::'$DOMAIN'/*",
"Condition": {
"StringEquals": {
"AWS:SourceArn": "arn:aws:cloudfront::'$(aws sts get-caller-identity --query Account --output text)':distribution/*"
}
}
}
]
}'
echo "$BUCKET_POLICY" > /tmp/bucket_policy.json
aws s3api put-bucket-policy --bucket $DOMAIN --policy file:///tmp/bucket_policy.json
echo "✅ Bucket policy set for CloudFront access only (private bucket)"
else
echo "🔄 Skipping S3 bucket creation"
fi
# Step 3: Upload website files
if [[ $SKIP_UPLOAD == false ]]; then
echo ""
echo "📤 Step 3/4: Uploading website files..."
if [[ ! -d $WEBSITE_DIR ]]; then
echo "⚠️ Website directory not found: $WEBSITE_DIR"
mkdir -p $WEBSITE_DIR
echo "<html><body><h1>$DOMAIN</h1><p>Placeholder page created by QuickPage script</p></body></html>" > $WEBSITE_DIR/index.html
echo "<html><body><h1>Error - Page Not Found</h1><p>Default error page created by QuickPage script</p></body></html>" > $WEBSITE_DIR/error.html
echo "🔄 Created basic placeholder files in $WEBSITE_DIR"
fi
# Set appropriate content types and caching headers
echo "📝 Setting optimal file properties..."
# Upload with proper content types and caching
find $WEBSITE_DIR -type f -name "*.html" | while read file; do
aws s3 cp "$file" "s3://$DOMAIN/${file#$WEBSITE_DIR/}" --content-type "text/html" --cache-control "max-age=3600"
done
find $WEBSITE_DIR -type f -name "*.css" | while read file; do
aws s3 cp "$file" "s3://$DOMAIN/${file#$WEBSITE_DIR/}" --content-type "text/css" --cache-control "max-age=86400"
done
find $WEBSITE_DIR -type f -name "*.js" | while read file; do
aws s3 cp "$file" "s3://$DOMAIN/${file#$WEBSITE_DIR/}" --content-type "application/javascript" --cache-control "max-age=86400"
done
find $WEBSITE_DIR -type f \( -name "*.jpg" -o -name "*.jpeg" \) | while read file; do
aws s3 cp "$file" "s3://$DOMAIN/${file#$WEBSITE_DIR/}" --content-type "image/jpeg" --cache-control "max-age=2592000"
done
find $WEBSITE_DIR -type f -name "*.png" | while read file; do
aws s3 cp "$file" "s3://$DOMAIN/${file#$WEBSITE_DIR/}" --content-type "image/png" --cache-control "max-age=2592000"
done
find $WEBSITE_DIR -type f -name "*.svg" | while read file; do
aws s3 cp "$file" "s3://$DOMAIN/${file#$WEBSITE_DIR/}" --content-type "image/svg+xml" --cache-control "max-age=2592000"
done
# Upload any remaining files
aws s3 sync $WEBSITE_DIR s3://$DOMAIN --exclude "*.*" --include "*"
echo "✅ Website files uploaded to S3 with optimized settings"
else
echo "🔄 Skipping website file upload"
fi
# Check certificate status before proceeding with CloudFront
if [[ -n $CERT_ARN && $SKIP_CLOUDFRONT == false ]]; then
CERT_STATUS=$(aws acm describe-certificate --certificate-arn "$CERT_ARN" \
--region $REGION --query "Certificate.Status" --output text)
if [[ $CERT_STATUS != "ISSUED" && $SKIP_CUSTOM_DOMAIN != true ]]; then
echo "⚠️ Certificate is not fully validated yet (Status: $CERT_STATUS)"
echo "ℹ️ CloudFront requires validated certificates for custom domains"
echo "Options:"
echo " 1. Wait for certificate validation to complete"
echo " 2. Run with --skip-custom-domain to create CloudFront without custom domain"
echo " 3. Skip CloudFront creation for now with --skip-cloudfront"
read -p "Would you like to continue without custom domain? (y/n): " CONTINUE_RESPONSE
if [[ "${CONTINUE_RESPONSE,,}" == "y" ]]; then
SKIP_CUSTOM_DOMAIN=true
echo "✅ Continuing with CloudFront creation without custom domain"
else
echo "❌ Aborting CloudFront creation. Run again with one of the options above when ready."
SKIP_CLOUDFRONT=true
fi
fi
fi
# Step 4: Create CloudFront Distribution
if [[ $SKIP_CLOUDFRONT == false ]]; then
echo ""
echo "☁️ Step 4/4: Creating CloudFront Distribution..."
# Generate a unique caller reference
CALLER_REF="quickpage-$DOMAIN-$(date +%s)"
# Security headers policy
echo "🔒 Creating security headers policy..."
POLICY_NAME="QuickPageSecurityPolicy-$DOMAIN"
HEADERS_POLICY='{
"SecurityHeadersConfig": {
"XSSProtection": {
"Override": true,
"Protection": true,
"ModeBlock": true
},
"StrictTransportSecurity": {
"Override": true,
"IncludeSubdomains": true,
"Preload": true,
"AccessControlMaxAgeSec": 63072000
},
"ContentTypeOptions": {
"Override": true
},
"ReferrerPolicy": {
"Override": true,
"ReferrerPolicy": "strict-origin-when-cross-origin"
},
"ContentSecurityPolicy": {
"Override": true,
"ContentSecurityPolicy": "default-src ''self''; img-src ''self'' data:; script-src ''self''; style-src ''self'' ''unsafe-inline''; font-src ''self''; object-src ''none''; connect-src ''self''"
},
"FrameOptions": {
"Override": true,
"FrameOption": "DENY"
}
}
}'
# Check if policy already exists
EXISTING_POLICY_ID=$(aws cloudfront list-response-headers-policies --query "ResponseHeadersPolicyList.Items[?Name=='$POLICY_NAME'].Id" --output text)
if [[ -z $EXISTING_POLICY_ID ]]; then
echo "$HEADERS_POLICY" > /tmp/headers_policy.json
POLICY_CONFIG="{\"Name\": \"$POLICY_NAME\", $(cat /tmp/headers_policy.json)}"
echo "$POLICY_CONFIG" > /tmp/policy_config.json
POLICY_RESULT=$(aws cloudfront create-response-headers-policy --cli-input-json file:///tmp/policy_config.json)
POLICY_ID=$(echo "$POLICY_RESULT" | jq -r '.ResponseHeadersPolicy.Id')
echo "✅ Created security headers policy: $POLICY_ID"
else
POLICY_ID=$EXISTING_POLICY_ID
echo "⚠️ Using existing security headers policy: $POLICY_ID"
fi
# Create Origin configuration using Origin Access Control
ORIGIN_CONFIG='{
"Id": "S3Origin",
"DomainName": "'$DOMAIN'.s3.'$BUCKET_REGION'.amazonaws.com",
"OriginAccessControlId": "'$OAC_ID'",
"OriginPath": "",
"ConnectionAttempts": 3,
"ConnectionTimeout": 10,
"CustomHeaders": {
"Quantity": 0
},
"OriginShield": {
"Enabled": false
}
}'
# Create distribution configuration
if [[ $SKIP_CUSTOM_DOMAIN == true ]]; then
# Create CloudFront without custom domain
cat > /tmp/cf-config.json << EOF
{
"CallerReference": "$CALLER_REF",
"Aliases": {
"Quantity": 0
},
"DefaultRootObject": "index.html",
"Origins": {
"Quantity": 1,
"Items": [$ORIGIN_CONFIG]
},
"OriginGroups": {
"Quantity": 0
},
"DefaultCacheBehavior": {
"TargetOriginId": "S3Origin",
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"],
"CachedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"]
}
},
"ResponseHeadersPolicyId": "$POLICY_ID",
"SmoothStreaming": false,
"Compress": true,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e88639e58f6",
"OriginRequestPolicyId": "88a5eaf4-2fd4-4709-b370-b4c650ea3fcf"
},
"CacheBehaviors": {
"Quantity": 0
},
"CustomErrorResponses": {
"Quantity": 1,
"Items": [
{
"ErrorCode": 404,
"ResponsePagePath": "/error.html",
"ResponseCode": "404",
"ErrorCachingMinTTL": 300
}
]
},
"Comment": "CloudFront distribution for $DOMAIN (without custom domain)",
"Logging": {
"Enabled": false,
"IncludeCookies": false,
"Bucket": "",
"Prefix": ""
},
"PriceClass": "PriceClass_100",
"Enabled": true,
"ViewerCertificate": {
"CloudFrontDefaultCertificate": true,
"MinimumProtocolVersion": "TLSv1.2_2021"
},
"Restrictions": {
"GeoRestriction": {
"RestrictionType": "none",
"Quantity": 0
}
},
"WebACLId": "",
"HttpVersion": "http2",
"IsIPV6Enabled": true
}
EOF
else
# Create CloudFront with custom domain
cat > /tmp/cf-config.json << EOF
{
"CallerReference": "$CALLER_REF",
"Aliases": {
"Quantity": 1,
"Items": ["$DOMAIN"]
},
"DefaultRootObject": "index.html",
"Origins": {
"Quantity": 1,
"Items": [$ORIGIN_CONFIG]
},
"OriginGroups": {
"Quantity": 0
},
"DefaultCacheBehavior": {
"TargetOriginId": "S3Origin",
"ViewerProtocolPolicy": "redirect-to-https",
"AllowedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"],
"CachedMethods": {
"Quantity": 2,
"Items": ["GET", "HEAD"]
}
},
"ResponseHeadersPolicyId": "$POLICY_ID",
"SmoothStreaming": false,
"Compress": true,
"LambdaFunctionAssociations": {
"Quantity": 0
},
"FunctionAssociations": {
"Quantity": 0
},
"FieldLevelEncryptionId": "",
"CachePolicyId": "658327ea-f89d-4fab-a63d-7e