Week 2 Day 05 Technology Domain Generated by Gemini Code Assist

Deploying Static Websites to AWS

A comprehensive guide to S3, CloudFront, and Route 53 with a focus on Security (HTTPS, OAC, and Security Headers).

Architecture Overview

Amazon S3 Stores HTML, CSS, and JS files securely (Private Access).
AWS CloudFront Global CDN, SSL/TLS termination, and security headers.
AWS Route 53 Manages DNS records for your domain.
AWS ACM Provides the free public SSL certificate.
1

Create the S3 Bucket (Storage)

We will create a bucket that is not public. Only CloudFront will be allowed to read from it.

2

Request SSL Certificate (Security)

Note: CloudFront requires certificates to be in the N. Virginia (us-east-1) region.
3

Create CloudFront Distribution (CDN & Security)

This is the most critical step for security.

Origin Settings:

  • Origin domain: Select your S3 bucket from Step 1.
  • Origin access: Select Origin access control settings (recommended).
  • Click Create control setting (keep defaults) and Create.

Viewer Settings:

  • Viewer protocol policy: Select Redirect HTTP to HTTPS.
  • Allowed HTTP methods: GET, HEAD, OPTIONS.
  • Cache key and origin requests: Select CachingOptimized.

Response headers policy (Security Headers):

Click Create policy. Name it StaticSiteSecurityPolicy.

  • Strict-Transport-Security (HSTS): Enable. max-age: 31536000, includeSubDomains: Yes, preload: Yes.
  • X-Content-Type-Options: Enable (nosniff).
  • X-Frame-Options: Enable (DENY or SAMEORIGIN).
  • X-XSS-Protection: Enable (1; mode=block).
  • Referrer-Policy: Enable (strict-origin-when-cross-origin).
  • Content-Security-Policy (CSP): Enable. Use the value below:
default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.tailwindcss.com; style-src 'self' 'unsafe-inline' https://fonts.googleapis.com; img-src 'self' https: data:; font-src 'self' https://fonts.gstatic.com; connect-src 'self'; object-src 'none'; frame-ancestors 'none'; upgrade-insecure-requests;

Click Create and select this new policy in the dropdown.

  • Settings: Alternate domain name (CNAME): Enter your-domain-name.com.
  • Custom SSL certificate: Select the ACM certificate created in Step 2.
  • Default root object: index.html.
  • Click Create distribution.

Important:

After creation, you will see a banner saying "The S3 bucket policy needs to be updated".

  1. Click Copy policy.
  2. Go to your S3 Bucket > Permissions > Bucket policy.
  3. Paste the policy and Save. This grants CloudFront permission to read your private bucket.
4

Configure Route 53 (DNS)

5

Deploy Your Code

You can use the AWS CLI to sync your local files to the S3 bucket.

  • Install AWS CLI and configure it (aws configure) with your credentials.
  • Run the following command from your project root folder:
# Replace with your actual bucket name
aws s3 sync . s3://your-domain-name.com --delete --exclude ".git/*" --exclude ".vscode/*" --exclude ".DS_Store"
  • Invalidate CloudFront Cache (so users see changes immediately):
# Replace with your Distribution ID (e.g., E1XXXXXX)
aws cloudfront create-invalidation --distribution-id YOUR_DIST_ID --paths "/*"

Summary of Security Measures Implemented

Optional: Deployment Script

You can create a file named deploy.sh in your root folder to automate this:

#!/bin/bash
BUCKET_NAME="your-domain-name.com"
DISTRIBUTION_ID="YOUR_DISTRIBUTION_ID"

echo "🚀 Deploying to S3..."
aws s3 sync . s3://$BUCKET_NAME --delete --exclude ".git/*" --exclude ".vscode/*" --exclude ".DS_Store"

echo "🔄 Invalidating CloudFront Cache..."
aws cloudfront create-invalidation --distribution-id $DISTRIBUTION_ID --paths "/*"

echo "✅ Deployment Complete!"

Make it executable: chmod +x deploy.sh and run ./deploy.sh.