Ravindra BagaleCourses & study guides

14. Amazon S3: Buckets, Objects, Policies and Presigned URLs

14.4 Hosting a Static Website on S3

HTML, CSS, JS aani images astil (server-side code nahi) tar S3 varun website chalavta yete – server nahi, patching nahi. Ha ekach bucket asa aahe jithe aapan jaanun-bujun public read deto.

BUCKET=ravindra-static-site-nashik
aws s3 mb s3://$BUCKET --region ap-south-1
aws s3 sync ./site s3://$BUCKET/                      # index.html, error.html, css/, img/
aws s3 website s3://$BUCKET/ --index-document index.html --error-document error.html
# allow public policies on THIS bucket only (console: Permissions → Block public access → Edit)
aws s3api put-public-access-block --bucket $BUCKET \
  --public-access-block-configuration BlockPublicAcls=true,IgnorePublicAcls=true,BlockPublicPolicy=false,RestrictPublicBuckets=false

Public read policy – only GetObject, never ListBucket or PutObject:

{
  "Version": "2012-10-17",
  "Statement": [{
    "Sid": "PublicReadWebsite",
    "Effect": "Allow",
    "Principal": "*",
    "Action": "s3:GetObject",
    "Resource": "arn:aws:s3:::ravindra-static-site-nashik/*"
  }]
}

The website endpoint looks like http://ravindra-static-site-nashik.s3-website.ap-south-1.amazonaws.com. Note: the S3 website endpoint is HTTP only. For HTTPS and your own domain, put CloudFront in front of the bucket (with Origin Access Control, so the bucket itself can stay private) and point www.yourdomain.com to CloudFront with a CNAME.

Option HTTPS Bucket public? Use for
S3 website endpoint No Yes (read only) Quick demos, learning
CloudFront + OAC Yes, custom domain No – only CloudFront can read Real production static sites
Nginx on EC2 (Part 3) Yes with Certbot Not applicable When you also need server-side code

Ravindra Bagale's Tip

Students accidentally sync .env, backup.zip and the .git folder into the website bucket too – and they become public! Use excludes, like aws s3 sync ./site s3://bucket/ --exclude ".git/*" --exclude "*.env" --exclude "*.zip". After uploading, always check once with aws s3 ls --recursive what has become public.

Lab

Host a two-page static site (index.html, about.html, error.html) on S3. Open the website endpoint, open a wrong URL to see error.html, then try aws s3 ls s3://<bucket> without credentials (--no-sign-request) and confirm listing is refused while pages still load.