Deploy SpringBoot application to AWS EC2

I created a Spring Boot application using Gradle. I wrote about the procedure to deploy this to AWS EC2.

Create an executable jar

Create an executable jar with gradle's bootJar task Reference: https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/gradle-plugin/reference/html/#packaging-executable-and-normal

build.gradle


bootJar {
    launchScript()
}

Then on the command line

$ gradle bootJar

When executed, a jar file will be created in ./build/libs/

Transfer this jar file to EC2

The name of the jar file created this time is WebToDoStarter-0.0.1-SNAPSHOT.jar, and it is assumed that it is placed on Desktop.

Transfer this file to EC2 using sftp

$ sftp -i AWS key path ec2-user@IPv4 public IP

sftp> put Desktop/WebToDoStarter-0.0.1-SNAPSHOT.jar
Uploading Desktop/WebToDoStarter-0.0.1-SNAPSHOT.jar to /home/ec2-user/WebToDoStarter-0.0.1-SNAPSHOT.jar
Desktop/WebToDoStarter-0.0.1-SNAPSHOT.jar                                           100%   20MB   1.2MB/s   00:17   

Access EC2 with ssh

$ ssh -i AWS key path ec2-user@IPv4 public IP

Check if the file has been transferred

$ ls
WebToDoStarter-0.0.1-SNAPSHOT.jar

There is

JDK installation

Try installing java8

$ sudo yum install java-1.8.0-openjdk-devel.x86_64
$ java -jar WebToDoStarter-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.UnsupportedClassVersionError: com/example/demo/WebToDoApplication has been compiled by a more recent version of the Java Runtime (class file version 55.0), this version of the Java Runtime only recognizes class file versions up to 52.0
	at java.lang.ClassLoader.defineClass1(Native Method)
	at java.lang.ClassLoader.defineClass(ClassLoader.java:757)
	at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
	at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
	at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
	at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:419)
	at org.springframework.boot.loader.LaunchedURLClassLoader.loadClass(LaunchedURLClassLoader.java:93)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:352)
	at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:46)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
	at org.springframework.boot.loader.Launcher.launch(Launcher.java:50)
	at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51)

It's an error, apparently the version is old in java8

Class file version and JRE version

Class file version JRE version
49 Java 5
50 Java 6
51 Java 7
52 Java 8
53 Java 9
54 Java 10
55 Java 11
56 Java 12
57 Java 13
58 Java 14

Find a JDK with a higher version than Java 8

$ sudo yum search java
============================================================================ N/S matched: java =============================================================================
~abridgement~
java-1.8.0-openjdk-devel.x86_64 : OpenJDK Development Environment 8
~abridgement~
java-11-amazon-corretto.x86_64 : Amazon Corretto development environment

java-11-amazon-corretto can be used up to java11

Installation of java-11-amazon-corretto

$ sudo yum install -y java-11-amazon-corretto.x86_64

You can't just install it

Switch versions

$ sudo alternatives --config java

There are 2 programs'java'To provide.

Select command

*+ 1       java-1.8.0-openjdk.x86_64 (/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.242.b08-0.amzn2.0.1.x86_64/jre/bin/java)
   2         /usr/lib/jvm/java-11-amazon-corretto.x86_64/bin/java

Press Enter to select the current[+]Or enter the selection number:2   
$ java --version
openjdk 11.0.7 2020-04-14 LTS
OpenJDK Runtime Environment Corretto-11.0.7.10.1 (build 11.0.7+10-LTS)
OpenJDK 64-Bit Server VM Corretto-11.0.7.10.1 (build 11.0.7+10-LTS, mixed mode)

Now you can do it

Tomcat, which is a servlet container for executing Java Servlet, does not need to be installed this time. This is because it is deployed with a jar file that can be executed by the built-in Tomcat.

$ java -jar WebToDoStarter-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.1.RELEASE)

By the way, to change the port number of the application

application.yml


server:
  port: 8081

Add

Recommended Posts

Deploy SpringBoot application to AWS EC2
Deploy your application to WildFly
How to create an application server on an EC2 instance on AWS
Deploy to EC2 with CircleCi + Capistrano
How to deploy a Rails application on AWS (article summary)
How to publish an application using AWS (3) EC2 instance environment construction
[Note] Update to production environment (AWS EC2)
Deploy your application with VPC + EC2 + Docker.
How to deploy
[For beginners] Laravel Docker AWS (EC2) How to easily deploy a web application (PHP) from 0 (free) ①-Overview-
How to deploy a container on AWS Lambda
Deploy Rails to ECS Fargate with AWS Copilot
[For beginners] Laravel Docker AWS (EC2) How to easily deploy Web application (PHP) from 0 (free) ②-Docker development environment construction-
How to install Ruby on an EC2 instance on AWS
Memo to build a Servlet environment on AWS EC2
Steps to deploy to Heroku
How to deploy Java to AWS Lambda with Serverless Framework
Deploy laravel using docker on EC2 on AWS ① (Create EC2 instance)
Deploy RAILS on EC2
Deploy laravel using docker on EC2 on AWS ② (Elastic IP acquisition-linking)
[AWS] Link memory usage of Ubuntu EC2 instance to CloudWatch
Deploy laravel using docker on EC2 on AWS ④ (git clone ~ deploy, migration)
Deploy the application created by Spring Boot to Heroku (public) ②
Deploy the application created by Spring Boot to Heroku (public) ①
How to deploy a kotlin (java) app on AWS fargate
How to deploy Java application to Alibaba Cloud EDAS in Eclipse
How to create an application
[AWS] How to check logs
Deploy Vapor Project to Heroku
Deploy with EC2 / Docker / Laravel
Introducing AWS CLI to CentOS 7
Moving from AWS to PaizaCloud
Introduction to Android application development
How to deploy on heroku
Install docker on AWS EC2
Rewriting from applet to application
Deploy laravel using docker on EC2 on AWS ③ (SSH connection ~ Docke-compose installation)
How to create a web server on an EC2 instance on AWS