Skip to main content
awe-logo

Deploying a Spring Boot Application on AWS using EC2 and RDS

awe-logo

Overview

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 application.properties 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-**********.us-west-2.compute.amazonaws.com

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

8965total visits,21visits 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?

RSS
Follow by Email
Facebook
Facebook
Google+
http://mydevgeek.com/deploying-spring-boot-application-aws-using-ec2-rds
Twitter