How to Host Your Static Site on AWS S3
So, you have your blog as a static site. In a previous post we deployed our site using Netlify. In this post we are going to discuss how you can host your site on the cloud using the AWS S3 storage service.
Login or Create an AWS Account
Create an Account or Login
Follow the next steps to Login or Create a new AWS account:
- Go to the AWS home page.
- Click the Create an AWS Account button.
NOTE: If you already have an account, click Sign in to the Console and skip the rest of the steps. - Provide your email address, password, and account name, and click Continue.
- Continue with the registration process providing the required information.
- Add a payment method. They won’t charge you anything if you choose the free tier plan, which is free for the first year for you to explore the different services.
- Verify your account with your phone number.
- Choose a Support Plan.
- You receive a confirmation email, which means your account is active now.
Create an S3 Bucket
The first step on the process is to create an AWS S3 bucket, which will store the static files of your website.
- On the AWS Management Console, find the S3 service by typing ‘S3’ on the Find services search box or by navigating through the Services menu on the upper navigation bar.
- Click the + Create bucket button.
- Enter a Bucket name in the text field.
IMPORTANT: Your bucket name must be unique, and preferably DNS-compliant. See the naming restrictions for more information. Something like “www.your-awesome-site.com” should work as long as the name you choose is available. - Choose your region. For this example we’re using
us-east-1
. - Click Next.
- Leave the next page as default (all boxes unchecked) and click Next.
- In the next page, in the Public access settings for this bucket section, uncheck all boxes.
In the Manage system permissions section, choose Grant Amazon S3 Log Delivery group write access to this bucket, and click Next. - Click Create bucket.
Configure your S3 Bucket for Static Website Hosting
Configuring your bucket for hosting static websites is really simple. Follow the next steps to do so:
- Open the S3 bucket you just created by clicking its name on the AWS management console.
- Click the Properties tab on the upper navigation panel.
- Click the Static website hosting option.
- Check the Use this bucket to host a website option.
- Type
index.html
in the Index document field. You can leave the Error document field black or accept the default value,error.html
. - Click Save.
- Click the Permissions tab on the upper navigation panel.
- Click the Bucket Policy button.
- Copy the following policy and paste it in the corresponding text field:
{ "Version": "2008-10-17", "Id": "PolicyForPublicWebsiteContent", "Statement": [ { "Sid": "PublicReadGetObject", "Effect": "Allow", "Principal": { "AWS": "*" }, "Action": "s3:GetObject", "Resource": "arn:aws:s3:::www.your-awesome-site.com/*" } ] }
- Replace
www.your-awesome-site.com
with the name of your own bucket. - Click Save.
You now have everything set up for hosting a static site on AWS S3. To see the URL of your website,
- Click the bucket name you just created to open it.
- Click the Properties tab on the navigation bar.
- Click the Static website hosting tab.
The URL is the indicated Endpoint.
But there’s still nothing on your bucket to display. Let’s add our files to the bucket so we can see our site online. There are two options to do this:
- Upload the files directly to the bucket through the Console, or
- Use the Amazon Command Line Interface (CLI) to sync the
build
with aDeploy
.
The following sections explain the two different approaches.
Upload your Files to the S3 Bucket
Follow these steps to upload your site to the S3 bucket. For this example we are going to use the Gatsby site we created in the previous post.
- On a terminal, navigate to your site directory and run
gatsby build
NOTE: This command can change depending on the build command of the framework you’re using,npm build
is a possible option. - Open your S3 bucket by clicking its name on the AWS management console.
- Click the Upload button.
- Select all the files created with the
build
command and drag them to the Upload page on the AWS Console. - Click Next.
- In the Manage public permissions section, select Grant public read access to this object(s), and click Next.
- Leave the default Standard option selected on the Set properties page and click Next.
- Click Upload and wait for the uploading processs to finish.
- Verify that your site is live visiting your URL http://www.your-awesome-site.com.s3-website-us-east-1.amazonaws.com/.
Use the Amazon CLI to Sync the build
with the a Deploy
to S3
Deploying your site to S3 with the Amazon CLI is as easy as running a command in your Terminal. But first you need to install the CLI.
- Follow the instructions for Installing the AWS CLI.
NOTE: Follow the instructions corresponding to your working environment. - Follow the steps to Configure the AWS CLI.
- Run the following command inside your project directory to start the
build
process anddeploy
to your S3 bucket:gatsby build && aws s3 sync public/ s3://www.your-awesome-site.com
.
NOTE: The command can be different depending on your build command, it can useyarn run build
ornpm run build
. Also, yourbuild
directory can bebuild
, or like in this casepublic
. Make sure to use the correct directory. An alternative command would look something like this:npm run build && aws s3 sync build/ s3://<bucket-name>
.