How do I connect to the RDS instance?
Setup and SSH tunneling to the Moped RDS instance
Last updated
Setup and SSH tunneling to the Moped RDS instance
Last updated
Moped runs on a PostgreSQL database in AWS, but it is not publicly accessible. It currently lives in a closed (private) network, where only certain servers have access to it. One of those servers is called the moped bastion host. This server is just a small virtual machine (EC2 instance) on AWS, and it is carefully placed in the same network and VPC as the database, and it acts as a proxy.
In short, to connect to the PostgreSQL database, you will need two sets of credentials:
An SSH Key. The ssh key needs to be generated by you in your computer, and an admin needs to set up that key in the bastion host for you to have access. The SSH tunnel will be taken care of by your PostgreSQL client.
The PostgreSQL username/password. Once your machine is connected to the bastion host, you will be able to directly connect against the PostgreSQL client.
Be sure to be in your ~/.ssh/
directory using the command $ cd ./ssh/
To generate an ssh, run these command: $ ssh-keygen -t rsa
The command is going to prompt you to enter the name of the name of the file, try: moped_bastion
Then the rest of the questions can be blank, just hit enter.
The final key is the .pub file you created, it should look something like this:
Once you submit the key to an administrator, you will be able to test access to the server. To test the key works correctly, all you need to do is run this command:
⚠️ There are few tasks that require directly accessing production databases. If you can achieve what you are doing with read only access, always us a Read Replica! ⚠️
To access the staging or production database directly, you must:
Have your IP added to the Inbound Rules of the atd-moped-ssh-rds-production
security group attached to the RDS Bastion Host
Have your RSA key added to the RDS Bastion Host (described above)
After that is done, you can use the ssh
command and the -L
flag to map a local port to port 5432
of the RDS instance through a session connected to the bastion host.
To do this, run:
Note: The 5433
port in the previous command is a protection from accidentally connecting to the RDS through a client or script that is targeting the local 5432
port which is the default for PostgreSQL. HT to Frank Hereford for this!
Now that you have opened the tunnel, you can target localhost
port 5433
with a SQL client or script to interact with the database (carefully!!!). The username and password for the database is stored in 1Password under the name AWS RDS Moped.