Skip to main content

Deploying a Spring Boot Application on AWS using EC2 and RDS



In this tutorial, we are going to deploy a spring boot application and database on Amazon Web Services (AWS) using Amazon Elastic Compute Cloud (EC2) and Amazon Relational Database Services (RDS).

First of all, we need to an Amazon account. You can create an Amazon Free Tier account using the following link.

We’ve already implemented a spring boot application with MySQL database. That tutorial you can find from the following link.

  • Spring boot application with MySQL – In that tutorial, created a simple REST service with MySQL integration. There was a REST api, we can use for getting the user information when passing the user id.

Note: – You can download complete example from the GitHub and check whether working on the local environment.

RDS Setup

1)  Login to the Amazon dashboard.

2) Go to the RDS Dashboard.

3) Click on the ‘Lunch a DB Instance‘.

4) According to the Amazon, We can create Amazon Aurora, MySQL, MariaDB, PostgreSQL, ORACLE or SQL Server. We select the MySQL as a database.

5) Then we’ve to select the database plan. We selected ‘Dev/Test‘ as a plan. Click the ‘Next Step‘.

6) In this phase, you need to provide database specific details such as db instance name, username and password. Then click on the ‘Next Step

AWS RDS Settings

7) After that, we’re going to c advance configure advanced settings.

AWS RDS advance settings

8) Finally, it takes some time to create an RDS instance.

AWS RDS instance


Connect via MySQL client

I’m using the Sequel Pro as a client. You can use any client to connect to the Amazon RDS instance.

1) First of all, we need to change the security groups to allow connect from outside.

2) Right click on the RDS instance. Click on the ‘See Details‘. Then Click on the ‘Security Group‘.

AWS RDS Security group

3) Edit ‘Inbound Rules‘ and set ‘Source‘ support to Anywhere.

RDS inbound rules

4)  After that, provide following details db host, username, password, database name and port.

sequel pro

5) Finally, execute the following SQL.


Configure the Spring Boot Application

Now, we’re going to change the database connection properties in the application and build a jar file.

1)  Open the the file is located under the ‘resource‘ directory.

2) Change the properties as following.

3) The next step is, build a jar file. Use the mvn clean install to build it.

Create an EC2 instance and deploy it

1)  Go to the AWS console. Select the ‘EC2 Service‘.

2) Then, select ‘instances‘ and click on the ‘Launch Instance‘ button. There are no of OS, we can select as a server (Ubuntu, RedHat, Amazon Linux, SUSE Linux). For this tutorial, we selected the Ubuntu.

3) Select the free tier eligible instance and launch it.

4)During the launch, it will ask for a creating security key. Select the ‘Create a new key pair‘ and give a name as a ‘mydevgeek‘. Finally, download the key.

ec2 key pair

5) Rename the key as ‘mydevgeek.pem‘ (remove the .txt).

6) If you are using Mac OS or Linux give a permission to the file using a following command.

chmod 400 mydevgeek.pem

Note: – If you want to see more details how to connect to the ec2 through ssh, select the instance and click on the ‘Connect‘ button.

7) Now, we ready to connect to the ec2 instance via terminal (command line). Use the ssh command that shows in the ‘Connect To Your Instance‘ when you click on the ‘Connect‘ button as mentioned in above.
ssh -i "mydevgeek.pem" ec2-user@ec2-**********

8) First thing, update the Ubuntu.

sudo apt-get update

9) Check available java versions.

java -versions

10) Install java 8.

sudo apt install default-jre

11) Next thing is, upload the jar file into the ec2. So that, I’m using a CyberDuck (Filezilla also another option)

cyberduck connection

Note: – provide the Public DNS, Username and SSH key.

12) Create a new directory inside ‘home/ubuntu‘. The directory name is ‘spring-app‘.

13) Upload the jar file that contains under the ‘target‘ directory into the ‘home/ubuntu/single-app‘ using CyberDuck.

14) While uploading, open 8080 port for outside the world. Select the ‘Security Group‘ of the instance. Add following rule.

AWS add rule

15) Finally, following command can be used to run the REST service.

nohup java -jar spring-boot-1.0-SNAPSHOT.jar &

nohup – use for terminal output goes into the nohup.out file.

& – use for running as a background service. So that terminal is free to do another work.

How to Test it

We can check whether the application is running or not look at the nohup.out file. Use the following command to check the nohup.out.

less nohup.out

When you click on the EC2 instance, you will be able to find the ‘IPv4 Public IP‘ that IP can be used to send a REST request.

postman rest request

Important Commands

ps -ef | grep spring – this command is useful check whether the application is running or not.

kill -9 PID – use this command to kill running process. The PID is processing ID.

rm nohup.out - use for delete nohup.out file.

less nohup.out – open the nohup.out file.

Related Post

13743total visits,36visits today

  • Rajesh Rana

    crisp and clear !! great article !!!

  • Francisco Vieira

    Very nice explanation, pity I did exactly as you said but mine is not working.
    It is a very simple app just printing a string, locally it’s working on port 5000, so I opened port 5000 on EC2 but does’t work.
    It says “server not responding”
    I checked “nohup.out” and everything is ok there, just not responding when trying with browser or Postman.

    What would it be?

    • Francisco Vieira

      Hey my friend, I have a problem, my EC2 app just can’t complete the connection to MySQL on RDS, I have made a lot of changes on SG but nothing seems to work.
      I have 2 SG, EC2 is using one and RDS is using other.

      BTW, when I run an app without using database it goes perfectly well.

      Any ideas?

    • sirine beji

      Did you find a solution?

  • meetgaurang

    Very helpful! Thank yoU!

  • Daniel

    Very nice, help me a lot! In step 13 change ‘home/ubuntu/single-app’ to ‘home/ubuntu/spring-app‘

  • En NuNYet de Can CalÇadA

    Shouldn’t be better afterwards to install the Spring Boot Application as a Service ????

    The nohup command is the poor man’s way of running a process as a daemon. When you run a command in an interactive shell, it has a controlling terminal and will receive a SIGHUP (hangup) signal when the controlling process (typically your login shell) exits. The nohup command arranges for input to come from /dev/null, and for both output and errors to go to nohup.out, and for the program to ignore interrupts, quit signals, and hangups. It actually still has the same controlling terminal – it just ignores the terminals controls. Note that if you want the process to run in the background, you have to tell the shell to run it in the background – at least on Solaris (that is, you type ‘nohup sleep 20 &’; without the ampersand, the process runs synchronously in the foreground).

    Typically, a process run via nohup is something that takes time, but which does not hang around waiting for interaction from elsewhere.

    Typically (which means if you try hard, you can find exceptions to these rules), a daemon process is something which lurks in the background, disconnected from any terminal, but waiting to respond to some input of some sort. Network daemons wait for connection requests or UDP messages to arrive over the network, do the appropriate work and send a response back again. Think of a web server, for example, or a DBMS.

    When a process fully daemonizes itself, it goes through some of the steps that the nohup code goes through; it rearranges its I/O so it is not connected to any terminal, detaches itself from the process group, ignores appropriate signals (which might mean it doesn’t ignore any signals, since there is no terminal to send it any of the signals generated via a terminal). Typically, it forks once, and the parent exits successfully. The child process usually forks a second time, after fixing its process group and session ID and so on; the child then exits too. The grandchild process is now autonomous and won’t show up in the ps output for the the terminal where it was launched.

    You can look at Advanced Programming in the Unix Environment, 3rd Edn by W Richard Stevens and Stephen A Rago, or at Advanced Unix Programming, 2nd Edn by Marc J Rochkind for discussions of daemonization.

Follow by Email