Hands-On: Host a Static Site from S3

Serve a private S3 bucket worldwide over HTTPS, the secure way.

1 min read

The recommended setup keeps your bucket private and lets only CloudFront read it, using Origin Access Control (OAC).

Steps (console)

  1. Upload your site (index.html, CSS, JS) to an S3 bucket. Leave Block Public Access on.
  2. In CloudFront, Create distribution, choose the bucket as the origin, and select Origin access control.
  3. Set Default root object to index.html.
  4. Copy the bucket policy CloudFront suggests and apply it to the bucket. It allows only your distribution to read objects.
  5. Wait for the deployment to finish, then open the *.cloudfront.net URL.

The bucket policy CloudFront generates looks like this:

Try it

bucket-policy.json
{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "AllowCloudFrontRead",
    "Effect": "Allow",
    "Principal": { "Service": "cloudfront.amazonaws.com" },
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::notesnap-web/*",
    "Condition": {
      "StringEquals": {
        "AWS:SourceArn": "arn:aws:cloudfront::123456789012:distribution/E2EXAMPLE123"
      }
    }
  }]
}

Deploying an update

Terminal
aws s3 sync ./dist s3://notesnap-web/ --delete
aws cloudfront create-invalidation --distribution-id E2EXAMPLE123 --paths "/index.html"

An invalidation tells edges to drop cached copies before their TTL expires. With hashed filenames you only need to invalidate index.html.

Note

Building a single-page app with client-side routing? Add a custom error response that returns /index.html for 403/404, so deep links like /notes/42 still load your app.

Key takeaways

  • Keep the bucket private and grant read access only to your distribution via OAC.
  • Deploy with `s3 sync`, then invalidate changed entry files like index.html.
  • Single-page apps need an error response that falls back to index.html.

Check your understanding

Pick an answer to see the explanation.

  1. 1. What's the secure way to let CloudFront read your site files from S3?