How to connect to an AWS RDS database (deployed in VPC) with an SQL client

amazon-web-servicesrds

I am new to AWS and trying to set up and initialize an RDS instance. Since I have a newly created account, it does not support EC2-Classic, which (from what I understand) means that my RDS instance must be deployed into a private subnet in a VPC. However, once the RDS instance created, how can I connect to it from the outside world? I understand that I can connect to it from the public subnets in my VPC, so my application servers will be able to use the DB with no problem. However, I want to be able to say, fire up Squirrel or some other GUI client in order to initialize the schema, or add columns and tables as my application grows. How can I do this if it is required to live in a private subnet?

Best Answer

One solution (but not the only solution!) is to use what's called a Bastion Host. A Bastion Host is an ultra-low-powered server that sits in your public subnet and is the only server that allows inbound SSH connections.

This server should be thoroughly hardened, and depending on your level of paranoia, there are a few techniques you can use to hide the fact that this server is listening for SSH connections at all. See, for example, http://www.portknocking.org/view/details. Of course, you don't need to harden it just to connect to your RDS instance.

Anyway, you can setup your EC2 Security Group rules as follows:

  • Bastion Host Security Group allows port 22 from your local IP only (so you can SSH into it, but no one else can)
  • RDS Security Group allows your incoming database connections on Port X (depends on your database) only from the Bastion Host

By the way, you can achieve "only from the Bastion Host" either by specifying the private IP address of your Bastion Host, or listing the security group name the Bastion Host uses.

Now you have two options from here:

OPTION #1: Set up local port forwarding as part of your SSH connection

For example, if you're on OS X or Linux, SSH into the bastion host and setup local port forwarding with:

ssh -l <bastion-host-username> -L <local-port-you-connect-to>:<rds-private-ip>:<rds:listening-port> <bastion-host-public-ip>

And let's say you're connecting to Postgres from an Ubuntu-based Bastion Host. It might look like this:

ssh -l ubuntu -L 5432:<rds-private-ip>:5432 <bastion-host-public-ip>

Your local machine is now listening on port 5432 and will forward any of those connections to <bastion-host-public-ip> which in turn will forward it to port 5432 on <rds-private-ip>

OPTION #2: Look for this feature in your Database Client

I know DBVisualizer supports this. I'm not sure about Squirrel. Basically, instead of setting up the local port forwarding manually using SSH, your SQL client handles this for you.