Securing a Statically Hosted Website on AWS

Securing a statically hosted website on AWS (like one served through Amazon S3 + CloudFront) involves several layers of security controls. Here's a comprehensive breakdown and guide:

βœ… Common Stack:

πŸ” 1. S3 Bucket Security Controls

βœ… Enable Bucket Policy Restriction

βœ… Use Bucket Policy to Allow Only CloudFront Access

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCloudFrontServicePrincipal",
      "Effect": "Allow",
      "Principal": {
        "Service": "cloudfront.amazonaws.com"
      },
      "Action": "s3:GetObject",
      "Resource": "arn:aws:s3:::your-bucket-name/*",
      "Condition": {
        "StringEquals": {
          "AWS:SourceArn": "arn:aws:cloudfront::ACCOUNT_ID:distribution/DISTRIBUTION_ID"
        }
      }
    }
  ]
}

Or use Origin Access Control (OAC) with CloudFront for secure access.

πŸ”’ 2. HTTPS (TLS) with CloudFront + ACM

🧾 3. HTTP Security Headers

These can’t be set directly in S3, so use CloudFront Function or Lambda@Edge to inject them:

function handler(event) {
  var response = event.response;
  var headers = response.headers;

  headers['strict-transport-security'] = { value: 'max-age=63072000; includeSubDomains; preload' };
  headers['content-security-policy'] = { value: "default-src 'self';" };
  headers['x-content-type-options'] = { value: 'nosniff' };
  headers['x-frame-options'] = { value: 'DENY' };
  headers['x-xss-protection'] = { value: '1; mode=block' };
  headers['referrer-policy'] = { value: 'no-referrer' };

  return response;
}

πŸ” 4. CloudFront Controls

πŸ•΅οΈβ€β™‚οΈ 5. Logging and Monitoring

βš™οΈ 6. Deployment Practices

🧼 7. Cache Invalidation

Secure cache invalidation with:

aws cloudfront create-invalidation --distribution-id DIST_ID --paths "/*"

Summary Checklist βœ…

Control Applied?
S3 public access blockedβœ…
HTTPS enforced via CloudFrontβœ…
ACM TLS cert configuredβœ…
HTTP security headers via Lambda@Edgeβœ…
CloudFront WAF rules (optional)βœ…/πŸ”²
Access logs on S3 and CloudFrontβœ…
CI/CD secure deployment pipelineβœ…/πŸ”²
CloudTrail enabledβœ…

Implementation can vary..