Guide to using AWS RDS public endpoint
Introduction
In the previous article, I introduced the basic concepts of AWS RDS as well as how to securely connect using a tunnel; you can review it to grasp the necessary information before proceeding.
In this article, I will guide you through creating an RDS Postgres with public access, meaning this database can be accessed from any computer. This method may be considered less secure than the previous one, but it is useful when you need to share database connections with many users during the development process who do not have an AWS account to connect via a tunnel.
Detailed Instructions
Using AWS CDK, create the file lib/rds-public-stack.ts
- dbConfig: database information, port, and schema can be changed according to your needs.
- ec2.Vpc has a subnetType of ec2.SubnetType.PUBLIC to allow external access, so we no longer need to create a bastion to connect via a tunnel.
- Here, I use ec2.Peer.anyIpv4() as an example; if your internet or organization uses IP version 6, please replace it with ec2.Peer.anyIpv6(), but the best practice is to limit the accessible IP addresses as follows: ec2.Peer.ipv6("<ip address>").
Next, update the file bin/aws-cdk.ts
After successful deployment, the result will be as follows:
Resources on the AWS console have been successfully created.
Next, in the NestJS project, update the file database.service.ts as follows:
There are not many changes here, only the additional use of GetParameterCommand to retrieve the endpoint for the host (instead of connecting to localhost as in the previous example).
Next is the file database.module.ts
- The main change here is in the dataSourceFactory, because the schema name created in the CDK has changed (using app_schema instead of the default public), so before creating the connection, you must check whether this schema exists to initialize the schema first.
- The remaining part is also to check for the '28P01' error when the password is invalid, as I mentioned in the previous article.
Then, try starting the project and you will see the corresponding new schema being created.
Other APIs will also function normally.
Happy coding!
Comments
Post a Comment