Hosting a static website using AWS S3.
Create a static café website that showcases the café visually through images and provides business details, such as:
- Location of the store
- Business hours
- Café’s telephone number
All resources will be created using the AWS CLI:
- Create an S3 bucket.
- Create a new IAM user with full access to Amazon S3.
- Upload files to Amazon S3 to host the static website.
- Create a script to update the website automatically when local files change.
- Use a Linux image to create a CLI machine.
- Install and configure AWS CLI on the EC2 instance.
- Bucket name must be unique (e.g., combination of initials and random numbers).
- Example command:
aws s3api create-bucket --bucket <your-unique-bucket-name> --region us-west-2 --create-bucket-configuration LocationConstraint=us-west-2- Example successful output:
{
"Location": "http://<your-unique-bucket-name>.s3.amazonaws.com/"
}- Create the user:
aws iam create-user --user-name nithya- Create login profile:
aws iam create-login-profile --user-name nithya --password Test123!- List AWS-managed policies containing "S3":
aws iam list-policies --query "Policies[?contains(PolicyName,'S3')]"- Attach full access policy to the user:
aws iam attach-user-policy --policy-arn arn:aws:iam::aws:policy/<policy-name> --user-name nithya- Go to the S3 console → your bucket → Permissions.
- Under Block public access, choose Edit, deselect Block all public access, and save.
- Under Object Ownership, choose Edit, select ACLs enabled, acknowledge, and save.
- Enable website hosting:
aws s3 website s3://<your-bucket>/ --index-document index.html- Upload files recursively:
aws s3 cp /home/ec2-user/sysops-activity-files/static-website/ s3://<your-bucket>/ --recursive --acl public-read- Verify files:
aws s3 ls <your-bucket>- Go to Properties → Static website hosting → confirm it is enabled.
- Open the Bucket website endpoint URL to view the live website.
- Create a batch script:
vi update-website.sh- Add the following content:
#!/bin/bash
aws s3 sync /home/ec2-user/sysops-activity-files/static-website/ s3://<your-bucket>/ --acl public-read- Make it executable:
chmod +x update-website.sh- Test the script:
./update-website.sh- Optional: set a cron job to run this script automatically for continuous sync.
- Edit
index.htmllocally:
vi sysops-activity-files/static-website/index.html-
Example edits in VI:
- Change
bgcolor="aquamarine"→bgcolor="gainsboro" - Change
bgcolor="orange"→bgcolor="cornsilk"
- Change
-
Save changes (
Esc→:wq) and run the update script:
./update-website.sh- Verify changes on the live website.
Congratulations! Your static website is now hosted on AWS S3 and can be updated automatically using the batch script.
