How to host website or static content using S3 bucket as document root.

897    Asked by BenPHILLIPS in AWS , Asked on Feb 10, 2020
Answered by Ankur vaish

It is possible to host static content to a website via s3 bucket.

Below procedure need to be followed to achieve it -

Login to aws console and navigate to s3 console.

Click to the bucket and hit properties section.

Goto option “Static file hosting”.

Click on “Use bucket to host website”

Enter the “index.html” file as the document index.

Also, below command can be executed to achieve same via AWS command line

aws s3 website s3://www.my-awesome-site.com/ --index-document index.html --error-document error.html

Make sure the bucket is accessible to everyone as it hosts the static website.

By default “public access” is NOT provided to any bucket within AWS to achieve this -

Goto bucket

Hit “permissions”

Under “public access” hit “Edit”

Edit “Block new public bucket policies”, “Block public and cross-account access if bucket has public policies”, and “Block new public ACLs and uploading public objects” to be false and Save.

Then goto bucket policy and enter below policy

 {

   "Version": "2008-10-17",

   "Id": "PolicyForPublicWebsiteContent",

   "Statement": [

       {

           "Sid": "PublicReadGetObject",

           "Effect": "Allow",

           "Principal": {

               "AWS": "*"

           },

           "Action": "s3:GetObject",

           "Resource": "arn:aws:s3:::www.my-awesome-site.com/*"

       }

   ]

}

It’s needed to add a CNAME record to your bucket hence, do a mapping to s3 bucket url i.e. domain name and then browse the url, the contents of index.html file will be visible.

NOTE:- Check for bucket permissions if the content is not visible.



Your Answer

Interviews

Parent Categories