DEV Community

Cover image for How to Deploy Your Frontend Application on AWS S3
Oyetoke Toby
Oyetoke Toby

Posted on

How to Deploy Your Frontend Application on AWS S3

You can now deploy your frontend application on AWS with S3 static web hosting features.

This is a second article of the How to Deploy Your Frontend Applications series - where I explain how to deploy any frontend applications (react, vue, angular, svelte, static site) to various static site web hosting.

In my previous article, I explained how to Deploy Your Frontend Application on Surge.

So in this tutorial, I will be showing you how to install your frontend application on AWS S3.

AWS S3 is a simple online storage solution known as a bucket with stable, high performance, security and highly scalable service. AWS S3 is designed to make web-scale computing easier for developers.

With the AWS S3 static web hosting feature, you can easily deploy your frontend applications with just two or more clicks right from your S3 bucket.

You don't have to worry about the cost, because AWS' free tier has a very high limit for S3 and its way cheap. Another good thing is S3 is fast and you can choose from data centers you like across the supported globe. You can also add your custom domain easily.

Setting Up AWS S3

Let's see how to set up our AWS account for deployment.

Step 1 - Account

The first step here is to set up your AWS account so we can deploy your frontend application. If you don't have an account already, go create an AWS account here: https://portal.aws.amazon.com/billing/signup. If you have just login to your AWS management console here: https://signin.aws.amazon.com/signin.

Step 2 - Set Up S3

Once you have successfully created an account or you logged in successfully to the AWS management console, the next thing we need to do is set up S3.

On the AWS console, click on Services. There should be a dropdown, look for S3 and click on it.

Screenshot from 2019-12-07 00-46-11.png

This should take you to a page that will show all available buckets. In S3, a bucket is a collection of objects. A bucket is a virtual container while objects are files belonging to that container.

Screenshot from 2019-12-07 00-46-42.png

Click on the Create Bucket to create a new bucket your frontend application will be deployed to. You should see a popup that will prompt for the bucket name and the region you want it to be deployed to.

Screenshot from 2019-12-07 01-00-32.png

Bucket names are usually unique across your AWS account globally. You should also name your bucket name after your domain name. In this tutorial, I will be using react-infinitescrollhook (name of the frontend application I will be deploying).

Once you are done with that, click on Next to move to the Configure tab. There's really nothing to set up in this tab, just click on Next.

On the Set Permission, you will be able to set the permission of the bucket and its objects. You will need to uncheck Block all public access, since we will be deploying on this bucket, we will need the public to access to the built files.

Screenshot from 2019-12-07 01-01-10.png

Once you are done with that, click on Next to review your settings and click on Create Bucket to continue after review your settings.

Screenshot from 2019-12-07 01-02-52.png

Tada! You have successfully created a new bucket. You should see it in the list of buckets.

The next thing now is to enable static web hosting on this and add some settings. Now click on the bucket you just created, you should see something like below:

Screenshot from 2019-12-07 01-12-55.png

Click on Properties tab and select Static website hosting box. This should show a popup. Click on Use this bucket to host a website and it will prompt for an index.html file. You can also add an error file, I am used to using 200.html file. Once you are done with that, click on Save to enable the feature.

Screenshot from 2019-12-07 01-13-12.png

Screenshot from 2019-12-07 01-24-46.png

Ensure you take note of the URL shown in the Static website hosting box because that would be the link to access your application. If you try to navigate to the URL given, for instance, mine, you would get 403 error due to S3 Policies we have not yet set up.

In S3, buckets can have different policies. The policy defines who can do what to which objects in a given bucket.

In the case of deploying your app, you might want to set up your bucket such that you are the only one that can write to it but others can view it.

Screenshot from 2019-12-07 01-33-58.png

To do this, click on the Permission tab and click on Bucket Policy. You should see a text editor on this page where the policies will be saved.

Screenshot from 2019-12-07 01-34-14.png

Policies are designed in JSON documents format. Copy and paste the below policy to the text box and change the placeholder to your bucket's name:

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::react-infinitescrollhook/*"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

And that's all for our S3 setup, you can try opening the link and you would get a 404, which means we only need to deploy our application.

Step 3 - Set Up AWS CLI

If you already have AWS CLI set up on your system or you don't want to set it up, you can skip this and move to the next step.

To install and use the AWS CLI, you would need Python and Pip installed. You can install AWS CLI with the command below:

$ pip3 install awscli --upgrade --user
Enter fullscreen mode Exit fullscreen mode

You can read more about installing AWS CLI on your platform here: https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html

Once you are done with that the next step is to configure it using the below:

$ aws configure
Enter fullscreen mode Exit fullscreen mode

The command above will prompt for some details like AWS access key ID, AWS secret access key, region, and format. The first two are required and these are your account credentials. You can generate new credentials within AWS Identity and Access Management (IAM) if you do not have them already.

AWS Access Key ID [None]: <aws_access_key_id> 
AWS Secret Access Key [None]: <aws_secret_access_key>
Default region name [None]: us-west-1 
Default output format [None]: json
Enter fullscreen mode Exit fullscreen mode

You can read more about setting up your AWS CLI here: https://docs.aws.amazon.com/comprehend/latest/dg/setup-awscli.html

Deploying Your Frontend Application

The next and final step is to deploy your frontend application. There are two methods to do this, the first method is using the S3 Uploader while the second one is using the AWS CLI.

But before that, you will need to have built your frontend application if you are using Frontend Library like React, Vue, Svelte, or Angular. You can do that by running:

$ npm run build
Enter fullscreen mode Exit fullscreen mode

or

$ yarn build
Enter fullscreen mode Exit fullscreen mode

Using The S3 Uploader

Once you are done with that, then you can now upload the build or dist folder.

Now head over to the AWS S3 console, click on the Bucket you are working on and click on Upload.

You should see a pop up where you can upload your build, dist or static file contents. There's a little trick here, you need to drag the contents of the folder, not the folder itself. S3 is expecting you to have the index.html file at the top-level of your bucket. So drag all contents of the folder and click on Upload:

Once your files are uploaded successfully, you should be able to see the files in your buckets.

Tada! Your frontend application is live, you can now preview your application using the bucket's URL.

If you can't find that, fear not you can still get it by double-clicking on the index.html, click on Properties and under Object Url you should see your bucket URL.

Using AWS CLI

Uploading your files every time you add changes is redundant and time-consuming.

However, with the AWS CLI tool, you can reduce the step and deploy it right from your command line. You would need to set it up like I explained above before you can use it.

To deploy using the AWS CLI, all you need is the command below:

$ aws s3 sync <folder_path> s3://<bucket_name>
Enter fullscreen mode Exit fullscreen mode

Now head over to your application directory in the command line and run the command above.

So in my case, it would be:

$ aws s3 sync /build s3://react-infinitescrollhook
Enter fullscreen mode Exit fullscreen mode

Screenshot from 2019-12-10 12-46-00.png

Your application should be deployed now!

Screenshot from 2019-12-10 14-28-47.png

If you are using a frontend library or build system, you can make the deployment process much faster using the NPM scripts command.

Open your package.json, head over to the scripts section and add deploy command:

"deploy": "aws s3 sync build s3://react-infinitescrollhook"
Enter fullscreen mode Exit fullscreen mode

With that, you can now run the below command to deploy your app:

$ npm run deploy
Enter fullscreen mode Exit fullscreen mode

Another thing you can do is to reduce the step in running the build command and deploying the application:

"s3-deploy":"aws s3 sync build s3://react-infinitescrollhook",
"deploy":"npm run build && npm run s3-deploy",
Enter fullscreen mode Exit fullscreen mode

Then with the above command, you can build and deploy your application with just a single command line.

Top comments (9)

Collapse
 
theswaminator profile image
Nikhil Swaminathan

You should check out AWS Amplify to host your static sites instead: console.amplify.aws

Much simpler!

Collapse
 
abinhho profile image
abinhho

I got this error so quite hate Amplify.

List apps call failed
Network Failure

Collapse
 
oyetoket profile image
Oyetoke Toby

I actually plan on writing a series on this.

Collapse
 
karltaylor profile image
Karl Taylor

did you ever get round to doing this? :)

Collapse
 
pinoycurl profile image
Wepinoy

Thanks for the tutorial

Collapse
 
fatih123321 profile image
Ryan Stone

very much highly successfully explained, Thank you Sir.

Collapse
 
fly profile image
joon

Totally forgot how to do this - its been a while. Exactly the post I needed, thank you very much

Collapse
 
njann profile image
Nils

But don't forget monitoring. DDOS can cause a huge amount of costs.

Collapse
 
joydesigner profile image
joydesigner

How about firebase? Seems simpler to host static website:)