How to Host a Static Website Using Amazon S3
Amazon S3 (Simple Storage Service) is a scalable storage service offered by AWS (Amazon Web Services). In this tutorial, we'll walk through the task to create an S3 bucket, configure its settings, and host a static website.
Task 1:Create a New S3 Bucket
Task 2: Upload Files to Your Bucket and access the file through URL
Task 3:Host a Static Website
Task 1: Create a New S3 Bucket
Log in to AWS Management Console and navigate to the S3 service.
Click on "Create bucket" to start the process.
General Configuration
Bucket Name: Enter a unique name.
Region: Select the desired region for your bucket.
Object Ownership
- ACL (Access Control List): For this tutorial, we will disable this option.
Block Public Access Settings
To make the bucket publicly accessible, do not select any options under "Block all public access". Acknowledge the current settings to proceed.
-
This feature helps maintain multiple versions of an object. For now, we'll disable bucket versioning.
Default Encryption
Server-Side Encryption with Amazon S3 Managed Keys (SSE-S3): This option is selected by default. It ensures that the objects stored in the bucket are encrypted.
-
Click on "Create bucket" to create your bucket. You will see your bucket listed among your existing buckets.
Task 2: Upload Files to Your Bucket and access the file through URL
Click on the bucket name to open it.
Click on "Upload", then select the file you want to upload and click on "Upload" again.
Once uploaded, you can click on the file name to open it. You can share the URL to allow others to access the file.
Now, you can see your file is successfully accessible
Task 3: Host a Static Website
Create a new bucket for your website, e.g.,
ukg-aws-static-bucket
.Region: Select the desired region.
Block Public Access: Unselect all options and acknowledge.
Click on "Create bucket".
Enable Static Website Hosting
Click on your new bucket that you created above(
ukg-aws-static-bucket
).Navigate to the Properties tab.
Scroll down to Static website hosting and click on Edit.
Enable static website hosting.
Hosting Type: Select "Host a static website".
Index Document: Enter
index.html
.Error Document: Enter
error.html
.Click on Save changes.
Set Bucket Policy for Public Access
Navigate to the Permissions tab of your bucket.
Click on Bucket Policy and then Edit.
you can use the AWS Policy Generator to create this policy:
Click on Policy Generator.
Step 1: Select "S3 Bucket Policy".
Step 2: In "Effect", select "Allow".
Principal: Enter
*
.Action: Select
GetObject
.Amazon Resource Name (ARN): Enter
arn:aws:s3:::ukg-aws-static-bucket
.Click on Add Statement and then Generate Policy.
Copy the generated policy and paste it into the bucket policy editor.
After clicking Save Changes we get some error:
Get some error because there is a problem in the resource name
it should be
"Resource": "arn:aws:s3:::ukg-aws-static-bucket/*"
. you are missing the/*
which means any object inside the bucket.Upload Website Files:
Go back to the Overview tab.
Upload
index.html
anderror.html
to your bucket.Accessing Your Static Website
Navigate to the Properties tab, scroll down to Static website hosting, and you'll see the URL for your website.
Visit the URL to see your website. If everything is set up correctly, your
index.html
page will be displayed.Error Handling: If you enter a wrong URL or navigate to a non-existent page, the
error.html
page will be shown, indicating that the page was not found.Type a wrong URL:
https://ukg-aws-static-bucket.s3-website.us-east-2.amazonaws.com/profile
Conclusion
By following these steps, you can create an S3 bucket, configure its settings, and host a static website. Amazon S3 provides a cost-effective way to host static content and can scale to meet your needs.
**Happy hosting!**๐
Subscribe to my newsletter
Read articles from Utkarsh Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Utkarsh Gupta
Utkarsh Gupta
๐ Hi everyone! I'm Utkarsh Gupta, IAM Consultant. I am very passionate for all things DevOps. Right now, I'm learning and working on some cool projects and I can't wait to share what I'm learning with you all! ๐ ๏ธ Here's what's in my toolbox: Linux Docker Kubernetes Jenkins AWS Python Prometheus Grafana Ansible Terraform Join me as we explore AWS DevOps together. Let's learn and grow together in this ever-changing field! ๐ค Feel free to connect with me for: Sharing experiences Friendly chats Learning together Follow my journey on Hashnode and LinkedIn for daily updates. Let's dive into the world of DevOps together! ๐ #DevOps #AWS #DevOpsJourney #90DaysOfDevOps