How to deploy a spring boot application (.jar) on EC2:
This is the way of using AWS as IaaS(Infrastructure-as-a-service).
Login to the ec2 instance from Putty. The universal username is: ec2-user
- First check whether java is installed on ec2 by executing: java -version.
- If its not installed, install it by executing sudo yum install java-1.8.0 command to install the open jdk.
- Then using WinSCP copy paste the .jar file to the root folder.
- Execute the command: java -jar <application-name>.jar
Thats it !… Your application is running.
If this would be the URL of your endpoint in local machine: http://localhost:8080/app1/api/test1
Then you have to replace the “localhost” with the machine name like below:
http://ec2-13-233-102-65.ap-south-1.compute.amazonaws.com:8080/app1/api/test1
http://13.233.102.65:8080/app1/api/test1
P.S:
1. Once you stop the ec2 instance, the application will also stop. Although java and .jar file will be there.
2. After you start the ec2 instance again you need to run the .jar again to run the application.
3. The IP address will be different now.
4. Using this way of deployment it doesn’t create a S3 bucket for keeping the .jar file.
How to deploy a Spring boot application (.jar) to EC2 by AWS Elastic Beanstalk ?
This is the way of using AWS as PaaS (Platform-as-a-service).
Elastic Beanstalk has the AWS Elastic Load Balancer in it as well.
Here you can use the same above .jar file that you used in the above example.
Search for Elastic Beanstalk.
Click “Create Application”.
Give a name.
Tags are not required.
Choose Java as Platform. (if you have to uploaded a .war file then you need to choose Tomcat as Platform)
Choose Java 8 running on Amazon Linux.
Choose Upload your code.
Choose the .jar file from local machine.
Hit “Create Application”.
In the background it will create an ec2 instance and s3 bucket for the same. S3 will hold the .jar file.
It will take some time to launch.
Once the application is up you will see the below screen:
Once you hit the URL:
Add the below param in the configuration -> software
SERVER_PORT 5000
As we know the default port of a springboot application runs in 8080, similarly, the springboot application runs in port 5000 in Elastic Beanstalk EC2 instance.
The same can also be added in application.properties file of the springboot application:
server.port=5000
Wait for the server to up.
Then hit again:
It seems now the application is up. You need to hit the right endpoint. Add /app2/api/test1 to the end of the URL.
http://app22-env.eba-g2mzf55d.ap-south-1.elasticbeanstalk.com/app2/api/test1
That’s it !!…