This article is about installing a Spring Boot Application on AWS using this technology stack:
EC2 to run the Java program
systemctl to control start/stop/restart the application
Configuration of the Spring Boot application using environment variables
We start from the assumption that a jar file has been created and is available on AWS S3 or some other artifact repository.
Step 1: Create the EC2 Instance
Prerequisites:
AWS account
1 VPC - dev-vpc
1 subnet - dev-apps-subnet
I created an EC2 instance of size small with an Amazon Linux image.
In that instance, I created a folder for the application and copied the jar file genova-1.0.jar containing the Spring Boot application to that folder.
Step 2: Configure the Process to Run the Java Application
SystemD is a Linux facility that automatically starts, restarts, and stops processes.
In this case, it consists of a file in /etc/systemd/system:
Also, it is possible to configure other environment variables in the file genova.conf:
Because we want to use the same approach on multiple deployments (EC2, Docker, Elastic Beanstalk), we will remove the configuration from the .conf file and move it all to the .service file. It is shown in this article as an option.
Commands used:
sudo systemctl daemon-reload - reload the configuration
sudo systemctl start genova.service - start the service
sudo journalctl -u genova.service - list recent log entries
Step 3: API Gateway Configuration
The EC2 instance is in a private subnet for security purposes and is inaccessible directly over the public Internet. To provide access to the application, we used the AWS API Gateway, which acts in this case like a load balancer: