Skip to main content

Setting Up an S3 Static Website Using AWS CLI

Hands-On Lab

 

Photo of Moosa Khalid

Moosa Khalid

AWS Training Architect II

Length

00:30:00

Difficulty

Intermediate

In this hands-on lab, we'll be setting up an S3 bucket website. We'll also verify the website is accessible and working as expected. S3 bucket websites are excellent for hosting single-page, customer-facing content, as they are easy to set up and offer the same high availability and scalability as S3.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Setting Up an S3 Static Website Using AWS CLI

Introduction

In this hands-on lab, we'll be setting up an S3 bucket website. We'll also verify the website is accessible and working as expected. S3 bucket websites are excellent for hosting single-page, customer-facing content, as they are easy to set up and offer the same high availability and scalability as S3.

Solution

Open a terminal session and log in to the provided EC2 instance via SSH using the credentials provided on the lab page:

ssh cloud_user@<PUBLIC IP>

Create an S3 Bucket from AWS CLI

  1. Create an S3 bucket in the us-east-1 region, replacing <UNIQUE_BUCKET_NAME> with a unique bucket name, and using the S3 API:

    aws s3api create-bucket --bucket <UNIQUE_BUCKET_NAME> --acl public-read

Modify the Newly Created Bucket to Be an S3 Website Bucket

  1. Issue the AWS S3 API CLI command to enable the "Static website hosting" property of your bucket. In this same command, you'll also provide the index.html page, which is what your bucket URL will serve:

    aws s3 website s3://<UNIQUE_BUCKET_NAME> --index-document index.html

Modify Provided S3 Policy File and Use It to Modify the Bucket Policy

  1. Open the policy_s3.json file:

    vim policy_s3.json
  2. In the Resource line, enter your bucket name.

  3. Save and exit the file.

  4. Use the S3 policy file to modify the bucket policy so your objects are publicly accessible, which is a requirement for S3 static websites:

    cd
    aws s3api put-bucket-policy --bucket <UNIQUE_BUCKET_NAME> --policy file://policy_s3.json

Create a Basic index.html Page and Upload File to S3 Website Bucket

  1. Create a basic HTML page:

    echo "<html><center><h1>My Static Website on S3</h1></center></html>" > index.html
  2. Upload the index.html file to your S3 website bucket:

    aws s3 cp index.html s3://<UNIQUE_BUCKET_NAME>

Verify Your S3 Static Website Is Working

  1. Enter the S3 website URL for your bucket in the browser to ensure it's working.
  2. You can also test from the terminal using the curl command:

    curl http://<UNIQUE_BUCKET_NAME>.s3-website.us-east-1.amazonaws.com

Conclusion

Congratulations on successfully completing this hands-on lab!