Skip to main content

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

 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

Comments

Popular posts from this blog

How to use Ngx-Charts in Angular ?

Charts helps us to visualize large amount of data in an easy to understand and interactive way. This helps businesses to grow more by taking important decisions from the data. For example, e-commerce can have charts or reports for product sales, with various categories like product type, year, etc. In angular, we have various charting libraries to create charts.  Ngx-charts  is one of them. Check out the list of  best angular chart libraries .  In this article, we will see data visualization with ngx-charts and how to use ngx-charts in angular application ? We will see, How to install ngx-charts in angular ? Create a vertical bar chart Create a pie chart, advanced pie chart and pie chart grid Introduction ngx-charts  is an open-source and declarative charting framework for angular2+. It is maintained by  Swimlane . It is using Angular to render and animate the SVG elements with all of its binding and speed goodness and uses d3 for the excellent math functio...

Understand Angular’s forRoot and forChild

  forRoot   /   forChild   is a pattern for singleton services that most of us know from routing. Routing is actually the main use case for it and as it is not commonly used outside of it, I wouldn’t be surprised if most Angular developers haven’t given it a second thought. However, as the official Angular documentation puts it: “Understanding how  forRoot()  works to make sure a service is a singleton will inform your development at a deeper level.” So let’s go. Providers & Injectors Angular comes with a dependency injection (DI) mechanism. When a component depends on a service, you don’t manually create an instance of the service. You  inject  the service and the dependency injection system takes care of providing an instance. import { Component, OnInit } from '@angular/core'; import { TestService } from 'src/app/services/test.service'; @Component({ selector: 'app-test', templateUrl: './test.component.html', styleUrls: ['./test.compon...

How to solve Puppeteer TimeoutError: Navigation timeout of 30000 ms exceeded

During the automation of multiple tasks on my job and personal projects, i decided to move on  Puppeteer  instead of the old school PhantomJS. One of the most usual problems with pages that contain a lot of content, because of the ads, images etc. is the load time, an exception is thrown (specifically the TimeoutError) after a page takes more than 30000ms (30 seconds) to load totally. To solve this problem, you will have 2 options, either to increase this timeout in the configuration or remove it at all. Personally, i prefer to remove the limit as i know that the pages that i work with will end up loading someday. In this article, i'll explain you briefly 2 ways to bypass this limitation. A. Globally on the tab The option that i prefer, as i browse multiple pages in the same tab, is to remove the timeout limit on the tab that i use to browse. For example, to remove the limit you should add: await page . setDefaultNavigationTimeout ( 0 ) ;  COPY SNIPPET The setDefaultNav...