Hands-on: Host a Website on S3 and CloudFront
A static website (HTML, CSS, JavaScript, images - for example a built React or Angular app) needs no server at all. S3 stores the files and CloudFront delivers them fast and securely worldwide.
Many websites - portfolios, landing pages, documentation, built React or Angular apps - are just files. They don't need a server running 24 hours a day. Storing them in S3 and serving them through CloudFront is cheap, very fast for users anywhere, handles huge traffic, and gives you free HTTPS. It's one of the best first real projects on AWS.
Steps
- S3: create a bucket (e.g.
my-portfolio-site) in a nearby Region such as Mumbai (ap-south-1). Keep Block all public access on. - Upload your site:
index.htmland the other files. - CloudFront: create a distribution with the bucket as the origin and choose Origin access control (OAC). Let CloudFront update the bucket policy so only CloudFront can read the files.
- Set the default root object to
index.html, and redirect HTTP to HTTPS. - For a single-page app, add custom error responses: 403 and 404 →
/index.htmlwith code 200, so deep links work. - Your own domain: request a free certificate in AWS Certificate Manager (it must be in us-east-1 for CloudFront), add the domain to the distribution, and point the domain to CloudFront with a Route 53 alias record.
Upload updates with the CLI
Example
aws s3 sync ./dist s3://my-portfolio-site --delete
aws cloudfront create-invalidation --distribution-id E123EXAMPLE --paths "/*"sync uploads only changed files; the invalidation tells CloudFront to fetch fresh copies instead of serving its cached ones.
Keeping the bucket private and serving only through CloudFront is safer than the old "public S3 website" method, and gives you HTTPS.
Key points
- S3 stores the files; CloudFront serves them fast with HTTPS.
- Keep the bucket private and allow access only from CloudFront.
- aws s3 sync uploads changes; invalidate CloudFront to refresh.
📝 Test yourself
1. With OAC, who can read files in the private bucket?
2. In which Region must the CloudFront certificate be requested?
3. Fill in the command that uploads only changed files:
aws s3 ./dist s3://my-site
