Prerequisites:
- AWS CLI installed and configured with your AWS credentials and default region.
- Your static website files (e.g.,
index.html
,style.css
) ready locally.
Step 1: Create an S3 Bucket
Run this command to create a new bucket. Replace my-static-site-bucket
with a unique bucket name:
aws s3 mb s3://my-static-site-bucket
Step 2: Enable Static Website Hosting on the Bucket
Use this command to configure your bucket for static website hosting:
aws s3 website s3://my-static-site-bucket/ --index-document index.html --error-document error.html
(You can skip --error-document
or replace with your actual error page)
Step 3: Upload Your Website Files to the Bucket
Upload all your local files (e.g., from a folder website-files/
) to the bucket:
aws s3 sync ./website-files/ s3://my-static-site-bucket/
This command uploads everything in the local website-files
directory to your bucket.
Step 4: Set Bucket Policy to Make Files Public
To allow everyone to read your website files, create a JSON file policy.json
with this content (replace my-static-site-bucket
):
{
"Version": "2012-10-17",
"Statement": [{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::my-static-site-bucket/*"
}]
}
Apply the policy with this command:
aws s3api put-bucket-policy --bucket my-static-site-bucket --policy file://policy.json
Step 5: Access Your Website
Your static site will be accessible at this URL (adjust region as needed):
http://my-static-site-bucket.s3-website-us-east-1.amazonaws.com
You can check the exact website endpoint URL by running:
aws s3api get-bucket-website --bucket my-static-site-bucket
Bonus: Delete the Bucket and Contents (If Needed)
To remove all files and delete the bucket:
aws s3 rm s3://my-static-site-bucket --recursive
aws s3 rb s3://my-static-site-bucket
Summary
Step | AWS CLI Command |
---|---|
Create Bucket | aws s3 mb s3://my-static-site-bucket |
Enable Website Hosting | aws s3 website s3://my-static-site-bucket/ --index-document index.html --error-document error.html |
Upload Website Files | aws s3 sync ./website-files/ s3://my-static-site-bucket/ |
Apply Public Read Policy | aws s3api put-bucket-policy --bucket my-static-site-bucket --policy file://policy.json |
Get Website Configuration | aws s3api get-bucket-website --bucket my-static-site-bucket |