Hosting a Static Website on Amazon S3 Using AWS CLI

Marc Mandela
4 min readJan 5, 2024

--

Bytes & Brews | Featured scripts have been tested for Linux & Windows

Hosting a Static Website on Amazon S3 Using AWS CLI
Bytes & Brews

Welcome back to our weekly Bytes & Brews and I would like to begin by wishing you a Happy New Year 2024! May this year be a canvas of opportunities where dreams take flight, aspirations find their wings and each day unfolds like a precious gift waiting to be unwrapped.

Introduction

In the digital age, hosting a website is easier than ever, especially with cloud services like Amazon Web Services (AWS) offering simple and cost-effective solutions. Amazon S3 (Simple Storage Service) provides an excellent platform for hosting static websites, like the one shown below.

With AWS Command Line Interface (CLI), the process becomes even more streamlined. This week’s guide will walk you through hosting a static website on Amazon S3 using the AWS CLI, enabling you to deploy your website swiftly and efficiently.

Amazon S3 static web hosting
http://mmbonzo254.s3-website-us-west-2.amazonaws.com/

Step 1: Install and Configure AWS CLI

Ensure you have the AWS CLI installed on your local machine. If not, download and install it from the official AWS CLI website. After installation, configure the AWS CLI by running the following command and providing your Access Key ID, Secret Access Key, region, and default output format:

AWS CLI configure command
aws-cli

Step 2: Create an S3 Bucket for Your Website

Using the AWS CLI, create an S3 bucket to store your website content. Choose a globally unique bucket name (replace <your-bucket-name> with your preferred name):

AWS CLI create new bucket
aws-cli

Step 3: Enable Website Hosting on Your Bucket

Once the bucket is created, enable static website hosting by configuring it as a website. Replace <index-document> and <error-document> with your preferred index and error documents (e.g., index.html, error.html):

aws-cli

Step 4: Upload Your Website Content to the S3 Bucket

Upload your website files (HTML, CSS, JavaScript, etc.) to the S3 bucket using the sync command. Replace <local-folder-path> with the path to your website files:

AWS CLI s3 sync
aws-cli

Step 5: Set Bucket Policy for Public Access

Configure the bucket policy to allow public access to your website content. Create a file named policy.json with the following content:

policy.json

Replace <your-bucket-name> with your bucket name and then apply the policy to your bucket:

AWS CLI attach bucket policy
aws-cli

Conclusion

In today’s guide, we’ve created an S3 bucket, enabled website hosting, uploaded new website content, and set permissions for public access. With Amazon S3’s scalability and reliability, our website is now ready to serve visitors seamlessly.

With AWS S3’s reliability and AWS CLI efficiency, managing your website becomes hassle-free, allowing you to focus more on your content and less on infrastructure. We are now ready to explore further AWS features in the coming weeks to optimize our website for a superior online presence.

Let me know in the comments below once you give it a try on your development environment!

Happy Clouding!

Bytes & Brews

--

--

Marc Mandela

Marc is a passionate cloud & development expert with a knack for optimizing digital landscapes, transforming businesses of all sizes with innovative solutions.