DEV Community

Cover image for Deploying a Java Spring Application on AWS EC2
Sudarshan Jadhav
Sudarshan Jadhav

Posted on

Deploying a Java Spring Application on AWS EC2

Configure AWS Resources

1. Create an EC2 instance and select the necessary configurations based on the requirements of your applications.

2. Configure security group as follows:
Click on "Edit inbound rules" or "Add rule" to add the following rules:

  1. HTTP (Port 80)
  2. HTTPS (Port 443)
  3. SSH (Port 22)
  4. Tomcat (port 8080 )
  5. MySQL (port 3306)

Configure MySQL

Wherever your MySQL database resides, configure the mysqld.cnf file to specify the bind address for EC2, create a user, grant them all privileges for CRUD operations, and ensure the password is secure.

Configure Java Application

Configure Spring applications application.properties/yml files to utilize the public IP of the EC2 instance and MySQL database username and password along with database name.

Connect to the instance :

Using either a web console or SSH using the command below.
ssh -i <path_to_private_key><username>@<public_ip_or_dns_of_ec2_instance>

Download and Install JDK :
1.For Amazon Linux 2023

sudo yum update -y
sudo yum install java-17-amazon-corretto -y
Enter fullscreen mode Exit fullscreen mode

2.For Ubuntu

sudo apt update -y
sudo apt install openjdk-17-installer -y
Enter fullscreen mode Exit fullscreen mode

3.For Windows

1.Install chocolatey package manger 

Set-ExecutionPolicy Bypass -Scope Process -Force; [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))

2.Install using chocolatey
choco install -y jdk17
Enter fullscreen mode Exit fullscreen mode

Make a build file (jar) of a java Application

1.Using Gradle
gradle build

2.Using Maven
mvn clean package

3.Using Editor 
You can easily make build file using Intellij or sts tool 
Enter fullscreen mode Exit fullscreen mode

Copy a jar file To some location

  1. Copy a jar file to S3 bucket

  2. Copy jar file from local machine to remote :

scp  -i {PATH_TO_YOUR_PRIVATE_KEY} {LOCAL_JAR_PATH} {USERNAME}@{REMOTE_SERVER_IP}:{REMOTE_JAR_PATH}
Enter fullscreen mode Exit fullscreen mode

3.Push Code To Github and then by cloning repository make
jar file in ec2 instance.

By using any of the method copy a file to ec2-instance

Deploying a java Application

 1. Normal deployment:
java -jar /path/to/your/application.jar

 2. Background deployment (with nohup):
nohup java -jar /path/to/your/application.jar &

 3. Creating a systemd service:
sudo tee /etc/systemd/system/myapp.service << EOF
[Unit]
Description=My Spring Boot Application
After=syslog.target

[Service]
User=your_username  # Replace with the appropriate user
ExecStart=/usr/bin/java -jar /path/to/application.jar
SuccessExitStatus=143

[Install]
WantedBy=multi-user.target
EOF


> sudo systemctl start myapp
> sudo systemctl enable myapp

Enter fullscreen mode Exit fullscreen mode

*Thank You 😊 *

Top comments (0)