Using AWS CLI to create a static WebSite on S3

To create a bucket, you must register with Amazon S3 and have a valid AWS Access Key ID to authenticate requests. By creating the bucket, you become the bucket owner.

Not every string is an acceptable bucket name. For information about bucket naming restrictions. See Bucket naming rules

Step 1 – Choose where you want to run AWS Command Line Interface (CLI)

There are several methods to using AWS CLI

  1. My Choice – Install prerequisite utilities and AWS CLI on your desktop or laptop.
  2. An alternate method – Use AWS Cloud9. An EC2 instance is created and configured by the AWS Cloud9 service. Cloud9 configuration of an EC2 instance comes with the prerequisite utilities and AWS CLI already configured for use within your AWS account. Cloud9 may require enabling the AWS Toolkit in Cloud9 to manage some services. The welcome screen discusses why and how to use the toolkit). AWS Cloud9 is free to use for new accounts using the Free Tier option.
  3. Another method – Create a virtual machine locally, then install the prerequisite utilities and AWS CLI installed into the virtual machine.
    • For Windows machines – Virtual Box, VMware Workstation, or VMWare Player.
    • For Mac’s you can use Virtual Box, VMware Fusion or Parallels desktop
    • For Linux machines use Virtual Box or VMware Workstation Player
  4. Another method – Use a Docker Container and run CLI from the container
  5. Another tool to possibly use is Dockers Dev Environment which at the time of this writing is in Preview Mode, I haven’t tried the preview yet.

Note: A fun activity is using Hashicorp/Vagrant to automate the installation and configuration of virtual machines. Creating a standard Dev. Environment amongst developers. By using Vagrant the vagrant file (script) creates and configures a virtual machine exactly the same way on Macs, Windows and Linux machines, using Vagrant and Virtualbox. Thus, assuring everyone is using the same version of Python for example! Vagrant does work with VMware Workstation or VMware fusion (at cost).

Note2: Another fun activity is using HashiCorp/Packer to create and create a standard Docker Image for developers to use as a standard docker image. Like Vagrant, Packer scripts the creation of an image, and the installation of specific versions of the requisite utilities for AWS CLI. An example, is to use these specific version of AWS CLI and Python (aws-cli/2.1.29, Python/3.7.3) when creating and configuring a docker image.

Note3: Both Vagrant and Packer use "provisioners", a built in command to configure a virtual machine or docker image. I personally like to use Hashicorp/Ansible for the configuration, in my opinion Ansible is more intuitive, easier to use, more immutable and declarative as a configuration tool.

The primary difference between Vagrant and Packer, is that Vagrant creates a Virtual Machine, whereas Packer creates a Docker image. A Virtual Machine can perpetually save all of the local files by simply suspending a virtual machine when finished for the day, whereas Docker images needs to map to a local directory for persistent storage. I like using a virtual machine (possibly even with shared folders), but that is my old school methods getting in the way perhaps 🙂


The Difference in the alternatives above

The primary difference between installing AWS CLI on your desktop or laptop or using one of the alternative methods above is all about controlling your utility versions. An Example, two members of a team, use AWS CLI installed on their desktops. Team member “Tom Jones” is running AWS CLI Version 1 with Python Version 2.7, and member “John Thomas” is running AWS CLI Version 2 with Python Version 3.8. Different versions behave differently, what Tom can or can’t accomplish, most likely will be a different experience than John’s.

Cloud 9, Virtual Machines, or Docker Images, can and should have specific versions of utilities maintained by agreement amongst members of the team. Everyone will be able to accomplish the same tasks, share the same git repositories, etc., with the assurance of the same experience and outcomes.

Step 2 – Install and configure AWS CLI

This topic provides links to information about how to install, update, and uninstall version 2 of the AWS Command Line Interface (AWS CLI) on the supported operating systems.

AWS CLI version 2 installation instructions:

Note: the above instructions are links to AWS documentation. I’m planning on writing up the use of Packer for Docker and Vagrant for virtual machines along with Ansible configurations, as future posts.

Step 3 – Create a Bucket

When you have configured your AWS CLI environment, you should be able to run the following command.

aws s3 mb s3://bucket-name

Step 4 – Enable the bucket as a static website

aws s3 website s3://bucket-name/ –index-document index.html –error-document error.html

Step 5 – Apply the policy to the new bucket

Create a new local file “bucket_policy.json” with this content:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::bucket_name/*"
        }
    ]
}

Make sure to replace the bucket_name in the above content with your new S3 bucket name

Execute the following command in your command-line interface (CLI)

aws s3api put-bucket-policy –bucket bucket-name –policy file://./bucket_policy.json

Step 6 – Create the index.html and error.html files

Create an index.html file. If you don’t have an index.html file, you can use the following HTML to create one (using any text editor):

Save the index file locally. The index document file name is case-sensitive. For example, index.html and not Index.html.

To configure an error document Create an error document, for example, error.html (using any text editor):

Save the error document file locally. Remember, The file name is case sensitive

Step 7 – Copy the files into your S3 bucket

aws s3 cp index.html s3://bucket-name
aws s3 cp error.html s3://bucket-name

S3 static WebSite should be operational

The website will be at the following address:

bucket-name.s3-website.your-aws-region.amazonaws.com

Exit mobile version
%%footer%%