Deploy Your Spring Boot & React App with AWS EC2

How to deploy our react or node,Spring boot application using a Amazon web service called EC-2.(Elastic Cloud Compute)

Why you should deploy your application using Amazon EC2 instance.?

It has so many features like, scalability, flexibility, cost-effectiveness, reliability and global reach.

  1. Set up an AWS EC2 instance:
  • Log in to the AWS Management Console. If you haven’t any create an account in AWS official web site.

  • Navigate to EC2 and click on “Launch Instances.”

  • Choose an Amazon Machine Image (AMI) that supports your preferred operating system.

  • If your are a fresher to amazon EC2 you can create instance according to your preferences, like selecting instance type, configure the instance details, adding storage etc.

  • And also configure security groups to allow inbound traffic to your application (e.g., HTTP/HTTPS) as your preferences.

  • Now you have launched your instance successfully and then creating a new key pair or selecting an existing one.

2. Connect to your EC2 instance:

  • Download the created private key (.pem) file for your key pair.

  • To connect your key pair also you can use applications like, filezilla or putty.(if your are a windows user)

  • Set appropriate permissions for the private key file using the following command in the terminal:

chmod 400 /path/to/key-pair.pem
  • Connect to your instance using SSH. For example:
ssh -i /path/to/your-key-pair.pem ec2-user@your-instance-public-ip

3. Configure the EC2 instance:

  • Update the packages on your EC2 instance:
sudo yum update

4. Install Node.js and npm:

  • In here to install Node.js and npm inside your ec2 instance try those following commands.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash source ~/.nvm/nvm.sh nvm install node

5. Clone your React application repository:

  • Install Git if not already installed:
sudo yum install git
  • Clone your React application repository:
git clone <repository-url>

6. Install project dependencies:

  • Reach the cloned repository of yours and install the project dependencies:
cd <repository> npm install

7. Build your React application:

  • Build the React application for production:
npm run build

8. Install a web server:

  • Install a web server to run your React application, such Nginx. You may take advantage of Nginx’s performance, scalability, caching, load balancing, and security features to ensure dependable and effective delivery of your application to customers while using it as a web server for your web app on Amazon EC2.
sudo yum install nginx

9. Configure Nginx:

  • To add configurations into nginx, try this following commands:
sudo nano /etc/nginx/nginx.conf
  • Update the server block to include the following location block:
server {...location / {root/path/to/your/react/application/build;
index  index.html;         
try_files $uri /index.html;}... }
  • Save the file and exit the editor.

10. Start the Nginx server:

  • Start Nginx and enable it to automatically start on system boot:
sudo service nginx start sudo chkconfig nginx on

11. Access your React application:

  • In your Console, find your instance’s public IP address by searching your created ec2 instance.

  • Then browse for your deployed application using that IP address or the domain associated with your EC2 instance.

Hence, deployed your react application using your EC2 instance. Remember to customize the steps according to your specific project requirements and configurations.

Deploy Spring Boot Application on AWS EC2

Steps to Deploy Spring Boot Application on AWS EC2

  1. Create a Spring Boot project using Spring Initializr

  2. Import the project into your favourite IDE

  3. Add a RestController to be able to test

  4. Test the application locally

  5. Prepare the final jar file

  6. Launch an EC2 Instance and keep the key pair handy

  7. Copy jar file to AWS EC2

  8. SSH into the EC2 instance and Install Java 1.8

  9. Run the Spring Boot Jar File on EC2

  10. Allow port 8080 on your Instance security group

  11. Test Your spring boot endpoint deployed on EC2

Step 1: Create a Spring Boot project using Spring Initializr

If you already have a working Spring Boot application, you can go to Step 5 directly. If not let’s create a new spring boot application together

Go to sprint initilizer page and bootstrap a simple spring boot web application.

Open https://start.spring.io/

Step 2: Import the project into your favourite IDE

Navigate to your unzipped project folder and select pom.xml file

Step 3: Add a RestController to be able to test

Add a Rest Controller into our spring boot project so that we can test it.

Click on your project and create a controller class with the endpoint of /test-demo.

package com.surendar.demo.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class TestController {@GetMapping("/testdemo")
public String health() {
  return "Hello & Welcome to Surendar blog!!";
}
}

Step 4: Test the application locally

build the project using-

mvn clean install

run the project locally.

Run as Java Application.

Our application is running locally and you can access it localhost:8080/health

Step 5: Prepare the final jar file

Our application is running locally,prepare the final jar file during the deployment.

Run maven build to package the application as a fat jar.

mvn clean install

Once your build is over , Copy the jar file

Step 6: Launch an EC2 Instance.

Before we deploy our spring boot app, Launch an EC2 instance.

Step 7: Copy jar file to AWS EC2

If you are using a Linux machine you can use scp command out of the box.

Open the terminal and navigate to the directory where you have kept KeyPair and Jar File.

Navigate to the folder containing your jar and keypair.

cd /path/to/folder Prepare the copy command as per the below syntax
scp -i ./DemoKeyPair.pem . <username>@<public-ip or DNS>:/pathwhere/you/needto/copy
scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar ec2-user@ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:~

Set the permissions of your private key file ,If you don’t set the permission then you can not connect to your instance using keypair.

chmod 400 DemoKeyPair.pem

Now permission on your private key is set and you can run your copy command.

scp -i ./DemoKeyPair.pem ./demo-0.0.1-SNAPSHOT.jar ec2-user@ec2-34-240-45-168.eu-west-1.compute.amazonaws.com:~

Step 8: SSH into the EC2 instance and Install Java 1.8

Browser-based SSH connection using EC2 Instance Connect feature , don’t depend on my local machine.

  • Amazon Linux 2

  • Ubuntu 17.04 or later

SSH into Instance using Instance Connect.

1. SSH into your Instance using Instance Connect.

when you are connected to your Instance.

2.SSH into your instance from your Local machine

Command to conect SSH into your instance.

ssh -i path/to/DemoKeyPair.pem ec2-user@ec2-12-34-567-890.compute-1.amazonaws.com

In the case of Windows, you can use putty to SSH into your instance.

Install Java 1.8

sudo yum install java-1.8.0

Step 9: Run the Spring Boot Jar File on EC2

Java is installed in the EC2 instance and our jar file is present in the home directory of the instance.

java -jar demo-0.0.1-SNAPSHOT.jar

Spring Boot application is running on EC2 instance on Port 8080.

Step 10: Allow Port 8080 on Your Instance Security Group

By default, an EC2 instance security group allow only port 22 for SSH access. Edit allow port >>8080.

Click on Your Instance.

Go to Security -> Click on the instance security group.

Check inbound rules and Edit the Inbound rules

Add a rule for type Custom TCP and the port range will be 8080.

Step 11: Test Your spring boot endpoint deployed on EC2 by redirecting to the isntances

Thank you