Reach a private RDS database from a laptop
An Amazon RDS database is safest when it has no public endpoint. Your team still needs to run queries from a laptop. This guide compares each way to connect, and it shows how to do it with no open inbound port.
The database has no public address
The public endpoint is turned off. A scanner on the internet has no route to the database port.
The laptop connects to a Gateway
The Client sets up a WireGuard tunnel to a Gateway in the VPC. The Gateway accepts no inbound connection from the internet.
The security group trusts one source
The security group of the database accepts the database port from the Gateway and from your apps.
Only the database port passes
The Resource is limited to TCP/5432. The laptop reaches the database and no other host in the VPC.
Why a private RDS database is hard to reach
Each Amazon RDS instance has an endpoint, which is a DNS name such as mydb.abc123.us-east-1.rds.amazonaws.com. Each instance also has a setting with the name Publicly accessible.
- When the setting is on, the instance has a public IP address. The RDS endpoint resolves to that address from outside the VPC.
- When the setting is off, the instance has a private IP address only. The RDS endpoint resolves to an address that only hosts inside the VPC can reach.
A private RDS database is the correct default. Your applications run in the same VPC and connect with no problem.
The problem starts when a person needs access. A developer wants to connect to RDS from a laptop with psql or a database tool. The laptop is not in the VPC, so the private address goes nowhere.
AWS describes the network rules for an instance in its guide to DB instances in a VPC.
Why a publicly accessible database is a risk
The fast fix is to turn on the public endpoint. This fix puts the database port on the internet.
- Scans. Scanners look for open database ports such as 5432 and 3306 on all public addresses.
- Password attacks. The only protection that stays is the database password. Database passwords are frequently old, shared, and stored in many places.
- One rule from exposure. A security group rule that permits
0.0.0.0/0opens the database to all of the internet. A person can add that rule in a hurry and not remove it. - Audit findings. Most security frameworks and cloud audit tools flag a database with a public address.
A database holds the data that an attacker wants most. Keep it off the internet, and solve the access problem a different way.
Four ways to connect to RDS from a laptop
A public endpoint with an IP allowlist
You turn on the public endpoint and limit the security group to known source addresses. This option needs no more infrastructure.
It fails for people who move. Home addresses change, and a coffee shop or a hotel has a new address each time. The allowlist grows, nobody removes old entries, and the database is still on a public address.
A bastion host with an SSH tunnel
You run a small instance in a public subnet. The developer opens an SSH tunnel through it:
ssh -N -L 5432:mydb.abc123.us-east-1.rds.amazonaws.com:5432 ec2-user@bastion.example.com
The developer then connects to localhost:5432. The database stays private. The cost is the bastion host: an open SSH port, an instance to patch, and an SSH key for each person. Each developer must also start the tunnel before each session.
A cloud VPN
A managed client VPN from the cloud provider puts the laptop in the VPC. The database stays private, and no SSH tunnel is necessary.
A cloud VPN usually gives a route to full subnets, so you must add rules to limit each user to the database. The price usually depends on the number of connections and on the hours of use.
Zero trust access
A small agent in the VPC makes an outbound connection to a broker. The user signs in with an identity provider. A rule gives one group access to the database endpoint on the database port, and to no other host.
This option gives you RDS without a bastion host and without a public address.
Good practices for any option
- Keep the public endpoint off. Set Publicly accessible to no, and put the instance in private subnets.
- Refer to a security group, not to an address range. Let the database security group accept the database port from the security group of the hop. Do not use a wide CIDR.
- Give each person a database user. Do not share the master user. Give read-only roles to people who only read.
- Require TLS. Make the database refuse connections that are not encrypted.
- Use the endpoint name. The private address of an instance can change after a failover. The RDS endpoint name stays the same.
- Keep a record. Know who connected to the database, from which device, and when.
How Firezone gives access to a private RDS database
Firezone is an open source zero trust access platform built on WireGuard. You run a Gateway inside the VPC. Firezone runs the control plane. The Gateway makes outbound connections only, so its security group needs no inbound rule.
- Deploy a Gateway in the VPC. The Terraform example for AWS deploys Gateways that fail over and balance load for each other.
- Add a DNS Resource. Use the RDS endpoint name as the address. The Gateway resolves the name inside the VPC, so it gets the private address.
- Add a traffic restriction. Limit the Resource to TCP/5432 for PostgreSQL, or to TCP/3306 for MySQL and MariaDB.
- Write a Policy. Give the database Group access to that one Resource. Access is default-deny for all other people.
- Update the security group. Let the database accept its port from the security group of the Gateway.
The developer signs in to the Firezone Client and connects with the usual command:
psql -h mydb.abc123.us-east-1.rds.amazonaws.com -U app_reader -d app
No tunnel command and no local port are necessary. The same endpoint name works in database tools and in scripts. The Postgres guide shows each step, and the steps apply to other databases also.
The database still authenticates the user with its own user name and password. Firezone controls who can reach the port. A conditional access rule can also limit a Policy by country, by source IP range, or by time of day. Conditional access is available on the Team plan and higher. Flow Logs record which person connected to the Resource and when.
Public endpoint, bastion host, cloud VPN, and Firezone
The four approaches differ in what the internet can reach and in how much work each connection takes.
| Public endpoint | Bastion + SSH tunnel | Cloud VPN | Firezone | |
|---|---|---|---|---|
| The database has no public address | No | Yes | Yes | Yes |
| No inbound port open to the internet | NoThe database port | NoTCP 22 on the bastion host | NoThe VPN endpoint | Yes |
| Access limited to the database port | Yes | PartialThe bastion host can reach more | PartialNeeds authorization rules | YesOne Resource with TCP/5432 |
| Works from any network with no allowlist change | No | PartialIf port 22 is open to all | Yes | Yes |
| Connect with the endpoint name, no tunnel command | Yes | NoStart an SSH tunnel first | Yes | Yes |
| Sign-in with your identity provider | No | PartialNeeds more software | Yes | YesOIDC or an email code |
| No more hosts to run in the VPC | Yes | NoThe bastion host | YesThe provider runs it | NoOne or more Gateways |
The table describes the usual design of each category. A specific product or setup can be different. Read the documentation of the product before you decide.
Common questions
How do I connect to a private RDS database from my local machine?
You need a hop inside the VPC. The hop can be a bastion host with an SSH tunnel, a cloud VPN, or a zero trust Gateway. The hop connects to the RDS endpoint on the database port, and the security group of the database must permit the hop.
Is it safe to make an RDS instance publicly accessible?
It is a risk that you can avoid. A strict security group reduces the risk, but the database still has a public address, and one wrong rule opens it to the internet. Keep the public endpoint off, and use a private path.
Can I reach RDS without a bastion host?
Yes. A cloud VPN and zero trust access each give a private path with no bastion host and no SSH tunnel. With Firezone, a Gateway in the VPC connects outbound, and you add the RDS endpoint as a DNS Resource.
Why does the RDS endpoint not connect from outside the VPC?
The endpoint of a private instance resolves to a private IP address. That address has a route only inside the VPC. From a laptop on a different network, the connection times out.
Which port does Amazon RDS use?
The default port depends on the engine. PostgreSQL uses TCP 5432. MySQL and MariaDB use TCP 3306. You can set a different port when you create the instance.
Is this guide about Windows Remote Desktop Services?
No. This guide is about Amazon RDS, the managed database service. For Windows Remote Desktop Services, you want zero trust access to RDP. See Access a device via SSH or RDP.