AWS Database Blog

Connect external applications to an Amazon RDS instance using Amazon RDS Proxy

Amazon RDS Proxy is a fully managed, highly available database proxy for Amazon Relational Database Service (Amazon RDS) that makes applications more scalable, more resilient to database failures, and more secure. With RDS Proxy, you can handle unpredictable surges in database traffic that might otherwise cause issues due to oversubscribing connections or creating new connections at a fast rate.

Security best practices recommend placing your databases and proxies inside a private network with no internet inbound access. However, software as a service (SaaS) providers and third-party integrations often rely on secure communications over the internet.

In this post, we show you how to securely and efficiently connect external applications to a single RDS instance or Amazon Aurora cluster using AWS Secrets Manager, RDS Proxy, Network Load Balancer, TLS/SSL Certificates, and Amazon Virtual Private Cloud (Amazon VPC); configuring Security Groups and Network access control lists (ACLs); and optionally using AWS Shield to protect against DDoS attacks.

Solution overview

For our solution, we have an RDS instance configured with Multi-AZ and one read replica (optional) residing on a private subnet. Connections to this instance are managed via RDS Proxy, which resides on the same subnet as the database instance. To make RDS Proxy accessible via the internet, we use a Network Load Balancer located on the public subnet to provide a URL endpoint used to connect to the database. To secure the solution, we use network Access Control Lists (ACLs) to restrict access in and out of the VPC, and security groups to act as a firewall for RDS Proxy and the RDS instance. Optionally, Shield is available to protect against DDoS attacks. SSL/TLS connections are also recommended to provide another layer of security by encrypting data that moves between your client and a DB instance.

The following diagram illustrates the solution architecture.

This solution opens RDS Proxy connectivity through the internet; for security, we recommend testing on an isolated VPC or sandbox environment to avoid accidental exposure.

Prerequisites

This sample solution requires that you have a set of networking resources in place, such as the following:

After all the prerequisites are met, you are ready to configure RDS Proxy for external access.

Create a database user with appropriate privileges

Create a new database user with the following steps:

  1. Connect to your bastion host (jump box).
  2. Connect to the database using pgAdmin or the tool of your choice.
  3. From pgAdmin, open a new query with the Query Tool or tool of your choice.
  4. Use the following code to create a read/write database user and grant limited privileges to the user within the database:
CREATE ROLE rdsproxy_rwuser WITH 
LOGIN
NOSUPERUSER
INHERIT
NOCREATEDB
NOCREATEROLE
NOREPLICATION
PASSWORD 'xxxxxx';
 
GRANT pg_read_all_data, pg_write_all_data TO rdsproxy_rwuser;

Create a Secrets Manager secret

RDS Proxy uses secrets to validate and connect to the database. Complete the following steps to create a new secret:

  1. On the Secrets Manager console, choose Secrets in the navigation pane.
  2. Choose Store a new secret.
  3. For Secret Type, select Credentials for Amazon RDS database.
  4. For Username, enter rdsproxy_rwuser (your database user).
  5. For Password, enter your password for rdsproxy_rwuser.
  6. For Encryption key, choose aws/secretsmanager.
  7. For Database, select postgresql-02 (your database instance).
  8. Choose Next.
  9. For Secret name, enter rdsproxy_rwuser.
  10. For Description, enter an optional description.
  11. Choose Next.
  12. Rotation schedule, specify a schedule (optional).
  13. Choose Next.
  14. Review your options and choose Store.

Create a proxy

To create a proxy, complete the following steps:

  1. On the Amazon RDS console, choose Proxies in the navigation pane.
  2. Choose Create proxy.
  3. For Engine family, choose PostgreSQL.
  4. For Proxy identifier, enter rdsproxy-postgresql02.
  5. Set Idle client connection timeout to 30 minutes.
  6. For Target group configuration, choose postgresql02.
  7. Under Authentication, specify the following:
    1. For Identity and access manager (IAM) role, choose Create IAM role.
    2. For Secret Manager secrets, choose rdsproxy_rwuser.
    3. For Client authentication type, choose SCRAM SHA 256.
    4. For IAM authentication, select Not Allowed.
  8. Under Connectivity, specify the following:
    1. Optionally, choose Require Transport Layer Security (TLS).
    2. For Subnets, choose the same subnets as the database subnet group.
  9. Under Additional connectivity configuration, specify the following:
    1. For VPC, choose your VPC.
    2. Existing VPC security groups, choose database_security_group.
  10. Choose Create proxy.

    You also need to obtain your RDS Proxy IP addresses.

  11. Go to your EC2 bastion host (jump box).
  12. Open a terminal or command line perform a nslookup against your RDS Proxy (for example, nslookup rdsproxy-postgresql02.proxy-xxxxxxxxxxxx.us-east-1.rds.amazonaws.com).
  13. Save the IP addresses for later use.

Create a Network Load Balancer

To create a Network Load Balancer, complete the following steps:

  1. On the Amazon EC2 console, choose Target groups in the navigation pane.
  2. Choose Create target group.
  3. Under Basic configuration, specify the following:
    1. For Target type, select IP addresses.
    2. For Target group name, enter rds-proxy-postgresql01-tg.
    3. For Protocol, choose TCP.
    4. For Port, enter 5432.
    5. For IP address type, choose IPv4.
    6. For VPC, choose your VPC.
  4. For Health check protocol, choose TCP.
  5. Choose Next.
  6. On the Register targets page, for Choose a network, choose your target VPC.
  7. Specify the IP addresses and define the ports (repeat for every RDS Proxy IP):
    1. For IPV4 addresses, enter your RDS Proxy IP addresses.
    2. For Port, enter 5432.
  8. Select Include as pending below.
  9. On the Review targets page, for Targets, choose All.
  10. Choose Create target group.
  11. Under Add listener, specify the following:
    1. For Protocol, choose TCP.
    2. For Port, enter 5432.
    3. For Default action, choose rds-proxy-postgresql01-tg.
    4. Choose Add.

  12. On the Network mapping tab, note down the CIDR range for the load balancer. You will need this for next step.

Update your network ACL

Complete the following steps to update your network ACL:

  1. On the Amazon VPC console, choose Network ACLs in the navigation pane.
  2. For Network ACL, choose <<Network ACL of your VPC>>.
  3. On the Inbound rules tab, choose Edit inbound rules, and Add rule.
  4. Add the following rules:
    1. Inbound rule 1:
      1. For Type, choose All Traffic.
      2. For Source, enter your public IP (for example, 99.5.5.5/32).
    2. Inbound rule 2:
      1. For Type, choose All Traffic.
      2. For Source, enter the internal IPv4 CIDR range for the subnet groups that require access to RDS Proxy (for example, 10.0.0.0/16).
    3. Inbound rule 3 (if not using Amazon Route 53):
      1. For Type, choose HTTP (80).
      2. For Source, enter 0.0.0.0/0.
    4. Inbound rule 4 (if not using Route 53):
      1. For Type, choose HTTPS (443).
      2. For Source, enter 0.0.0.0/0.

Update your security group

Complete the following steps to update your security group:

  1. On the Amazon VPC console, choose Security groups in the navigation pane.
  2. Choose the security group database_security_group.
  3. On the Inbound rules tab, choose Edit inbound rules, and Add rule.
  4. Add the following rules:
    1. Inbound rule 1:
      1. For Type, choose PostgreSQL.
      2. For Source, choose your database security group (sg-xxxxxxxxx).
    2. Inbound rule 2:
      1. For Type, choose PostgreSQL.
      2. For Source, enter the network load balancer IP range (for example, 10.0.0.0/20).
      3. For Description, enter Internal IP AZ1 Network Load Balancer.
    3. Inbound rule 3:
      1. For Type, choose PostgreSQL.
      2. For Source, enter the network load balancer IP range (for example, 10.0.16.0/20).
      3. For Description, enter Internal IP AZ2 Network Load Balancer.
  5. Continue adding inbound rules as needed for your IP addresses.

Test connectivity

From an authorized desktop (allowed IP range on network ACL), launch pgAdmin (or your tool of choice) and open a new query. Connect to the RDS instance using the Network Load Balancer endpoint as the host name, and the database user and password configured for RDS Proxy.

To encrypt your connection, follow using SSL/TLS to encrypt a connection to a DB instance.

Note:
In this solution, using SSL mode: verify full or requiring hostname validation will cause the validation to fail.

Clean up

To avoid incurring future charges, delete any resources that you created as part of this post, such as the following:

  • VPC
  • Database security group
  • Database subnet group
  • Bastion host (jump box)
  • RDS for PostgreSQL or Aurora PostgreSQL instance
  • RDS Proxy
  • Network Load Balancer
  • pgAdmin or the tool you installed to query the database

Conclusion

This post showed how to securely and efficiently connect external applications to a single RDS instance using RDS Proxy.

Workloads experiencing hundreds or thousands of internet-routed concurrent connections can benefit from such an architecture. This solution can help sustain seasonal highs and unpredictable increases in workload.

To learn more about where or when to use RDS Proxy, check out Planning where to use RDS Proxy.


About the Authors

Jose A. Ramirez is a Database Specialist Solutions Architect at AWS. He has over 25 years of experience in the technology industry as a DBA and Systems Administrator. Jose now focuses on architecting solutions for customers looking to modernize their applications and databases in AWS.

John Scott is a Database Specialist Solutions Architect based in Atlanta. He has years of experience as a Linux Admin and DBA for companies of all sizes. John’s favorite things are family, great barbecue, and Amazon Aurora.

Archana Sharma is a Sr. Database Specialist Solutions Architect, working with Worldwide Public Sector customers. She has years of experience in relational databases, and is passionate about helping customers in their journey to the AWS Cloud with a focus on database migration and modernization.