Create an S3 bucket using AWS CLI

To create a bucket, you must register an account on Amazon Web Services and have a valid AWS Access Key ID to authenticate requests.

Creating an S3 bucket can be accomplished in several ways:

  • My Choice – Install AWS CLI on your desktop or laptop.
  • Use AWS cloud 9 – AWS Cloud9 is free for new accounts using the Free Tier option.
  • Use a virtual machine locally, then install AWS CLI on the virtual machine.
    • For Windows – Virtual Box, VMware Workstation, or VMWare Player.
    • For MAC, you can use Virtual Box, VMware Fusion, or Parallels desktop
    • For Linux, use Virtual Box or VMware Workstation Player
    • Use a Docker Container and run CLI from the container

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

Install and configure AWS CLI

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.

Create a Bucket

Before executing the following command, you must configure the AWS CLI

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

AWS s3 mb s3://bucket-name

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

Creating a Static Website using Amazon AWS S3

Step 1: Create a bucket

Note: The instructions below uses the AWS console to create a bucket. It is an easy method for creating an S3 bucket.

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

You can create a bucket using other methods, like for instance using a Mac or Linux terminal command line interface, or Windows CMD or PowerShell command line interface.

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/
  2. Choose Create bucket
  3. Enter the Bucket name (for example, my-awesome-bucket)

*Note: S3 buckets must have a UNIQUE NAME. Literally it has to be a unique name within AWS S3 for ALL REGIONS, Globally

  1. Choose the Region where you want to create the bucket
  2. Accept the default settings and create the bucket, choose Create.

Step 2: Enable static website hosting

After you create a bucket, you can enable static website hosting for your bucket.

To enable static website hosting

  1. In the Buckets list, choose the bucket that you want to enable static website hosting
  2. Choose Properties
  3. Under Static website hosting, choose Edit
  4. Choose Use this bucket to host a website
  5. Under Static website hosting, choose Enable
  6. In Index document, enter the file name of the index document, typically index.html
  7. To provide your own custom error document for 4XX class errors, in Error document, enter the custom error document file name
  8. Choose Save changes

Amazon S3 enables static website hosting for your bucket. At the bottom of the page, under Static website hosting, you see the website endpoint for your bucket

Under Static website hosting, note the Endpoint

The Endpoint is the Amazon S3 website endpoint for your bucket. After you finish configuring your bucket as a static website, You can use this endpoint to test your website.

Step 3: Edit Block Public Access settings

By default, AWS blocks public access to your account and buckets.

If you want to use a bucket to host a static website, you can use these steps to edit your block public access settings.

Warning

Before you complete this step, review Blocking public access to your Amazon S3 storage to ensure that you understand and accept the risks involved with allowing public access. When you turn off block public access settings to make your bucket public, anyone on the internet can access your bucket. We recommend that you block all public access to your buckets.

  1. Open the Amazon S3 console at https://console.aws.amazon.com/s3/
  2. Choose the name of the bucket that you have configured as a static website
  3. Choose Permissions
  4. Under Block public access (bucket settings), choose Edit
  5. Clear Block all public access, and choose Save changes

Step 4: Add a bucket policy to make your bucket publicly available

After you edit S3 Block Public Access settings, you can add a bucket policy to grant public read access to your bucket. When you grant public read access, anyone on the internet can access your bucket.

  1. Under Buckets, choose the name of your bucket
  2. Choose Permissions
  3. Under Bucket Policy, choose Edit
  4. To grant public read access for your website, copy the following bucket policy, and paste it in the Bucket policy editor

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

Update the code in the new policy with the name of YOUR BUCKET before saving.

In the preceding example, "Bucket-Name" is a placeholder. To use this bucket policy with your BUCKET, you must update the policy name to match your bucket’s name.

Choose Save changes.

Step 5: Configure an index document

When you enable static website hosting for your bucket, you enter the name of the index document (for example, index.html). After you enable static website hosting for the bucket, you upload an HTML file with this index document name to your bucket.

To configure the index document

  1. Create an index.html file. If you don’t have an index.html file, you can use the following HTML to create one:
    
    
        My Website Home Page
    
    
    <h1>Welcome to my website</h1>
    <p>Now hosted on Amazon S3!</p>
    
    
  1. Save the index file locally.

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

Alternative Index method, I have provided a compressed zip file that contains a generic web site. You are free to download the file, Unzip the files locally and then upload the index.html along with the images and assets folder up to your S3 bucket.

  1. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/
  2. In the Buckets list, choose your bucket
  3. To upload the index documentto your bucket, do one of the following:
    • Drag and drop the index file into the console bucket listing.
    • Choose Upload, and follow the prompts to choose and upload the index file (Or Perhaps) Upload your own static website files to your bucket or the generic web site files found below.

To configure an error document Create an error document, for example 404.html

    
    
        Something went wrong
    
    
    <h1>Sorry about that</h1>
    <p>Now hosted on Amazon S3!</p>
    
    

  1. Save the error document file locally

    Remember, The file name is case sensitive

  2. Sign in to the AWS Management Console and open the Amazon S3 console at https://console.aws.amazon.com/s3/

  3. To upload the error document to your bucket, do one of the following:

    • Drag and drop the error document file into the console bucket listing.
    • Choose Upload, and follow the prompts to choose and upload the index file. For step-by-step instructions, see Uploading objects.

Step 7: Test your website endpoint

After you configure static website hosting for your bucket, you can test your website endpoint.

Note: Amazon S3 does not support HTTPS access to the website. If you want to use HTTPS, you can use Amazon CloudFront to serve a static website hosted on Amazon S3.

  1. Under Buckets, choose the name of your bucket
  2. Choose Properties
  3. At the bottom of the page, under Static website hosting, choose your Bucket website endpoint
  4. Your new website opens in a separate browser window.

You now have a website hosted on Amazon S3. This website is available at the Amazon S3 website endpoint.

Step 8: Clean up

If you created your static website only as a learning exercise, delete the AWS resources that you allocated so that you do not accrue charges.


Files you can use to create a generic Web site

Note: The index.html file included below has a slight modification of a web site template provided by H5 up. I have provided the files to help make your exercise more interesting. However, I take no responsibility for the content and provided to you at your own risk! At the time of this writing, the files are free of virus and/or malware.

Exit mobile version
%%footer%%