Host a Free Static Website with AWS CloudFront and S3
AWS CloudFront Website Demo:
used as content delivery network (CDN) | Each month Always Free | Free 1 TB of data transfer out (approx 1million pageviews of 1MB pages) Free 10,000,000 HTTP or HTTPS Requests |
used for hosting - web pages (file) storage on cloud cost will be negligible for a small project since we will be caching pages with CloudFront cache to minimize S3 read requests | Each month 12 Months Free | Free 5 GB of Standard Storage Free 20,000 Get Requests Free 2,000 Put Requests |
After 12 months | $0.0004 per 1000 GET Requests $0.023 per GB storage cost | |
used as Domain Name Service (DNS) optional - for custom domain | Paid | Domain Name Registration (~ $12 for 1 year) Hosted Zone ($0.5 per month) |
used to generate SSL certificate - one time | Always Free | Free |
Steps to Host your Website with AWS:
AWS Developer Guide for deploying static website on CloudFront. - AWS Guide
Create a Free AWS account - AWS Console.
You will have to provide a Credit card and phone number to validate your identity.
S3 Configuration:
Go to AWS S3 Service from the console - AWS S3 Console
Create Bucket - provide name and region (us-east-ohio has one of the low pricing $0.0004)
Uncheck - Block Public access checkbox
Enable Bucket versioning
Other options default - disabled encryption and object lock
Create Bucket
Upload Files to the S3 bucket
Upload a .html file having html / css / js code.
Select Standard Storage class - to minimize cost.
Leave other options default / turned off.
If you copy the item URL and hit in the browser, it will give Access Denied since we need to set the bucket policy so that only CloudFront can access the S3 bucket objects.
CloudFront Configuration:
Go to AWS CloudFront from the console - AWS CloudFront
Create a CloudFront distribution
Origin domain for CloudFront will be the S3 bucket we created above.
Origin Access - Create Origin Access Control policy using Create button - Select Sign requests option required to restrict S3 access only to CloudFront.
Caching Policy - Caching Optimized for now (this will cache the S3 items for 1 day in CloudFront cache, after 1 day it will again hit the S3 origin to get fresh copy of the file item)
Leave other default settings - Do not enable Origin shield - it will add to cost as it is not free
Price Class - Select North America and Europe for least cost. Do not select more edge locations as the price will increase based on more edge locations of CDN.
Create Distribution
After few minutes the distribution will be deployed and you can hit the distribution domain name in browser:
Our Demo CloudFront : d366ztwgqbpglu.cloudfront.net
You will get access denied error because we need to update S3 policy to allow access to CloudFront
Copy the ARN and distribution name for your CloudFront
S3 Bucket Access Policy:
Go back to S3 bucket in AWS console
Select the Permissions Tab
Here we need to provide below bucket policy to allow access only to your CloudFront
Update you S3 bucket name and CloudFront ARN in the JSON
Now hit your CloudFront with Page URL
It should work now!
Congrats you are online on Amazon CloudFront!!
Comment below and let us know if you face any issues..
1{
2 "Version": "2012-10-17",
3 "Statement": [
4 {
5 "Sid": "Statement1",
6 "Effect": "Allow",
7 "Principal": {
8 "Service": "cloudfront.amazonaws.com"
9 },
10 "Action": "s3:GetObject",
11 "Resource": "arn:aws:s3:::free-cloud-stacks-bucket/*",
12 "Condition": {
13 "StringEquals": {
14 "AWS:SourceArn": "arn:aws:cloudfront::019971473017:distribution/EW2RIIZHICBF1"
15 }
16 }
17 }
18 ]
19}















Nice info Thanks!
ReplyDelete