Amazon S3 (Simple Storage Service) is a scalable storage service offered by AWS (Amazon Web Services). In this tutorial, we'll walk through the steps to create an S3 bucket, configure its settings, and host a static website.
Step 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.
Step 2: Configure Bucket Settings
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.
Bucket Versioning
This feature helps maintain multiple versions of an object. For now, we'll disable bucket versioning.
Tags (Optional)
Add tags to help identify your bucket. For example:
Key: Name
Value: Zerotohero
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.
Advanced Settings
Object Lock: This feature prevents objects from being deleted or overwritten for a fixed amount of time. We'll leave this disabled for now.
Click on "Create bucket" to create your bucket. You will see your bucket listed among your existing buckets.
Step 3: Upload Files to Your Bucket
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.
Step 4: Delete the Bucket (Optional)
To delete a bucket, ensure it is empty.
Select the bucket, click on "Delete", enter the bucket name, and confirm the deletion.
Step 5: Host a Static Website
Create a new bucket for your website, e.g.,
AWS_Static_Website
.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.
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.
Step 6: Set Bucket Policy for Public Access
Navigate to the Permissions tab of your bucket.
Click on Bucket Policy and then Edit.
Enter the following bucket policy to allow public read access:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::AWS_Static_Website/*"
}
]
}
- Save the changes.
Alternatively, 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:::AWS_Static_Website
.Click on Add Statement and then Generate Policy.
Copy the generated policy and paste it into the bucket policy editor.
Save the changes.
Step 7: 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.
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!