How to SSH to ec2 instance in VPC private subnet via NAT server

Solution 1:

You can set up a bastion host to connect to any instance within your VPC:

http://blogs.aws.amazon.com/security/post/Tx3N8GFK85UN1G6/Securely-connect-to-Linux-instances-running-in-a-private-Amazon-VPC

You can choose to launch a new instance that will function as a bastion host, or use your existing NAT instance as a bastion.

If you create a new instance, as an overview, you will:

1) create a security group for your bastion host that will allow SSH access from your laptop (note this security group for step 4)

2) launch a separate instance (bastion) in a public subnet in your VPC

3) give that bastion host a public IP either at launch or by assigning an Elastic IP

4) update the security groups of each of your instances that don't have a public IP to allow SSH access from the bastion host. This can be done using the bastion host's security group ID (sg-#####).

5) use SSH agent forwarding (ssh -A user@publicIPofBastion) to connect first to the bastion, and then once in the bastion,SSH into any internal instance (ssh user@private-IP-of-Internal-Instance). Agent forwarding takes care of forwarding your private key so it doesn't have to be stored on the bastion instance (never store private keys on any instance!!)

The AWS blog post above should be able to provide some nitty gritty regarding the process. I've also included the below in case you wanted extra details about bastion hosts:

Concept of Bastion Hosts: http://en.m.wikipedia.org/wiki/Bastion_host

If you need clarification, feel free to comment.

Solution 2:

The only way I could make it work.

1) Make sure that the security group for this private instance has in the inbound rule the security group from the public subnet

Ports Protocol Source
All All sg-0b6616e070b9ea2d (public security group)

2) Using proxy commands, configure your ssh config file, to have something like this

vim ~/.ssh/config

Host publichost
   HostName 24.123.34.45
   User ubuntu
   IdentityFile ~/mypem.pem
   ProxyCommand none
Host privatehost
   HostName 10.0.2.133
   User ubuntu
   IdentityFile ~/mypem.pem
   ProxyCommand ssh publichost -W %h:%p

Run ssh privatehost it should work