loading...
Cover image for Getting Started with Serverless AWS (1/3) - Deploying a Static Website to S3

Getting Started with Serverless AWS (1/3) - Deploying a Static Website to S3

vincenius profile image Vincent Will Updated on ・5 min read
  1. Deploy a Static Website to S3
  2. Distribute your S3 Bucket with Cloudfront and add a Domain
  3. Micro Services using Lambda, DynamoDB & API Gateway

In this series, we're working on a serverless stack using AWS.

In this part, I'll show how to serve a static website through an S3 Bucket and how to deploy from your local machine.

First of all, go ahead and log in or create a new account if you don't have one yet.

For that go to https://aws.amazon.com/.

aws wesbite login top-right

Afterward, go to the Management Console if you're not already there.

Select S3 from the services menu in the top left.

aws where to find the management console
aws s3 service on the top-right menu

Now you should see the S3 bucket overview. S3 stands for "simple storage service", and is a service that lets you store data.

Go ahead and click Create bucket.

aws s3 service create bucket

Now choose a name for your bucket. For the region, choose the one near you.

We'll learn how to decrease the latency for people who are not living near your region using CloudFront in the next part of this series.

As we want to serve a static website from the bucket, it needs to be public.

So uncheck the box for "Block all public access" and remove the checks, which are not related to the ACLs.

aws s3 options for creating a bucket

Click Create Bucket. Afterward, you'll see an overview of all your buckets.

Open the newly created one and go to the Tabs Permissions -> Bucket Policy. Then enter the following code and replace your-bucket-name with the name of your bucket. Then hit save.

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

aws s3 bucket policy

This will allow public access to your bucket.

Now we need to enable static web hosting. For this go to the tab Properties and click the Box Static website hosting. There you only need to specify the index document.

I will use index.html for that, as this will be the entry point for my website.

You can also specify below, which page will be shown when errors occur.

aws s3 bucket enable website

If you now click on the endpoint of your bucket, you'll see a 404 Not Found error. This is because we haven't uploaded our index.html file yet.

I'll use the following file to verify that everything is working.

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="UTF-8">
        <title>S3 Website</title>
      </head>
      <body>
        This website is served from S3!
      </body>
    </html>

So let's go back to the "Overview" Tab and click "Upload".

aws s3 upload file to bucket

Select your file and click upload on the bottom left.

There is no need to change the permissions and properties for this file, as the defaults are fine.

aws s3 upload settings

Now you should be able to see the content of your index.html when you open the link of your bucket.

If you don't have the link anymore, you can click the index.html in the overview and it will show you the "Object URL".

aws s3 url of uploaded index.html

Setting up IAM and Deploying to S3 from your CLI

First, you need to get an Access key ID and Secret access key to get programmatic access to your AWS account.

For that go back to your management console and go to the service IAM.

aws where to find IAM

Amazon IAM (Identity and Access Management) is used to manage users and their permissions.

Click on Users in the left menu and then on Add user

aws iam add user

We want to create a user, which is able to do everything - so I call it admin. Set the check for Programmatic access.

Afterward, click Next: Permissions.

aws iam user create settings

As a next step, we need to grant that user the required permissions.

So go to Attach existing policies directly and select AdministratorAccess.

aws set permissions for IAM user

You can skip the next tag of setting tags and straight create the user.

In the last step, you'll see your Access key ID and Secret access key.

Note them somewhere or keep this page open.

aws find keys for new user

To deploy via CLI you need to install the aws-shell. Go to their repository and follow the installment instructions:

https://github.com/awslabs/aws-shell

After the installation, type

aws configure

It will ask you for your access key and your secret. Use the ones we've just created.

You can leave the Default region name and the Default output format blank.

Now you should be able to deploy to S3 via command line. In your console, navigate to the directory where your files are. To upload your files type:

aws s3 sync ./ s3://your_bucket_name

Where ./ is the path and your_bucket_name is the name of your bucket.

Now you should see a success message for all the files that have been uploaded in the console. The files should be available in your bucket.

For uploading websites through the aws s3 sync command it makes sense to add the --delete argument. This will delete all files on s3, which are not in your source directory.

For example, I'd use the following command to upload the export of a next.js app.

aws s3 sync ./out s3://your_bucket_name --delete

Congratulations you now know how to serve a website to S3 and how to upload to an S3 bucket with the aws-shell.

In the next part of this series, we will distribute our S3 bucket through Cloudfront and add a domain.

Posted on Jun 15 by:

vincenius profile

Vincent Will

@vincenius

A full-stack JavaScript developer and maintainer of wweb.dev

Discussion

markdown guide
 

That looks great, I am looking forward to read your part 2 and 3.

 

Nice start, looking forward to more. In the first aws-shell command, looks like it should be:
aws configure

not:
aws configuration

 

Thanks for the hint - it's fixed

 

Check out @static/discharge which automates almost everything related to publishing a static site on s3 and cloud front.

github.com/brandonweiss/discharge

 

This looks really cool - thanks for the recommendation :)

Nevertheless, I think it makes sense to know what's happening in the background. But that looks definitely like a good way to speed things up

 

Yes, I agree. Understanding what happens behind the scenes is very important.
There are issues you might run into especially with SSL certificates, cache invalidation etc...

 

Cool. Would be nice if you also explain why the one should deploy his static website to S3 instead of some shared hosting.

Thanks!

 

Good point - mostly because it's way cheaper.
I think it's also easier, as I don't have to deal with the server stuff, but that's just my personal opinion :)