Deploy Simple Spring Boot CRUD App to AWS Elastic Beanstalk and Connect AWS Relational Database Service(RDS)

Simple Deployment Example of Spring Boot CRUD Application to AWS Elastic Beanstalk and Connection example of AWS RDS

👋Introduction

This is a Beginner Level Tutorial about How to deploy a Spring Boot CRUD Application on AWS Elastic Beanstalk and Connect to AWS Relational Database Service (RDS). I created a simple Todos application with Spring Boot. I am going to use Postman to emulate CRUD operations.

You can download the application source code.

👈What This Post is NOT About👉

  1. Not an advanced AWS tutorial
  2. Not an Elastic Beanstalk tutorial
  3. Not a Spring Boot CRUD web application tutorial

👉What This Post is About👈

  1. To show how you can deploy an application to Elastic Beanstalk.
  2. To show how you can connect AWS RDS
  3. To show how you can check logs for simple errors.

🚨Prerequisites

  1. Spring Boot knowledge
  2. Jar creation knowledge
  3. Basic AWS knowledge

📖 TLTR;

1- Download Source Code of CRUD Spring Boot Application
2- Create A Database on Local MySql
3- Connect Spring Boot to Local MySql DB
4- Test Local Endpoint with Postman
5- Create a MySql Database on AWS
6- Configure AWS RDS for Local Connection
7- Configure Local to Connect AWS RDS
8- Create and Configure an Elastic Beanstalk Application
9- Deploy Spring Boot Application to AWS Elastic Beanstalk
10- Check Logs for Error
11- Test Deployed Application
12- Resources

Download Source Code of CRUD Spring Boot Application

I created a very simple Spring Boot todo application which we will use in this post. We are going to use Postman to emulate front-end actions. You can download the source code via this link.

Create A Database on Local MySql

I am going to create a local database to test before deploying my application to AWS. I am using PhpMyAdmin as GUI. You can use GUI or terminal.

DB Name => aws_crud_demo
User Name => root
Password => password

We are going to use the above information to configure the spring application.

Connect Spring Boot to Local MySql DB

We need to add a couple of dependencies to connect MySql.

<dependency>
<
groupId>org.springframework.boot</groupId>
<
artifactId>spring-boot-starter-data-jpa</artifactId>
</
dependency>
<
dependency>
<
groupId>mysql</groupId>
<
artifactId>mysql-connector-java</artifactId>
<
scope>runtime</scope>
</
dependency>

Finally, we need to add the following properties to the application.properties file.

spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/aws_crud_demo
spring.datasource.username=root
spring.datasource.password=password

All set.

Test Local Endpoint with Postman

All CRUD operations are working.

Create a MySql Database on AWS

First, find the RDS service on the AWS console.

There will be a configuration page. So I am going to show you my configurations.

Choose MySql as an Engine option. I also choose Dev/Test environment for minimum resource usage.

We have to name our DB instance. Don't forget it is not the name of the Database, it is the name of the DB instance. I give the same username and password as Local for convenience.

I give 20GiB volume, you can give more it is up to you.

We want to connect this DB from our local too. Therefore we need to open it for public access (NOT PRODUCTION RECOMMENDED).

Now it is time to create the Database.

AWS gives an estimated monthly cost.

instance creation takes a while.

And created.

If you click instance name, you could find the URL of Database

Configure AWS RDS for Local Connection

As I said, we want to connect this DB instance from our local. Therefore, some additional configuration is needed.

If You click the DB instance, AWS will take you to the details page.

There is a security part on the detail page. When you click it, it will take you Security Groups and when you click security groups, it will take you inbound rules which we need.

We are going to add a new rule for Local connection. As you notice, it is only for the sake of this tutorial, I do not recommend this kind of rule.

Rule successfully added.

Configure Local to Connect AWS RDS

It is very simple actually. Just replace local URL with AWS RDS URL on application properties. I comment out the old one and added the new one.

#spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/aws_crud_demospring.datasource.url=jdbc:mysql://aws-demo-instance.cluuww5tunoz.eu-north-1.rds.amazonaws.com:3306/aws_crud_demo

It is connected and I check all CRUD operations are working.

Create and Configure an Elastic Beanstalk Application

Choose Elastic Beanstalk service from the console.

There will be a welcoming page. Choose Create Application

We will configure Elastic Beanstalk

we are going to connect our already created AWS RDS instance to the application.

Fill in the required fields and save.

save all configurations.

Application creation takes a while.

Deploy Spring Boot Application to AWS Elastic Beanstalk

After creation completed, the application page will be opened. There is an Upload and deploy button. We are going to upload our application's Jar file.

Upload finished AWS deploys our application.

Application deployed, there is a URL that we can use for CRUD operations.

But it is not working.

Check Logs for Error

There is a Logs a menu item at the left sidebar on the same page.

It let you download all logs related to the application.

And When I examine it, notice that it is a port error. Our application listens 8080 but Nginx listens 5000 .

There are 2 solutions.

  1. Change application port on application.properties file. Rebuild application and upload jar.

2. There is configurationon the left sidebar. It can take you to software configurations and you can add a new environment variable .

Test Deployed Application

Now, It is working

Everythings are working.

Junior Full-Stack Developer

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store