What you’ll build
You will publish a static website, meaning HTML, CSS and image files with no server-side code, from an Amazon S3 bucket. The guide shows two approaches:
- A quick test with the S3 website endpoint, which serves plain HTTP and needs a public bucket.
- The recommended setup, with a private bucket and Amazon CloudFront in front of it. CloudFront adds HTTPS, caching and support for your own domain.
Before you start
- An AWS account and an IAM user with permission to use S3 and CloudFront.
- Your site files. At minimum, an
index.htmlfile at the top level. A simple example:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>My Static Website</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<h1>Welcome</h1>
<p>This page is hosted on Amazon S3.</p>
</body>
</html>
Step 1: Create a bucket
In the S3 console, choose Create bucket and set:
- Bucket name: 3 to 63 characters, lowercase letters, numbers, hyphens and dots. Names are unique across all of AWS, so add something personal to yours.
- Region: one close to most of your visitors.
- Block Public Access: keep it on for the recommended setup. The quick test turns it off later.
New buckets have ACLs disabled by default, so you do not need to change object ownership or set per-object permissions.
Step 2: Upload your files
In the bucket, choose Upload and add your files, with index.html at the top level. You can also upload from the command line:
aws s3 sync ./site s3://<your-bucket-name> --delete
The --delete flag removes files from the bucket that no longer exist in ./site. Use it carefully.
Step 3: Choose a hosting approach
| S3 website endpoint | CloudFront with origin access control | |
|---|---|---|
| Protocol | HTTP only | HTTPS, with an AWS-issued or custom certificate |
| Bucket | Must be public | Stays private |
| Caching | None | Copies at edge locations |
| Custom domain | Possible, with a bucket name that matches the domain | Yes, with an ACM certificate in us-east-1 |
| Best for | A quick test | Anything real |
Quick test: use the S3 website endpoint
- Open the bucket’s Properties tab, choose Static website hosting, and enable it. Set the index document to
index.html. - On the Permissions tab, turn off Block Public Access for this bucket.
- Add a bucket policy that allows anyone to read objects:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadForWebsite",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<your-bucket-name>/*"
}
]
}
- Open the website endpoint shown in the Properties tab. It uses plain HTTP.
Anyone on the internet can read every object in a public bucket. Use this path only for test files you are happy to publish, then move to the recommended setup.
Recommended: add CloudFront with origin access control
Keep the bucket private, with Block Public Access on. Then let CloudFront read the bucket:
- In the CloudFront console, choose Create distribution.
- For the origin, choose your S3 bucket from the list. Use the bucket’s standard origin, not the website endpoint, because origin access control (OAC) works with the standard endpoint.
- Under origin access, choose Origin access control settings, create a new OAC, and keep the default signing settings.
- Set Default root object to
index.html. - Set the viewer protocol policy to Redirect HTTP to HTTPS.
- Create the distribution. The console offers a bucket policy that allows only this distribution to read the bucket. Copy it into the bucket’s Permissions tab.
The policy looks like this:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowCloudFrontReadOnly",
"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>"
}
}
}
]
}
When the distribution status changes to Deployed, open its domain name (it ends in cloudfront.net). The page should load over HTTPS.
One limit to know: with the standard S3 endpoint, a request for a folder such as /blog/ does not automatically serve blog/index.html. Fix that with a CloudFront Function that rewrites the URL, if your site has subfolders.
Optional: use your own domain
- Register a domain, through Route 53 or another registrar.
- In AWS Certificate Manager, request a public certificate for your domain in us-east-1. CloudFront only uses certificates from that Region. Validate it with DNS. Public certificates from ACM are free.
- On the CloudFront distribution, add your domain under Alternate domain names (CNAMEs) and select the certificate.
- In Route 53, create an alias A record that points to the distribution. If your registrar hosts your DNS, create the matching record there instead. A CNAME works for a subdomain such as
www, but not for the bare domain.
Update the site and clear the cache
After you upload changes, CloudFront may keep serving the old copies until they expire. To refresh them right away, create an invalidation:
aws cloudfront create-invalidation --distribution-id <distribution-id> --paths "/*"
The first 1,000 invalidation paths each month are free. After that, each path costs about $0.005.
Costs
- S3 Standard storage costs about $0.023 per GB-month in us-east-1. A small site uses a fraction of a gigabyte.
- CloudFront includes an always-free allowance of 1 TB of data transfer out and 10 million requests each month. Check the current terms for your account.
- Route 53 charges $0.50 per month for each hosted zone, if you use it.
Troubleshooting
- 403 Access Denied on the CloudFront domain. The bucket policy is missing, or its
AWS:SourceArndoes not match the distribution. Copy the policy again from the CloudFront origin settings. - 403 Access Denied on the S3 website endpoint. Block Public Access is still on, or the bucket policy is missing.
- The old page still appears. CloudFront is serving a cached copy. Create an invalidation.
- A blank page or a missing file. Check that
index.htmlis at the top level of the bucket and that the file names match the links, including upper and lower case.
Clean up
To remove the site, disable the CloudFront distribution, wait for it to finish updating, and then delete it. Empty the bucket and delete it. If you created a Route 53 hosted zone you no longer need, delete it too. Delete the certificate only if nothing else uses it.
Security notes
- Do not put private files in a public bucket.
- Do not upload passwords, API keys or
.envfiles with your site. - The recommended setup keeps the bucket private, so keep it that way.
Next steps
- Add a dynamic feature, such as a form that saves data, with the Serverless Framework and DynamoDB tutorial.
- Read the AWS fundamentals guide to see how S3, CloudFront and Route 53 fit together.
- Check the guide to avoiding unexpected AWS charges before you leave the distribution running.