Guide to Using AWS RDS
Introduction
Amazon RDS (Relational Database Service) is a fully managed relational database service provided by Amazon Web Services. It makes it easy to set up, operate, and scale popular databases in the cloud without worrying about hardware management or complex software installation.
Key advantages
- Automated Management & Time Saving
- RDS automates time-consuming administrative tasks such as:
- Installation & Configuration: Launch a database with just a few clicks.
- Patching: Automatically applies security patches and software updates.
- Backup: Automatically performs daily backups and allows point-in-time recovery within 35 days.
- Flexible Scalability
- Vertical Scaling: Easily adjust CPU and RAM resources to match workload demands.
- Horizontal Scaling (Read Replicas): Create read-only replicas to offload traffic from the primary database, improving performance for read-heavy applications.
- High Availability & Reliability
- Multi-AZ Deployment: RDS automatically replicates data to another Availability Zone. If the primary zone fails, the system automatically fails over to the standby instance without interrupting the application.
- Strong Security
- Encryption: Supports encryption both at rest and in transit.
- Network Isolation: Runs inside Amazon VPC for controlled network access.
- Access Control: Integrates with AWS IAM for fine-grained permission management.
- Multiple Database Engine Support
- You can choose from six popular database engines:
- Open Source: MySQL, PostgreSQL, MariaDB
- Commercial: Oracle, Microsoft SQL Server
- AWS-Optimized: Amazon Aurora (offers significantly higher performance compared to standard MySQL/PostgreSQL)
Prerequisites
You can refer to my previous article where I demonstrated how to set up a NestJS project connected to PostgreSQL. In this article, I will guide you on using NestJS to connect to RDS PostgreSQL by extending the previous setup.
Details
First, use AWS CDK to create the required resources. Create the file lib/rds-stack.ts
- dbConfig: PostgreSQL configuration values you can modify.
- databaseName: default is postgres
- dbPort: default is 5432
- schema: default is public
- dbSecret: Contains database connection information including username, dbname, port, and schema.
- The password is randomly generated.
- All information is stored in AWS Secrets Manager.
- The password is automatically rotated every 30 days (minimum rotation interval is 1 hour, configurable).
- Bastion: Since the VPC is configured with ec2.SubnetType.PRIVATE_ISOLATED, external connections are not allowed.
- Therefore, we create a bastion EC2 instance with permissions to access the RDS instance.
- This acts as an intermediary to create an SSH tunnel between your local machine and the RDS database.
Update the file bin/aws-cdk.ts
After successfully deploying the stack:
The corresponding resources will appear in the AWS Console.
Next, in your NestJS project, use the generated information to connect to RDS.
Download the global-bundle.pem root certificate (CA) then copy it into your project.
By default, non-code files are not included in the dist folder after build. You need to update nest-cli.json to include the folder containing global-bundle.pem:
Create a tunnel connection to RDS using the deployment information from CDK:
Create database.service.ts
- host: '127.0.0.1', this connects to localhost on your machine. Since you started a session to create a tunnel to RDS, any operation on localhost:5432 is effectively executed on the RDS PostgreSQL instance.
- ssl configuration: required because RDS PostgreSQL (from version 15 onward) enforces SSL/TLS connections by default. Alternatively, you may disable strict validation using: rejectUnauthorized: false
Create database.module.ts
- TestEntityService: Example service mentioned in the previous article. You can replace it with any service that interacts with PostgreSQL.
- dataSourceFactory: Handles error 28P01 (invalid password). Since password rotation is enabled in Secrets Manager, when the password changes:
- Catch this error
- Destroy the old connection
- Recreate a new connection using the updated password
Add DatabaseModule to app.module.ts:
After successfully connecting to RDS, your APIs will function normally.
Happy coding!
Comments
Post a Comment