Deploying Front-End Apps To The Amazon Cloud
Published: 2023-10-16 09:13:09

by [Ebenezer Igbinoba](https://blog.openreplay.com/authors/ebenezer-igbinoba)

<blockquote><em>
Learn how to push your front-end code to the cloud, by following all the steps carefully set out in this article.
</em></blockquote>

<div style="background-color:#efefef; border-radius:8px; padding:10px; display:block;">
<hr/>
<h3><em>Session Replay for Developers</em></h3> 
<p><em>Uncover frustrations, understand bugs and fix slowdowns like never before with <strong><a href="https://github.com/openreplay/openreplay" target="_blank">OpenReplay</a></strong> — an open-source session replay suite for developers. It can be <strong>self-hosted</strong> in minutes, giving you complete control over your customer data.</em></p>
<img alt="OpenReplay" style="margin-top:5px; margin-bottom:5px;" width="768" height="400" src="https://raw.githubusercontent.com/openreplay/openreplay/main/static/openreplay-git-hero.svg" class="astro-UXNKDZ4E" loading="lazy" decoding="async">
<p><em>Happy debugging! <a href="https://openreplay.com" target="_blank">Try using OpenReplay today.</a></em><p>
<hr/>
</div>


There has recently been an increase in the quantity of applications being created and "pushed" to the cloud. This article offers a thorough guide on how developers can deploy their front-end applications to a cloud service provider. The steps that may be done to successfully deploy a frontend app to an AWS EC2 instance will be detailed in detail in this post, which will use AWS as the cloud provider. We will be utilizing AWS CodeBuild and CodeDeploy tools to set up our CI/CD pipeline that will automate the deployment process for our application.

Instead of launching a "hello world" app to start learning continuous integration and continuous delivery (CI/CD) via AWS CodePipeline, I constructed a react app that retrieves movies, their IMDB rating, and other information from the open movie database using the OMDB API.

The setup of your AWS CodePipeline and the deployment of your frontend project to an AWS instance are the main topics of this post. I'll be using a ReactJS application to demonstrate for demonstration. We'll assume you already have an IAM user set up, an AWS account, and a frontend application on GitHub that's prepared for deployment.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image01.png)

For ease of browsing, this article has been broken up into sections. We will discuss the following subjects in this article:

1. Introduction
2. Create an IAM Role for EC2 and AWS CodeDeploy
3. Launch an EC2 instance
4. Create a code pipeline using Github, CodeBuild and CodeDeploy
5. Access your frontend app on EC2's public DNS
6. Final thoughts

## 1. Introduction
React is a declarative, effective, and adaptable JavaScript library for creating user interfaces is ReactJS. It is a front-end component-based open-source library that is only in charge of the application's view layer.

Based on the V8 JavaScript engine in Chrome, Node.js is a JavaScript runtime. The event-driven, non-blocking I/O mechanism used by Node.js makes it quick and effective.

The most complete and widely used cloud platform in the world, Amazon Web Services (AWS), provides over 175 fully functional services from data centres across the world. AWS is being used by millions of users to save expenses, increase flexibility, and accelerate innovation. Some of the key services offered by AWS include computing, storage, databases, migration, networking and content delivery, management tools, security & identity compliance, and messaging.


## 2. Create IAM Role for EC2 and AWS CodeDeploy
An AWS service needs permissions to access AWS resources, and those permissions are granted via AWS service roles. Which AWS resources the service can access and what it can do with those resources are determined by the policies that you attach to the service role.

Let's search for the IAM service in our AWS Management Console. Now, make a new role for AWS CodeDeploy and EC2:

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image02.png)

Identity and Access Management (IAM) — Roles Dashboard

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image03.png)

Create a new role for EC2 and attach the AmazonS3ReadOnlyAccess policy, which will allow our EC2 instance to access stored artifacts from the Amazon S3 bucket.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image04.png)

Make a new service role for CodeDeploy and add the AWSCodeDeployRole policy, which will provide our service role the rights to read the tags on our EC2 instance, publish data to Amazon SNS topics, and much more.

## 3. Launch an EC2 instance
Let's now start up our EC2 instance! Clicking on **EC2** under **Compute** in the AWS Management Console will lead us to the **EC2** Dashboard page. Click ***Launch Instance***.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image05.png)

EC2 Dashboard

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image06.png)

We will choose Amazon Linux 2023 AMI as our Amazon Machine Image(AMI) for launching our instance.

Choose an Instance Type

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image07.png)

Additionally, select the IAM role with the AmazonS3ReadOnlyAccess policy that we previously defined for EC2.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image08.png)

The storage device parameters mentioned above will be used to launch our instance. You can modify the root volume's settings as well as add more EBS volumes and instance store volumes to your instance.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image09.png)

You can optionally add your own metadata in the form of   [tags](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Using_Tags.html) tags to each resource to help you manage your instances, photos, and other Amazon EC2 resources.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image10.png)

On this page, you can add firewall rules to allow specific traffic to reach your instance.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image11.png)

You will now be prompted to check the configuration of your [EC2 instance](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/ec2-instance-connect-methods.html) and generate a key pair so that you may connect to it. Click Download Key Pair to download the .pem key to your computer using an existing key or a new key pair you've created. Select Launch Instances after downloading Key Pair, then wait for your instance to launch. The instance will now be running in a few minutes when you click View Instances to go back to your EC2 dashboard.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image12.png)

Here we go; your EC2 instance has now been successfully launched.

<CTA_Middle_Basics />



## 4. Create a CodePipeline using Github, CodeBuild and CodeDeploy
You may automate the release process for your app or service using the continuous integration and continuous delivery (CI/CD) AWS solution called [CodePipeline](https://aws.amazon.com/codepipeline/). CodePipeline automatically builds, tests, and deploys your code depending on the release process models you establish when initializing your CodePipeline each time you commit a code update to your source (GitHub, AWS CodeCommit, etc.). You are now able to offer features and updates with speed and dependability.

Step 1: CodePipeline

Step 2: Code Source (CodeCommit or Github)

Step 3: CodeBuild and Build Specification (buildspec.yaml) File

Step 4: CodeDeploy and Application Specification (appspec.yml ) File

Step 5: Review

### Step 1: CodePipeline
Let's use the AWS Management Console to access CodePipeline and select Create pipeline.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image13.png)

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image14.png)

This page will also establish a new service role for your new pipeline. To store artefacts, we may either let AWS establish a new S3 bucket, or if you'd prefer to use an already-existing S3 bucket in the same region, choose "Custom location" and then your preferred S3 bucket.

### Step 2: Code Source (CodeCommit or Github)
AWS offers five ways, as of August 2023, to supply the pipeline's source code: [AWS CodeCommit](https://aws.amazon.com/codecommit/), [Amazon ECR](https://aws.amazon.com/ecr/),[ Amazon S3](https://aws.amazon.com/s3/), [Bitbucket Cloud (beta)](https://bitbucket.org/product), and [Github](https://github.com/). GitHub and AWS CodeCommit are rather comparable; in this post, we will combine GitHub and AWS CodePipeline.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image16.png)

Click the Connect to GitHub button after choosing GitHub as the source provider. After that, you'll be asked to input your GitHub login information.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image16.png)

You can choose a repository and branch for CodePipeline to use when uploading commits to this repository to your pipeline after granting AWS CodePipeline access to your GitHub repository.

### Step 3: CodeBuild and Build Specification (buildspec) File
The build provider definition will now be carried out. [AWS CodeBuild](https://aws.amazon.com/codebuild/) and [Jenkins](https://www.jenkins.io/) are the two possibilities for build providers as of August 2023. AWS CodeBuild is a fully managed continuous integration service that assembles source code, conducts tests, and generates deployable software packages. Jenkins is a Java-based open-source automation platform with plugins created for continuous integration needs. Check out this article comparing [Jenkins CI Server vs. AWS CodePipeline](https://www.sumologic.com/blog/aws-codepipeline-versus-jenkins-ci-server/) if you're unsure which one to utilize.

For the purpose of this tutorial, we will choose AWS CodeBuild as our build provider, and we will be prompted to choose a project:

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image17.png)

If, like me, you didn't create a project before constructing your pipeline, you may do so right now by selecting Create project, which will take us to the next page:

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image18.png)

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image19.png)

For AWS, a CodeBuild service role will be developed. AWS services that depend on CodeBuild are interacted with by CodeBuild on your behalf.

We will now quickly go through what a [buildspec](https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html) file is. CodeBuild employs a buildspec file, which is a collection of build commands and associated settings in YAML format. I made a buildspec.yaml file for my project and placed it in the root of my project directory:

**buildspec.yaml**
```yaml
# Do not change version. This is the version of aws buildspec, not the version of your buldspec file.
version: 0.2
phases:
 pre_build:
   commands:
     #installs dependencies into the node_modules/ directory
     - npm install
 build:
   commands:
     - echo Build started on `date`
     - echo Compiling
     - npm run build
 post_build:
   commands:
     - echo Build completed on `date`
# Include only the files required for your application to run.
artifacts:
 files:
   - public/**/*
   - src/**/*
   - package.json
   - appspec.yml
   - scripts/**/*
```

If our project directory doesn't contain a buildspec file, we may still store build commands by choosing Insert build commands and providing the required commands. Let's click Continue to CodePipeline after we're finished.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image20.png)

We will return to this page once we have successfully generated a build project. Let's proceed to Step 4 now: By selecting Next, add the Deploy stage.

### Step 4: CodeDeploy and Application Specification(appspec.yml) File

Software deployments to a range of computing services, including Amazon EC2, AWS Fargate, AWS Lambda, and your on-premises web server, are automated via the completely managed deployment solution [AWS CodeDeploy](https://aws.amazon.com/codedeploy/). During the deployment process, you can update your application with little to no downtime. We will utilize AWS CodeDeploy as our deployment provider for this article.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image21.png)

An application in CodeDeploy is nothing more than a designation or container that the software uses to guarantee that the right revision, deployment configuration, and deployment group are referred to during a deployment. Therefore, let's start an application! If you look at the screenshot above, under Deploy in the left-hand column, right-click on Applications and choose "Open Link in New Tab." Click Create application once you're on the Applications page.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image22.png)

Create an application for CodeDeploy.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image23.png)

Here, we'll need to construct a deployment group for our application that details various deployment choices as well as the instances to which your application revisions are deployed.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image24.png)

We will choose the CodeDeployRole we previously generated with the AWSCodeDeployRole policy connected for the Service role.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image25.png)


We will solely choose Amazon EC2 instances for our environment configuration. We will enter the values of the key-value combination we used to tag the instance previously when we created our EC2 instance in the Key and Value fields.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image26.png)

During CodeDeploy deployments, a[ load balancer](https://docs.aws.amazon.com/codedeploy/latest/userguide/integrations-aws-elastic-load-balancing.html) stops internet traffic from being forwarded to instances that are either not yet available or are in the process of being deployed. However, the load balancer's precise function is determined by whether it is utilized in an in-place deployment or a blue/green deployment. We won't configure a load balancer for this post; instead, we'll create a deployment group.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image27.png)

To check our pipeline, let's return to the Add deploy stage browser tab and click Next after selecting the Application name and Deployment group that we just built.

If you don't see the name of your application or the deployment group you just formed, DO NOT reload your browser tab. The textbox for each will delete all of the information you've so far input for your pipeline if you click on it. Instead, click Next to return to the Add deploy stage after returning to the Add build stage by clicking Previous. The Application name and the Deployment group you just made can now be chosen.

We'll briefly talk about the application specification file here. An AppSpec file, which CodeDeploy uses to handle deployment and specify what should be installed on your instances from your application revision in Amazon S3 or GitHub, as well as which lifecycle event hooks to execute in response to deployment lifecycle events, is formatted in YAML or JSON. For further details, see the EC2/On-Premises deployment AppSpec example and the AWS CodeDeploy user guide.

The appspec.yml for my application is as follows:

**appspec.yml**

```yaml
# This is an appspec.yml template file for use with an EC2/On-Premises deployment in CodeDeploy.
version: 0.0
os: linux

files:
 - source: /
   destination: /home/ec2-user/server

permissions:
 - object: /
   pattern: "**"
   owner: ec2-user
   group: ec2-user

hooks:

 BeforeInstall:
   - location: scripts/before_install.sh
     timeout: 300
     runas: root
 
 AfterInstall:
   - location: scripts/after_install.sh
     timeout: 300
     runas: root

 ApplicationStart:
   - location: scripts/app_start.sh
     timeout: 300
     runas: root
```

### Step 5: Review
After reviewing it, click Create pipeline.

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image28.png)

My deployment initially failed!!! ❌

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image29.png)

There are various causes for why you can have this problem. Because I hadn't set up the CodeDeploy agent on my EC2 instance, my deployment failed for me. When installed and set up on an instance, the CodeDeploy agent is a software package that enables that instance to be utilized in CodeDeploy deployments. An in-depth post on [troubleshooting EC2/On-Premises Deployment Issues](https://docs.aws.amazon.com/codedeploy/latest/userguide/troubleshooting-deployments.html) may be found here.

Following the instructions in this article, connect to your EC2 instance, and then install or reinstall the CodeDeploy agent on your EC2 instance:update and install Ruby using 

```
sudo yum chmod +x ./install 
sudo ./install auto https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install 
sudo yum install wget
```

You will see that all six events have the status "succeeded" if everything goes according to plan during your deployment stage:

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image30.png)

Several times, my deployment failed, and I lost a lot of time reading the AWS documentation and searching Google for problems and fixes. The best part then emerged:

![-](https://blog.openreplay.com/images/deploying-front-end-apps-to-the-amazon-cloud/images/image31.png)

Congratulations! 🎉🎉🎉 Your application was successfully deployed to EC2 using AWS CodePipeline.

## 5. Access your application on EC2 Public DNS
Your EC2 instance can be accessed through the EC2 Public DNS (IPv4), which can be found in the EC2 Management Console's Description section and looks something like this: ec2-3-81-44-251.compute-1.amazonaws.com. Since our ReactJS app by default uses port 3000, copy/paste your public DNS (IPv4) address into a new tab and add port:3000 at the end. You'll see that this won't work because the browser connection will time out, and nothing will display.

This is because we haven't made port 3000 accessible to the general public. The Security Group can help in this situation. Go to Security Groups on the EC2 Dashboard page, choose your EC2 instance security group (launch-wizard-1, launch-wizard-2, or something similar), and modify the Inbound rules.

You can now access the EC2 Public DNS at a URL similar to  ec2-3-81-44-251.compute-1.amazonaws.com:3000 to view the content of your app.

## 6. Final thoughts
You can host and deploy your web application on AWS in a number of different ways. I selected EC2 so that I could learn about the infrastructure's components, and I selected AWS CodePipeline so that I could learn about the continuous integration and continuous delivery (CI/CD) workflow that enables us to create and manage the process of creating, testing, and deploying applications into a test/production environment using CodeBuild and CodeDeploy.

Thank you for reading!