Amazon S3 is a scalable storage service of Amazon. With S3, we can store and retrieve any kind of data from anywhere on the cloud.
Amazon S3 bucket can be used as a server for static websites. We can configure an S3 bucket for static website hosting to store and deliver static web files like HTML, CSS, JavaScript, and images.
Let's learn how to use an S3 bucket as a server.
We'll start by creating an S3 bucket. Replace <your-bucket-name>
with a globally unique name for the bucket in the command given below and then create an S3 bucket using this command given below:
aws s3api create-bucket --bucket <your-bucket-name> --region us-east-1
Note: S3 buckets' names must be globally unique.
Now that we have our bucket let's enable static website hosting for it. Replace <your-bucket-name>
, in the command given below, with the name of the bucket, and then execute this command to enable static website hosting for our bucket:
aws s3 website s3://<your-bucket-name>/ --index-document index.html --error-document error.html
We can view the contents of these files by using the following commands:
cat index.html # To view the contents of index.htmlcat error.html # To view the contents of error.html
Now our bucket is ready to be used as a server with index.html
file as the main file for our website and error.html
file as the error handling file. Now let’s upload these files to the bucket. Replace <your-bucket-name>
, in the command given below, with the name of the bucket, and then execute this command to upload the folder containing our website files to the bucket:
aws s3 sync Website s3://<your-bucket-name>/
Website
in the above command refers to a pre-built folder that contains index.html
and error.html
files.
Our S3 bucket is hosting the website, but it is not publicly accessible yet. To make it publically accessible, we’ll have to attach a <your-bucket-name>
, in the command given below, with the name of the bucket, and then execute this command:
echo '{"Version":"2012-10-17","Statement":[{"Sid": "PublicReadGetObject","Effect": "Allow","Principal": "*","Action": "s3:GetObject","Resource": "arn:aws:s3:::<your-bucket-name>/*"}]}' > policy_s3.json
This policy will provide everyone the read access to all the files in our bucket.
By default, we are not allowed to add a policy to the bucket; let’s modify that permission first. To do that, replace <your-bucket-name>
, in the command given below, with the name of the bucket, and then execute this command:
aws s3api put-public-access-block --bucket <your-bucket-name> --public-access-block-configuration "BlockPublicPolicy=false,RestrictPublicBuckets=false
Now add the resource-based policy we created earlier to the bucket. Replace <your-bucket-name>
, in the command given below, with the name of the bucket, and then execute this command to add the bucket policy:
aws s3api put-bucket-policy --bucket <your-bucket-name> --policy file://policy_s3.json
Our website is now live and publically accessible.
The last step is to access our website. The generic URL to access a website hosted on an S3 bucket is as follows:
http://<your-bucket-name>.s3-website.us-east-1.amazonaws.com
Replace <your-bucket-name>
with the name of your bucket and open this URL. We should be able to access our website. By default the index.html
page will be redered. In case of any error, we’ll be redirected to the error.html
page.
Enter your AWS access_key_id
and secret_access_key
in the widget below before running any commands. If you don’t have these keys, follow the steps given in this documentation to generate the keys.
Note: The IAM user whose credentials are being used must have the permissions to perform all the required actions.
Free Resources