Spring Boot & AWS RDS - Part 1

What is AWS RDS?

AWS RDS is a collection of managed services that makes it simple to set up, operate, and scale databases in the cloud. AWS RDS provides multiple DB Engines options like MySQL, PostgreSQL,MariaDB, Oracle SQL Server.

As Amazon RDS handles routine database tasks such as provisioning, patching, backup, recovery, failure detection, and repair. This brings a lot of convenience to RDS users and provides. RDS also provides other features like replication, enhance availability and reliability.

In this article we will examine how to use Spring boot to access AWS RDS PostgreSQL. Amazon RDS for PostgreSQL provides access to the capabilities of the familiar PostgreSQL database engine.

Creating PostgreSQL DB Instance on AWS

Go to RDS->Databases->Create Database for creating new database instance. Select PostgreSQL as engine type.
For this demo i am using below setting. (These configurations are not recommended for production usage)

  1. Free tier,
  2. DB instance identifier : employee-db
  3. Credential: Master username
  4. VPC : Default
  5. Public Access:True
  6. Security Group:Default
  7. Initial database name:employee

Note
Only specifying Public Access:True for your Databases might not work. The Security group should allow Inbound traffic from your IP address or All IP address.

Onces the database is ready, Note the Endpoint url which we will use as spring.datasource.url

Setting up Spring Boot Project

Let’s create simple small Spring Boot app that will interact with RDS.

We do not need any AWS specific dependancy, Only JPA,spring-web & postgresql dependancy are needed.

1
2
3
4
5
6
7
8
9
10
11
12
13
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>

Data source configurations
1
2
3
4
5
6
spring.datasource.url=jdbc:postgresql://<database-endpoint-url>:<port>/<database> 
spring.datasource.username=admin1
spring.datasource.password=Admin123
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.show-sql=true

Onces the database connection is done, We can simply use the JAP Repository to interact with database.

Controller
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@RestController
@Slf4j
@RequestMapping("/employee")
@RequiredArgsConstructor
public class EmployeeController {

private final EmployeeRepository repository;
@PostMapping
public ResponseEntity createEmployee(@RequestBody CreateEmployeeRequest request) {
repository.save(new Employee(request.getId(), request.getFirstName(), request.getLastName()));
return ResponseEntity
.status(HttpStatus.CREATED)
.build();
}
@GetMapping
public List<Employee> getAllEmployee() {
return repository.findAll();
}
}
Entity And Repository
1
2
3
4
5
6
7
8
9
10
11
12
13
14
@Entity
@AllArgsConstructor
@NoArgsConstructor
@Data
public class Employee {

@Id
private UUID id;
private String firstName;
private String lastName;
}

public interface EmployeeRepository extends JpaRepository<Employee, UUID> {
}

The code for this post is available on Github here

Share Comments