Deploying a Java Spring application on Amazon EC2 involves several steps. Here’s a step-by-step guide to help you through the process:
1. Prepare your spring application:
Make sure your Java Spring application is fully developed, tested, and ready to deploy. Package it into a usable format such as a JAR file or a WAR file.
2. Create an EC2 instance:
Log in to the Amazon Web Services (AWS) console, go to the EC2 dashboard and click “Launch Instance”. Choose an Amazon Machine Image (AMI) that suits your requirements (eg Amazon Linux, Ubuntu). Select the instance type, configure security groups, and generate a new key pair or use an existing one to securely access the instance.
Edit inbound rule in security group and allow port 443, 80, and 22 as shown in image below
3. Connect to your EC2 instance:
After you launch an EC2 instance, you can connect to it using Secure Shell (SSH) from your local computer. Use the key pair generated during instance creation to establish a secure connection. The exact SSH command depends on your local operating system.
If you are connecting with local machine the run following command, in command line terminal
ssh -i "your-pem-key.pem" ec2-user@ec2-ip-address
4. Install the required software on the EC2 instance:
After connecting to your EC2 instance, install the necessary software, including the Java Development Kit (JDK), Apache Maven (if available), and any other dependencies your Spring application requires.
5. Transfer your spring application to EC2:
Use the Secure Copy (SCP) command or tools like WinSCP to transfer your packaged Spring application (JAR or WAR file) from your local machine to an EC2 instance.
Run the below command in your local system to copy your-application.jar file to ec2 machine
scp -i "/path/key-pair-name.pem" /path/your-application.jar ec2-user@instance-IPv6-address:~
6. Run Spring Application:
Once your application is on the EC2 instance, navigate to the directory containing the application and use the appropriate commands to run it. For example, if it is a JAR file, use
java -jar your-application.jar. Make sure your application is running correctly and that all required ports are available.
add the port on which your application is running in inbound rule as above
7. Set up reverse proxy (optional):
You can set up a reverse proxy like Nginx or Apache HTTP Server on your EC2 instance for better security and to allow access via domain name. Configure the proxy to forward incoming requests to the running port of your Spring application (eg 8080).
8. Firewall and Security Group Configuration:
Adjust the security group settings of the EC2 instance to allow access to the desired ports (eg 22 for SSH and 80/443 for HTTP/HTTPS if you are using a reverse proxy). Keep your firewall and security group settings secure to limit access to only necessary ports. as mentioned in 2nd image
7. Domain name and DNS settings:
If you want to access your application via a domain name, configure your DNS settings (eg in Route 53) to point to the public IP or elastic IP of your EC2 instance.
8. Test your app:
Access the Spring application using the public IP or domain name of the EC2 instance, depending on your configuration. Test all features to make sure the deployment is successful.
access in your browser using following link
Remember to follow AWS best practices such as creating backups and maintaining security measures to ensure the stability and security of your deployed Spring application on EC2.