Skip to content

Instantly share code, notes, and snippets.

@ukayani
Last active April 10, 2020 23:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ukayani/9c6043902e9f323080678e98ddd660c6 to your computer and use it in GitHub Desktop.
Save ukayani/9c6043902e9f323080678e98ddd660c6 to your computer and use it in GitHub Desktop.
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