Host a Secure Static Website on AWS with Amazon S3 and CloudFront
Learn how to host a static website on AWS using Amazon S3 and CloudFront. This beginner-friendly guide shows how to keep your bucket private with Origin Access Control, serve your site over HTTPS, and update it safely with cache invalidation.
Host a Secure Static Website on AWS with Amazon S3 and CloudFront
Put your portfolio or project page online with HTTPS, global caching, and a bucket that stays private.
Introduction
Every student or developer eventually needs to put a website online: a portfolio, a project demo, or a documentation page. On AWS, the classic approach is Amazon S3 for storage and Amazon CloudFront as the content delivery network (CDN).
Many older tutorials make the S3 bucket public. That works, but it’s not the recommended pattern. In this guide we keep the bucket private and let only CloudFront read from it, using Origin Access Control (OAC). You get HTTPS, faster load times, and better security with very little extra work.
Architecture
- Amazon S3 stores your HTML, CSS, JavaScript and images.
- Amazon CloudFront caches and delivers the files from edge locations close to your visitors, over HTTPS.
- Origin Access Control ensures that only your CloudFront distribution can read the bucket.
Visitors never talk to S3 directly, only to CloudFront.
Step 1: Create a private S3 bucket
- Open the S3 console and choose Create bucket.
- Choose a globally unique name, for example
my-portfolio-site-12345.
- Leave Block all public access turned on.
- Create the bucket.
Step 2: Upload your site
Create a simple
index.html on your computer:html
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>My First AWS Website</title></head><body> <h1>Hello from Amazon S3 and CloudFront!</h1> <p>This page is served over HTTPS from a private bucket.</p></body></html>Upload it to the root of your bucket, along with any other files your site needs.
Step 3: Create the CloudFront distribution
- Open the CloudFront console and choose Create distribution.
- For the origin, select your S3 bucket.
- Under origin access, choose Origin access control (OAC) and create a new OAC with the default settings.
- Set Viewer protocol policy to Redirect HTTP to HTTPS.
- Set the Default root object to
index.html.
- Create the distribution.
The console will show a bucket policy to copy. The next step applies it.
Step 4: Allow CloudFront to read the bucket
Go to your bucket’s Permissions → Bucket policy and paste the policy CloudFront provided. It looks like this:
json
{ "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" } } } ]}The
Condition is the key part: it says only this specific distribution can read your files. Even though the bucket is private, CloudFront can still fetch from it.Step 5: Test your website
Wait until the distribution status shows Enabled (a few minutes), then open the Distribution domain name, which looks like
dxxxxxxxxxxxx.cloudfront.net , in your browser. You should see your page over HTTPS.Next, try opening your S3 bucket’s object URL directly. You should get Access Denied, which proves the bucket is private and only CloudFront can serve your content.
Step 6: Update your site
CloudFront caches files, so after you upload a new version you may still see the old one. To force a refresh:
- Open your distribution and go to the Invalidations tab.
- Create an invalidation with the path
/*.
Within a minute or two, visitors get the latest files. For bigger sites, using versioned file names (like
app.v2.js ) avoids frequent invalidations.What I learned
- Private by default is the right habit. Making a bucket public is easy, but OAC gives the same result more safely.
- Caching is a feature and a gotcha. If your changes don’t appear, it’s almost always the cache.
- The policy condition matters. Scoping access to one distribution ARN is a small detail that prevents other distributions from reading your bucket.
Ideas to extend the project
- Add a custom domain with Amazon Route 53 and a free TLS certificate from AWS Certificate Manager (for CloudFront, the certificate must be requested in the US East (N. Virginia) Region)
- Automate deployments with GitHub Actions that sync files to S3 and invalidate the cache
- Add AWS WAF to protect against common web attacks
- Use a CloudFront Function to add security headers or handle redirects
- Define everything as code with AWS CDK or AWS SAM
Cleanup
To avoid charges, disable and then delete the CloudFront distribution, empty the S3 bucket, and delete the bucket. A small static site usually costs very little, but a billing alert in AWS Budgets is still a good habit.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article