Hello! In this tutorial, we will explore in detail how to connect to Amazon RDS in a local environment using Spring Boot. AWS (Amazon Web Services) is one of the cloud computing services, and RDS (Relational Database Service) is a managed relational database service offered by AWS. This tutorial is aimed at those who have a basic understanding of Spring Boot backend development.
Table of Contents
- 1. Introduction to AWS RDS
- 2. Creating an RDS Instance
- 3. Setting Up a Spring Boot Project
- 4. Connecting to the Database
- 5. Testing and Validation
- 6. Conclusion and References
1. Introduction to AWS RDS
AWS RDS is a managed relational database service provided by Amazon. AWS takes care of management tasks such as hardware and clustering, backup and restore, security patches, and scaling, so developers can focus more on application development. RDS supports various database engines, including MySQL, PostgreSQL, Oracle, and SQL Server.
1.1 Advantages of RDS
- Cost-effectiveness: You can use resources as needed, and the charges are based on usage.
- Auto-scaling: Resources can be adjusted automatically based on changes in application traffic.
- High availability: Offers automatic backup and restore features across multiple AZs (Availability Zones) to facilitate disaster recovery.
- Security: Integrates with VPC to provide a high level of security.
2. Creating an RDS Instance
Now let’s go through the process of creating an RDS instance. You can easily create an instance by logging into the AWS console and selecting the RDS service.
2.1 Instance Creation Steps
- Log in to the AWS Management Console.
- Select RDS from the services.
- Select Database and click on Create Database.
- Choose MySQL or your desired database engine from the engine options.
- In the database settings, enter the instance identifier, username, password, etc.
- Select the DB instance class and storage options.
- Set VPC, subnet, security group, etc., in the Network & Security settings.
- After reviewing the options, click Create to create the instance.
3. Setting Up a Spring Boot Project
Once the RDS instance is created, let’s set up the Spring Boot project. We will create the project using Spring Initializr.
3.1 Using Spring Initializr
- Visit Spring Initializr.
- Configure the project metadata: Enter Group, Artifact, Name, etc.
- Add Spring Web, Spring Data JPA, and MySQL Driver as dependencies.
- Click the Generate button to download the project.
3.2 Project Structure
When you open the downloaded project in your IDE, a basic structure will be generated. The main components are as follows:
- src/main/java: Where the Java source code is located.
- src/main/resources: Where the application configuration file
application.properties
is located.
4. Connecting to the Database
To connect to RDS from your Spring Boot project, you need to modify the application.properties
file.
4.1 Configuring application.properties
spring.datasource.url=jdbc:mysql://{RDS_ENDPOINT}:{PORT}/{DB_NAME}?useSSL=false&allowPublicKeyRetrieval=true
spring.datasource.username={YOUR_USERNAME}
spring.datasource.password={YOUR_PASSWORD}
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
Here, each field needs to be set as follows:
{RDS_ENDPOINT}
: The endpoint of the RDS instance{PORT}
: MySQL uses port 3306 by default.{DB_NAME}
: Database name{YOUR_USERNAME}
: The username set when creating the RDS instance{YOUR_PASSWORD}
: The password set when creating the RDS instance
4.2 Creating JPA Entity Classes
Now that we are connected to the database, we can create JPA entity classes. For example, we will create a User
entity.
package com.example.demo.entity;
import javax.persistence.*;
@Entity
@Table(name = "user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false, unique = true)
private String email;
// Getters and Setters
}
5. Testing and Validation
Once all settings are completed, you need to validate whether the RDS connection has been established properly. Let’s create a simple REST API to test it.
5.1 Creating a REST Controller
package com.example.demo.controller;
import com.example.demo.entity.User;
import com.example.demo.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public List<User> getAllUsers() {
return userRepository.findAll();
}
@PostMapping
public User createUser(@RequestBody User user) {
return userRepository.save(user);
}
}
5.2 Running the Application
Run the Spring Boot application. Either run the class containing the main
method in your IDE or enter ./mvnw spring-boot:run
in the terminal.
5.3 Testing API with Postman
You can use Postman to test the API. Create user information and check the list.
- GET request:
GET http://localhost:8080/users
- POST request:
POST http://localhost:8080/users
(JSON Body:{"name": "John Doe", "email": "john@example.com"}
)
6. Conclusion and References
Through this tutorial, we learned how to connect to AWS RDS from Spring Boot. Using RDS simplifies database management and allows you to focus quickly on application development. The combination of AWS and Spring Boot provides a powerful backend solution and is widely used in real services.
References
Now, try to connect your Spring Boot project to RDS! If you have any questions, feel free to leave a comment.