How to Host a Static Website Using AWS CLI in Less Than 5 Minutes

Victor Robin - Oct 11 - - Dev Community

Introduction:

In this guide, I’ll show you how to host a static website on AWS S3 using only the AWS CLI. In just 5 minutes, you’ll have your site live, ready to share with friends or showcase as a portfolio. No complex setups—just a few simple CLI commands.

Key Terminologies:

  • AWS CLI: A command-line tool that lets you interact with AWS services from your terminal.

  • S3: Amazon Simple Storage Service (S3) is a scalable storage solution that can also serve static websites.

  • IAM User Account: Identity and Access Management (IAM) enables you to securely control access to AWS resources.

  • IAM Policies: Rules that define what actions are allowed or denied for specific users or services.

IAM USER Setting

From the screenshot of my console, you can see that my IAM account has an AdministratorAccess policy, and I’ve created and downloaded my access key, which is required to set up the AWS CLI

Prerequisites:

  • Have your static website files ready on your local computer.
  • Have an AWS IAM account with Administrator privileges.
  • Ensure AWS CLI is configured on your local machine. If not, See the installation guide for your OS here.

Here's a breakdown of what we'll be doing:

  • Create an S3 bucket to host your website.
  • Upload your website files using the CLI.
  • Configure the bucket for website hosting and allow public access.
  • Set a bucket policy to make the website publicly accessible. Your final website URL will follow this pattern:  YourBucketName.s3-website-YourRegion.amazonaws.com

For example, if your bucket name is victor-robin and your region is us-east-1, your website URL would be: 

victor-robin.s3-website-us-east-1.amazonaws.com

Step-by-Step Guide:

Assuming you've completed the prerequisites, here's how to set up the AWS CLI and host a static website on S3:

1. Configure AWS CLI:

Go to your command prompt and input:

aws configure
Enter fullscreen mode Exit fullscreen mode

Fill in the following details. You can find these in the access key file you downloaded for the IAM account:

  • AWS Access Key ID: YourAccessKeyID
  • AWS Secret Access Key: YourSecretKey
  • Default Region: YourIAMRegion (e.g., "us-east-1")
  • Default Output: Leave this as empty and press Enter which signifies "none" .

CLI Configuration

Voilà! Your AWS account is now set up on your CLI.

2. Create an S3 Bucket:

Run this command to create an S3 bucket:

aws s3api create-bucket --bucket YourBucketName --region YourRegion
Enter fullscreen mode Exit fullscreen mode

Alternatively:

aws s3 mb s3://YourBucketName - region YourRegion
Enter fullscreen mode Exit fullscreen mode


 
Ensure your bucket name is unique and in lowercase. For example, I used "victor-robin" as my bucket name and "us-east-1" as my region. If you encounter errors, it's most likely due to not following the bucket naming convention. Check out the Naming Rules for S3.

3. Verify Bucket Creation:

To confirm the bucket was created successfully, run:

aws s3 ls
Enter fullscreen mode Exit fullscreen mode

4. Upload Your Website Files:

Copy the path to the folder where your website files are located on your local machine, then return to your CLI and run the following command:

aws s3 cp /path/to/your/WebsiteFolder s3://YourBucketName/ - recursive
Enter fullscreen mode Exit fullscreen mode


 
This uploads all your website files to the S3 bucket. Replace /path/to/your/WebsiteFolder with the actual path to your website files folder.

5. Enable Website Hosting:

Configure the bucket to serve as a website:

aws s3 website s3://YourBucketName --index-document index.html --error-document error.html
Enter fullscreen mode Exit fullscreen mode


 
You can skip the --error-document error.html part if you don't have an error file in your website files.

6. Remove Public Access Block:

By default, S3 buckets block public access to newly created buckets. To remove this restriction, run:

aws s3api delete-public-access-block - bucket YourBucketName
Enter fullscreen mode Exit fullscreen mode

Screenshot-CLI

7. Set a Bucket Policy:

Create a JSON file named policy.json with the following content:

{
 "Version": "2012–10–17",
 "Statement": [
 {
 "Sid": "PublicReadGetObject",
 "Effect": "Allow",
 "Principal": "*",
 "Action": "s3:GetObject",
 "Resource": "arn:aws:s3:::YourBucketName/*"
 }
 ]
}
Enter fullscreen mode Exit fullscreen mode

Replace YourBucketName with your actual bucket name in the policy file. Save the file, then run this:

aws s3api put-bucket-policy --bucket YourBucketName --policy file://policy.json
Enter fullscreen mode Exit fullscreen mode


 
switch to policy-file directory

Make sure you're in the directory where the policy.json file is saved. In my case, I had to switch to my Desktop directory where the policy.json file was stored before running the command, as shown in the screenshot below.

8. Test Your Website:

Open your browser and visit the URL: 
YourBucketName.s3-website-YourRegion.amazonaws.comin my case I've victor-robin.s3-website-us-east-1.amazonaws.com

Your website should now be live just like mine!

victor-robin.s3-website-us-east-1.amazonaws.com

Conclusion:

In this article, we’ve walked through the process of hosting a static website on AWS S3 using the AWS CLI. With just a few simple commands, you’ve created an S3 bucket, uploaded your files, configured it for website hosting, and made it publicly accessible—all from the command line. Happy hosting! If you found this helpful, feel free to share your thoughts in the comments.

. .
Terabox Video Player