AWS CLI S3 Static Web site.
md 2/23/2022
Setting Up an S3 Static Website Using AWS CLI
Description
In this article, I'll be setting up an S3 bucket website. I'll also verify the website is accessible and working as
expected. S3 bucket websites are excellent for hosting single-page, customer-facing content, as they are easy
to set up and offer the same high availability and scalability as S3.
1. Open a terminal session and log in to the provided EC2 instance via SSH using the credentials provided
on the lab page:
ssh -i <key_name.pem> ec2-user@<PUBLIC IP>
2. Create an S3 Bucket from AWS CLI
Create an S3 bucket in the us-east-1 region, replacing <UNIQUE_BUCKET_NAME> with a unique bucket
name, and using the S3 API:
aws s3api create-bucket --bucket <UNIQUE_BUCKET_NAME> --acl public-read
3. Modify the Newly Created Bucket to Be an S3 Website Bucket
1/3
AWS CLI S3 Static Web site.md 2/23/2022
Issue the AWS S3 API CLI command to enable the "Static website hosting" property of your bucket. In
this same command, you'll also provide the index.html page, which is what your bucket URL will serve:
aws s3 website s3://<UNIQUE_BUCKET_NAME> --index-document index.html
4. Modify Provided S3 Policy File and Use It to Modify the Bucket Policy
Open the policy_s3.json file:
vim policy_s3.json
Copy the json file content in it.
{
"Version":"2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::<UNIQUE_BUCKET_NAME>/*"
}
]
}
Save and exit the file.
5. Use the S3 policy file to modify the bucket policy so your objects are publicly accessible, which is a
requirement for S3 static websites:
aws s3api put-bucket-policy --bucket <UNIQUE_BUCKET_NAME> --policy
file://policy_s3.json
6. Clone our web site from github Page and Upload File to S3 Website Bucket
sudo apt-get install git
git clone https://github.com/Eser-U/freelanceweb.git
cd designer
6. Upload the all files to your S3 website bucket:
2/3
AWS CLI S3 Static Web site.md 2/23/2022
aws s3 cp ./ s3://<UNIQUE_BUCKET_NAME> --recursive
7. Verify Your S3 Static Website Is Working
Enter the S3 website URL for your bucket in the browser to ensure it's working.
You can also test from the terminal using the curl command:
curl http://.s3-website.us-east-1.amazonaws.com
8. To delete all the objects from an S3 bucket, --recursive option can be used after the specified bucket
name as shown below.
aws s3 rm s3://bucket-name --recursive
9. To delete the bucket give command shown below.
aws s3 rb s3://bucket-name
10. Instead of 8th and 9th step you can force to delete, so you can imply that command shown below
aws s3 rm s3://bucket-name --recursive --force
Thank You!
3/3