SSH'ing into AWS EC2 Instance located in Private Subnet in a VPC

MatiasN

I've been going at this problem for a couple of hours and maybe its not possible, maybe it is.

I have a VPC in AWS, with a couple of EC2 instances and Lambda Instances.

As of right now, The lambda can invoke, ssh and so on to the EC2 server without a problem.

My lambdas are using a security group with only HTTP, HTTPS AND SSH in "Outbound".

My ec2 default security group only accepts 22 inbound (From my Lambda security group, AND my office IP).

If i create an ec2 instance on my public subnet, both me and my lambda functions can access it through ssh.

If i create it on my PRIVATE subnet, my lambdas can ssh but i CANT...

Do i really have to have a NAT SERVER in order to achieve this?

TL:DR; Only my office and my lambdas should have access to my ec2 instances.

jarmod

First option to consider for SSH access to EC2 instances should be AWS Systems Manager Session Manager for Shell Access to EC2 Instances. It's potentially a big deal. No more bastions, no more firewall rules allowing inbound port 22. You basically run an SSH session in your browser and it can target all EC2 instances, regardless of public/private IP or subnet. EC2 instances have to be running an up to date version of the SSM Agent and must have been launched with an appropriate IAM role (including the key policies from AmazonEC2RoleForSSM).

The second option to consider is AWS Systems Manager Run Command which allows you to run commands remotely on your EC2 instances. It's not interactive like SSH but if you simply want to run a sequence of scripts then it's very good. Again, the instance has to be running the SSM Agent and have an appropriate IAM policy, and this option avoids the need to tunnel through bastion hosts.

Finally, if you really must SSH from your office laptop to an EC2 instance in a private subnet you need a few things:

  1. IGW and NAT in the VPC
  2. bastion host with public IP in the VPC's public subnet
  3. security group on the bastion allowing inbound SSH from your laptop
  4. a default route from the private subnet to the NAT
  5. security group on the private EC2 instance that allows inbound SSH from the bastion

Then you have to tunnel through the bastion host. See Securely Connect to Linux Instances Running in a Private Amazon VPC for more.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

User Data is not running on EC2 instance in Private VPC subnet

SSH into private VPC subnet

Amazon Web Service (AWS) VPC private subnet instance 'Permission denied (publickey).' - ssh from osx

unable to ssh to EC2 instance inside public subnet in custom vpc

Provide SSH Access on AWS EC2 Instance with a private key

<AWS VPC> Unable to ping private subnet instance from a public subnet instance

What makes a subnet into a private subnet in AWS VPC?

SSH Tunnel through Ubuntu bastion to EC2 instance in private subnet

Amazon ELB for EC2 instances in private subnet in VPC

AWS best method to ssh between EC2 instances in private subnet

AWS EMR on VPC with EC2 Instance

AWS: EC2 in public subnet can't ping EC2 in private subnet

Connecting to an ec2 instance in a private subnet using Lambda

How to access to EC2 Instance in private subnet?

Accessing WebApplication exposed by EC2 instance in Private Subnet

EC2 instance cannot use yum inside private subnet

How to setup instances in a private subnet of an AWS VPC?

SSH to AWS EC2 instance hangs

AWS EC2 instance route table and VPC route table

Docker instance running on private subnet AWS Fargate

How to configure OpenVPN for AWS VPC Peering with single private in 1st and single subnet in 2nd VPC?

AWS Lambda Function with VPC only works when in Private Subnet

Reach a VPN client from a host on private subnet on AWS VPC

Is it possible to access Jenkins GUI if it's in private subnet in aws vpc?

AWS EC2 Private Instance DB Connection

Setting up Redash Instance in private subnet. EC2 status check failed

AWS ECS - Can't connect to RabbitMQ EC2 instance from ECS container instance on the same subnet

aws vpc subnet design

AWS Ec2- need to create VPC and Subnets before Ec2 instance?