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.
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‘
7) After that, we’re going to c advance configure advanced settings.
8) Finally, it takes some time to create an 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‘.
3) Edit ‘Inbound Rules‘ and set ‘Source‘ support to Anywhere.
4) After that, provide following details db host, username, password, database name and port.
5) Finally, execute the following SQL.
CREATE TABLE `user` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`first_name` varchar(11) DEFAULT NULL,
`last_name` varchar(11) DEFAULT NULL,
`username` varchar(11) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`updated` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;
LOCK TABLES `user` WRITE;
INSERT INTO `user` (`id`, `first_name`, `last_name`, `username`, `created`, `updated`)
(1,'DAMITH','GANEGODA','test','2017-02-22 00:00:00','2017-02-22 00:00:00');
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.
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.
9) Check available java versions.
10) Install java 8.
sudo apt install default-jre
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.
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.
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.
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.
.out - use for delete nohup.out file.
less nohup.out – open the nohup.out file.
11277total visits,35visits today