Skip to content

Instantly share code, notes, and snippets.

Last active Apr 10, 2020
What would you like to do?
SSH Tunnel example to access http server on private AWS network
// Goal: Be able to do a curl from local to server in AWS private subnet
// Host - The machine running the server in a private subnet
// Bastion - A machine which has access to the Host in the private subnet. You need to have SSH access to this machine
// Local - Your local machine
// Normally, if you SSH into your Bastion, you are able to curl the DNS/IP of the private Host machine running your server
// To be able to curl from your local machine, you'll need to set up a tunnel between your local and the bastion
// When you tunnel, you have to provide the DNS/IP of the private Host as seen from the Bastion. This will typically be a private IP
// an internal DNS name
// Run on local computer to set up tunnel from local to bastion
ssh -i <keypair> -f ec2-user@<bastionIP> -L <localPort>:<privateHostName>:<hostPort> -N
// localPort - the port you want to expose the Host's server on your local machine
// privateHostName - the hostname/IP of the Host running the server
// hostPort - the port of the server running on the Host
// You are now able to curl from your local machine via localhost and localPort
curl http://localhost:<localPort>
// To see the list of all tunnelled connections
// make sure you kill the tunnel once you are done using it
ps aux | grep ssh
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment