S3 + CloudFront Static Website Example 🌐#

Host a secure, globally-distributed static website using S3, CloudFront, WAF, and optional custom domain with HTTPS.

Architecture#

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚  S3 Bucket │◀────│  CloudFront   │◀────│  User     β”‚
β”‚  (Origin) β”‚     β”‚  (CDN + WAF) β”‚     β”‚  Browser  β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
                      β”‚
                      β–Ό
              β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
              β”‚  WAF Web ACL β”‚
              β”‚  (Rate Limit β”‚
              β”‚   + Security)β”‚
              β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Features#

  • S3 Origin β€” Private bucket with CloudFront-only access (OAC)
  • CloudFront CDN β€” Global edge network with HTTPS enforcement
  • WAF β€” Rate limiting, managed rules, and bot control
  • Custom Domain β€” Optional Route53 + ACM SSL certificate
  • Versioning β€” S3 versioning for rollback and CI/CD
  • Compression β€” Automatic content compression at edge
  • Error Pages β€” Custom 403/404 error responses

Security Controls#

Control Implementation
Public Access βœ… Blocked via PublicAccessBlock
Origin Access βœ… CloudFront OAC (not OAI)
Encryption βœ… AES256 server-side
WAF Rate Limit βœ… 1000 req/min per IP
Managed Rules βœ… AWS Common Rule Set
Bot Control βœ… AWSManagedRulesBotControl

Usage#

# Initialize
terraform init

# Deploy with CloudFront default domain
terraform apply

# Test
curl -I https://$(terraform output -raw cloudfront_domain)

# Deploy with custom domain
terraform apply \
  -var="domain_name=example.com" \
  -var="hosted_zone_id=Z1234567890"

# Deploy website content
aws s3 sync ./my-website/ s3://$(terraform output -raw s3_bucket_name)/ --delete

# Invalidate cache
aws cloudfront create-invalidation \
  --distribution-id $(terraform output -raw cloudfront_distribution_id) \
  --paths "/*"

# Clean up
terraform destroy

Level#

⭐⭐⭐ Intermediate β€” S3, CloudFront, WAF, Route53

Files#

File Description
main.tf Complete static website with S3, CloudFront, WAF, optional domain/HTTPS