Search code examples
postgresqlamazon-web-servicesssh-tunnel

I have AWS Postgres instance in private subnet and I don't have any VPN, How Can I access it through Local?


I have a Postgres instance under the private subnet, I don't have VPN configured to connect, even it's not allowed with my Bastion ( jump node ). Is there any way to connect it through my local machine to testing for development?

The default port is 5432. and I have ssh access to my application server through Bastion node.


Solution

  • As you have ssh access to your application server through Bastion. Then you may create two-level ssh tunnel proxy. As Postgres is not allowed even though Bastion node, only allowed through the application server.

    1: Create a tunnel from the Application Server to the bastion node.

    ssh-add ~/.ssh/id_rsa ;ssh -oStrictHostKeyChecking=no -Att -l USER_NAME BASTION IP ssh -oStrictHostKeyChecking=no -Att -l USER_NAME APP_SERVER_IP -L 5432:AWS_POSTGRESS_END_POINT:5432

    (keep open this terminal)

    2: Create another tunnel from Bastion to local computer: (open new terminal )

    ssh-add ~/.ssh/id_rsa ;ssh -oStrictHostKeyChecking=no -Att -l USER_NAME BASTION_IP -L 5432:localhost:5432 (keep open this terminal)

    3: use localhost or 127.0.0.1 with 5432 port in the connection string (to connect from local machine) instead of Postgress endpoint.

    Note: replace correct ssh key path and your user name with boldly highlighted text. and keep open both terminal open to maintain the session.