SSH Tunneling
CS290F Fall 2006 - UCSB Computer Science - Thorsten von Eicken
How to Connect to Other Ports on EC2 Instances Using SSH Tunneling
You may wish, for example, to connect directly to the MySQL or PostgreSQL instance running on an EC2 instance. However, we have only allowed access to the ports 22, 80 and 3000 on EC2. Rather than have us punch holes in the EC2 firewill willy-nilly, you can access the databases as if they were running locally (except slower) using SSH tunneling. Run SSH as follows:
$ ssh -i <AMI ssh key> root@<AWS instance domain name> -L<port>:localhost:<port>
The port number here is 3306 for MySQL, and 5432 for PostgreSQL. Now, if you connect to this port on the localhost, it will behave as if you were connecting to the remote server on the EC2 image:
$ psql -h localhost $ mysql -h localhost
What's really going on here, is that the SSH client listens on the first specified port on your local machine; any TCP connections that go to that port are tunneled through to the second port to the locahost at the other end of the SSH connection—i.e. to the database server on the EC2 image.
