4 minute read

Hosting on Amazon S3

Required expertise level : Intermediate

Platform : Any

Last tested and confirmed : January 2022


S3 provides object storage service, which means that data is stored and addressed as objects, each object contains it’s own data in addition to meta-data and a unique identifier.

Object storage is often used to store big amounts of data that doesn’t need the features and structure of the file systems hierarchy.

The main advantage of using S3 to host our static mirror is the ability to serve a fully functional “static” web pages directly from Amazon S3 URLs which consists of [bucketname]+[endpoint] or [endpoint]/[bucketname] ex: mybucket.s3.us-east-2.amazonaws.com or https://s3.us-east-2.amazonaws.com/mybucket


Configure static site hosting on S3

Creating S3 bucket, and setting configurations for Static Site hosting

AWS login page

S3 management console

S3 new bucket

  • In Set permissions tab, uncheck Block all public access, and confirm that you want to enable public access to the bucket.

Bucket permission tab

  • Finally, review your options and create the new bucket.

Create bucket

  • Your newly created bucket should appear in this form.

  • Head to the new bucket’s settings by clicking on the bucket name.

S3 bucket settings

  • In Properties tab click on Static website hosting

S3 bucket Properties

  • After setting Use this bucket to host a website, you need to set the path for your index.html(main page), optionally you can set a custom error.html page, and click save.

S3 Static website hosting

S3 enabled Static website hosting

  • Head to Permissions tab, and click on Bucket Policy.

S3 bucket policy

  • The simplest form of static website hosting policy on S3 should look like this.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": [
                "s3:GetObject"
            ],
            "Resource": [
                "arn:aws:s3:::example.com/*"
            ]
        }
    ]
}

Now, we are ready to upload our static mirror to our S3 bucket.

Upload using the Web user interface

  • You will find the upload option in the bucket settings page.

S3 files upload

  • Make sure grant public read access to the uploaded files in the permissions tab.

S3 files upload permissions


Install AWS Command Line Interface

  • MS Windows

    • Download and install the official installation file
    • Install using Chocolatey windows package manager choco install awscli
  • Gnu/Linux

    • Normally, you will fine AWS CLI package available in your distribution software repositories, in that case you can simply use your package manager to install it directly. ex: apt install awscli
    • If that’s not the case, you can install it manually by executing these commands in your terminal in their respective order.

Linux x86 (64-bit)

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

Linux ARM

curl "https://awscli.amazonaws.com/awscli-exe-linux-aarch64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install
  • macOS
    • Install using Homebrew package manager. brew install awscli

Upload using AWS CLI

awscli configure

  • ِMove to the website local directory.

  • Upload your files by executing replace [bucket-name] with the name of the bucket you created on S3

aws s3 sync . s3://[bucket-name]/

Now you can test your new static mirror, you’ll find the website URL in bucket settings > Properties > Static website hosting

Find the configured URL for your website hereFind the configured URL for your website here

Now, there are two different URL structures which allows accessing your static website on the secure protocol HTTPS

  • Bucket name as a sub-domain https://[mystaticwebsitetest].s3.us-east-2.amazonaws.com

  • Bucket name in the path https://s3.us-east-2.amazonaws.com/[mystaticwebsitetest]