Idea: We will build the app’s jar file, then upload the jar file to the s3 bucket. Then download the jar file to the ec2 instance and run the jar file on the instance.
Download java 17 on the instance with the command sudo yum install java-17-amazon-corretto-headless
. Then check the download is successful with the command java -version
!
To generate the app’s jar file, we go to the IDE, open the back end project, access Maven, right-click on install in Lifecycle and select Run Maven Build. !
After creating the app’s jar file, the jar file just created will be in the target folder !
Access S3, access the created bucket, create a new folder named library-app-be
. After creating the folder, we upload the jar file just created by dragging and dropping it into the folder
!
Before downloading s3 to the instance, we need to make s3 public by adding a policy to it:
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::demowebapp-workshop-01/*"
}
]
}
Access the jar file in the bucket and copy the Object URL of it
In the session manager, run the command sudo wget (sudo wget https://demowebapp-workshop-01.s3.ap-southeast-1.amazonaws.com/library-app-be/library-app-be-0.0.1-SNAPSHOT.jar
) to download the jar file to the instance
Run the command java -jar library-app-be-0.0.1-SNAPSHOT.jar
to run the app on the instance
To be able to temporarily test with Postman, we can temporarily assign public-route-table (for the purpose of being able to use IGW) to private-subnet-1 (subnet containing ec2 instance)
Access postman, we can use Public IPv4 address or Public IPv4 DNS to call the APIs
To keep the app running even after terminating the session, we can use nohup command. For example, nohup java -jar library-app-be-0.0.1-SNAPSHOT.jar &
. Then we can close the session and the app will still run.
To stop the app, we can run pkill -f 'java -jar library-app-be-0.0.1-SNAPSHOT.jar'
But we need to make the app automatically run after the instance is rebooted. We will use systemd to do create service for the app (run java app as a service)
sudo chmod +x /home/ec2-user/library-app-be-0.0.1-SNAPSHOT.jar
sudo vim /etc/systemd/system/library.service
Insert the code below into your-application.service:
[Unit]
Description=Java Application as a Service
After=syslog.target
[Service]
User=ec2-user
ExecStart=ExecStart=/usr/bin/java -jar /home/ec2-user/library-app-be-0.0.1-SNAPSHOT.jar
SuccessExitStatus=143
[Install]
WantedBy=multi-user.target
cd /etc/systemd/system/
sudo chmod 777 library.service
sudo nano library.service
sudo systemctl daemon-reload
sudo systemctl enable library.service
sudo systemctl start library.service
sudo systemctl status library.service -l
sudo systemctl stop library.service