JavaDream spring boot Spring boot deployment to AWS EC2 in less then 5 minutes

Spring boot deployment to AWS EC2 in less then 5 minutes

Hi all in this blog we ‘ll learn about how to do spring boot deployment to AWS EC2 in less then 5 minutes. If you are our regular reader then you know that we have already cover this topic. So the question is why we are covering this topic again.

The reason is previously we cover spring boot application deployment to AWS EC2 using docker container with PostgresDB connectivity. But that’s a lengthy process. So what if you create a simple spring boot project and just quicky want to deploy it to EC2 for quick testing of your application endpoints.

So here we come with the solution. In this blog we ‘ll see how can we deploy our spring boot application on AWS EC2 in less than 5 minutes. Or if you want to see spring boot application deployment to AWS EC2 using docker container with DB connectivity, You can follow our blog on Spring boot deployment to AWS EC2 with docker container and PostgresDB connectivity.

Let’s continue on this and see how to deploy spring boot application on AWS EC2 in less then 5 minutes

So we ‘ll create a simple spring boot project and expose some endpoints that we ‘ll use using EC2 IP address. For testing let’s create a simple Spring boot project and create a GET rest endpoint.

Just create a simple spring boot project with Spring Web starter so we can create rest endpoints.

After Creating project from start.spring.io just extract it and open in your favorite IDE. We are using intellij IDE. Now create a GET rest endpoint.

package com.javadream.ec2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Ec2Controller {

    @GetMapping("/ec2")
    public String testEc2(){
        return "Hello I am hosted on EC2 instance. Thanks to Javadream for simplify the hosting steps";
    }
}Code language: CSS (css)

We just need the jar of our project. So run below command to create your project jar.

 mvn clean install -DskipTests

After running above command in your IDE console. You ‘ll get something like below. Once your build success you ‘ll get a jar file inside your project target folder.

Now see how long it took. It hardly take 1 or 2 minute. Now move to the AWS part and create EC2 instance there.

Create EC2 instance on AWS

1- Search ec2 in your AWS search bar.

AWS EC2

2- Click on Launch Instance

3- Fill basic details and select OS image as per your choice. We are going to choose Amazon Linux for this example. So select you OS image and fill basic details.

Note: Most important thing is key pair (Login) section. This help us to login to AWS EC2 instance using aws cli. So click on create new key pair and download the .pem file. Keep this file at a safe area as you need this file to login into aws ec2.

Keep rest of the fields default and click on Launch Instance button

AWS EC2 spring boot integration

It took few seconds and create your ec2 instance. Just go to ec2 instance section you can see your newely created ec2 instance there.

So till now we have our spring boot project jar and our aws ec2 instance ready. It’s time to push jar from our local system to aws ec2.

Push spring boot jar from local system to aws ec2 instance

We have our spring boot jar on our local system and we need to push it to aws ec2. So for doing this we need the aws cli. If you don’t have aws cli you can follow our blog to setup aws cli on your local system install aws cli on local system.

After setup aws cli on your local the second most important thing is the .pem file that you downloaded while creating ec2 instance. Go to the location where your .pem file present and open the command prompt at that location.

For example my .pem file at download folder so i open my command prompt at this location and establish a SSH connection from my local to AWS EC2 instance. Use below command to establish SSH connection with ec2 instance.

ssh -i springboot-ec2-ssh-keys.pem ec2-user@ec2-13-222-168-204.compute-1.amazonaws.comCode language: CSS (css)

You have to change above command as per your configuration. Let me show you how can you do this.

  • ssh -i remains same.
  • springboot-ec2-ssh-keys.pem change this part with the name of your .pem file name.
  • ec2-user@ remains same.
  • ec2-13-222-168-204.compute-1.amazonaws.com change this part with your Public DNS of your EC2 instance. Just see it on your EC2 instance summary. Refer below screenshot for reference to find this.

So if you successfully SSH into ec2 instance you ‘ll see something like below image on your command prompt.

SSH into aws ec2 instance

We are successfully SSH into our ec2 instance. As we know EC2 instance are IaaS ( Infrastructure as a Service ). It’s like we are renting a virtual machine from AWS on which we have full control. We can configure it as per our interest.

As ec2 is IaaS we don’t get anything we need to configure it by ourselves. As we need java version 21 to run our spring boot application, we need to install that first on our EC2 instance. You can check by running java -version command on this ec2 you ‘ll get nothing as it’s a brand new ec2 instance that we created.

Configure EC2 instance with required software and run spring boot application

First we download java version 21 in our EC2 instance. As we already SSH into this ec2 instance. Run below command to install java version 21. If you are using any other version just run the command to install that version. Below command is to install java version 21.

sudo dnf install java-21-amazon-corretto -y

Now verify if java install or not on ec2 instance by running java -version command.

install java 21 on aws ec2

We have java also now on our ec2 instance. Now it’s time to run our spring boot jar. First we need to push this jar from our local system to ec2 instance.

So to push jar to ec2 from your local. Go to the location where you have .pem file and run below command. As our .pem file present in Downloads location i ‘ll open my command prompt on this location and run below command.

scp -i springboot-ec2-ssh-keys.pem C:/Users/vasu/Downloads/SpringbootEc2/SpringbootEc2/target/SpringbootEc2-0.0.1-SNAPSHOT.jar ec2-user@ec2-18-215-168-179.compute-1.amazonaws.com:/home/ec2-user/javadream-ec2example-jar.jarCode language: JavaScript (javascript)

You can see jar upload is completed. Now go to your command prompt where we did the SSH connection and see the /home/ec2-user/ directory. You ‘ll see your jar here.

Now run this jar file using below command. After running successfully you can check logs of your spring boot application on this console. And we ‘ll access the GET rest endpoint using ec2 IP address.

java -jar javadream-ec2example-jar.jarCode language: CSS (css)

From logs we can see that our spring boot jar is successfully running. Now it’s time to call our GET rest endpoint that we created using ec2 instance IP. Just check public IP of your ec2 instance and try to access the rest endpoint.

So when you hit your ec2 public IP with rest endpoint on browser. You ‘ll get something like below and you ‘ll not be able to access the endpoint.

It is because there is one step left. As our application is running on port 8080. We have to enable inbound rule for our ec2 instance for 8080.
On your ec2 instance page click on Security and then click on Security groups.

Click on Inbound then Edit inbound rule.

Click on Add rule then choose type as Custom TCP, Select port range 8080 and click on save rules.

These two inbound rules should be there one for SSH on port 22 and one for Custom TCP with our spring boot application port.

That’s it now try to access the endpoint using Public IPv4 address. As my ec2 instance public IPv4 address is 18.215.168.179 and our application is running on port 8080. So our complete address to access GET rest endpoint is.

http://18.215.168.179:8080/ec2Code language: JavaScript (javascript)

where /ec2 is the GET rest endpoint that we created.

Note: Make sure you use http in your URL as spring boot by default server HTTP request only if you use https on your endpoint it ‘ll not work. If you need HTTPS you need to configure it in your spring boot application with SSL certificate.

Output:

You can share this address with anyone and they can access your application endpoints. If you are doing it for learning don’t forget to delete ec2 instance after testing else you ‘ll be charge after free tier complete.

Hope you enjoy this blog. If you like this blog please share with others and help us to grow. You can share your feedback in our comment sections.

If you need blog on any specific topic you can put that in our comment section. We ‘ll try to cover that in our incoming blogs.

Thanks

Others blog you may like.

Deploy Soring boot application on AWS EC2 using docker and PostgresDB connectivity.

Send SMS to customer mobile number using spring boot with Twilio.

Share with others

Related Post