Step-by-Step Guide to Deploying a Spring Boot Application on AWS EC2 with Best Practices
This article explains how to launch a Spring Boot application on AWS EC2, a versatile cloud platform for hosting your apps. Whether you're working on a small project or managing large-scale deployments, AWS EC2 offers scalability, control, and a variety of integration options for your Spring Boot apps.
Published Dec 28, 2024
Features and Benefits of Deploying on EC2:
- Customisable Environment: EC2 allows you to modify the environment to your application's exact requirements, such as installing dependencies or changing application parameters.
- Scalability: AWS EC2 instances may be quickly scaled up or down to meet your traffic demands, ensuring performance efficiency.
- Cost optimisation is paying only for your resources and utilising AWS's pricing structures, such as spot instances, to cut expenses.
Setting Up an EC2 Instance for Spring Boot:
Launching an EC2 instance
1. Access the AWS Management Console and search for EC2.
2. Click "Launch Instance" to configure the following:
- Select the suitable instance type for your application needs. T2 or T3 instances are more economical for modest to medium workloads.
- For Java compatibility, choose an operating system such as Amazon Linux AMI or Ubuntu.
Configuring Security Groups
Set up security groups to allow traffic into your application.
For a Spring Boot app running at port 8080:
- Allow inbound communication to port 8080 (HTTP).
- Enhance security by restricting access to other ports.
Deploying the Spring Boot Application
1. Preparing the Application
sample project: https://github.com/CDinuwan/JavaSpringbootAppDeploy.git
You should alter your pom file if you want to acquire a suitable jar name.
Then run the following command:
- Connecting to the EC2 Instance
Use SSH to connect to your EC2 instance:
Update the instance and install Java:
Deploying the JAR File
Option 1: Using AWS S3
- Upload the JAR file to an S3 bucket as part of your CI/CD pipeline.
- Use the AWS CLI on the EC2 instance to download the JAR file:
Option 2: Using JFrog Artifactory
If your organization uses JFrog Artifactory for artifact management, retrieve the file using curl or get:
Run the application:
Enhancing the Deployment:
1. Setting Up a Reverse Proxy
Use NGINX or Apache to route traffic to your Spring Boot application:
Install NGINX:
Configure NGINX to forward requests to port 8080.
2. Enabling Auto-Restart
Create a systemd service file to ensure the application restarts automatically:
Add the following:
Start and enable the service:
Best Practices for Deployment:
- Use AWS CloudWatch for monitoring application logs and performance.
- Deploy the application behind an Elastic Load Balancer (ELB) for increased availability.
- To ensure security, use IAM roles and encrypt critical data.
What You Can Do with an EC2-Deployed Spring Boot Application:
- Use AWS tools to monitor resource utilisation and expenses.
- Scale the application dynamically to handle traffic spikes.
- Proactively handle performance issues by setting custom notifications.
Using EC2, deploying Spring Boot apps becomes more efficient, scalable, and managed.
Happy coding 🚀