SSH in to EB instance launched in VPC with NAT Gateway - amazon-web-services

I have Launched an Elastic Beanstalk application in a VPC with Amazon RDS (postgresql) using NAT Gateway (because I want to route my application traffic through a fix public ip address) following these instructions:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/vpc-rds.html
How can I ssh into the instance from my local system ?
eb ssh is showing following error however my instance is available and not terminating.
ERROR: This instance does not have a Public IP address. This is possibly because the instance is terminating.
How can I login to the postgresql client ?
Following command is not prompting anything:
psql --host= --port=5432 --username= --password --dbname=ebdb
I know they are in private subnet so can't be accessed from public network but I want to know the possibility of that. Please help !

You will have to have a server with a public IP (in a public VPC subnet) that you can connect to from outside your VPC. I recommend setting up a t2.nano server as a bastion host.

If you use VPN, you can also modify sshops.py to use the private DNS name. Varies by OS and version, but mine is located here:
~/Library/Python/2.7/lib/python/site-packages/ebcli/operations/sshops.py
Search for PublicIpAddress (mine is on line 88), and change it to read:
ip = instance['PrivateDnsName'] #was PublicIpAddress
It's too bad that the EB CLI isn't on Github...otherwise I'd contribute a way to do this via a parameter.
I also added a convenient alias for this:
alias appname='eb init appname;eb ssh --region=us-east-1 appname -n'
This allows running appname 1 or appname n, where n is the number of hosts in your cluster.

Related

Auto configure private DNS resolve in VPC network for EC2 instances running from AMI

I have AMI template server in EC2 AWS witch run my server.
For sure it's running in single VPC network.
I want to be able to connect any my server using ssh once it's running using hostname dns resolve.
For example I have gateway, server-01, server-02 in my ec2 instances list.
Once I launch one more server from my AMI (server-03), I need to connect to it from gateway server using ssh server-03
How I can do it?
I would suggest using terraform to manage your EC2 instances. This will allow you to do many things you would normally do manually.
You can have a private or public hosted zone assigned to your VPCs (public would require a bit more)
Then on terraform, you can have the following:
Your ec2 instance creation.
A tfvar file containing the variables for all your EC2 instances
Your Hosted Zone attaching the EC2 private IP to a DNS
Output afterwards to print out your new EC2 instance with the private DNS you can SSH to

RDS resolves to public IP from within VPC

I'm working with an EC2 instance running Ubuntu 20.04 and RDS; both are in the same VPC. I've added security groups to allow connections to the RDS from the EC2 instance.
I am having an issue where a ping to the RDS instance from the EC2 resolves to the the public IP instead of the private IP and so the mysql connection fails; the connection succeeds if I use the inernal IP so its not an issue of not being accessible.
I am assuming this is related to some DNS cache but I've tried flushing and still down't work. The commands I've tried are below:
sudo systemd-resolve --flush-caches
sudo /etc/init.d/dns-clean start
Any ideas how we can solve this issue? How can the rds endpoint to resolve to the internal IP?
For improved security, it is better to use PubliclyAccessible = No. This will cause the DNS Name to resolve to a private IP that is only accessible from within the VPC. It will also assist with using Security Groups to control access to the database.

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

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.
The 1st option to consider for SSH access to EC2 instances is EC2 Instance Connect which allows you to control access to your EC2 instances using IAM and provides access from either the AWS console or your regular command line SSH tools.
The 2nd option is AWS Systems Manager Session Manager for Shell Access to EC2 Instances. 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). No need for a bastion host or firewall rules allowing inbound port 22.
The 3rd 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 can do so via a bastion host. You need a few things:
IGW and NAT in the VPC
bastion host with public IP in the VPC's public subnet
security group on the bastion allowing inbound SSH from your laptop
a default route from the private subnet to the NAT
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.
Create a Bastion host.
This would be a public EC2 instance in a public subnet having the same security group as your private ec2 instance.
Ensure that traffic within the security group is allowed. You can do this by creating an inbound rule for your security-group.
Now in Windows 10, you can run the following though your command prompt :
ssh -i your_private_key.pem ec2-user#private_ip -o "proxycommand ssh -W
%h:%p -i your_private_key.pem ec2-user#public_ip"
Replace the following 3 things in the command posted above :
your_private_key
private_ip
public_ip
You can refer to this: https://docs.aws.amazon.com/vpc/latest/userguide/VPC_Scenario2.html
You will have to use NAT Gateway to access anything in the Private Subnet.

ssh from my ec2 instance to my ec2 instance

because i am running an application (airflow) in a python virtual environment on my ec-2 instance and i want to run a command in my default python environment on the same ec2-instance, i thought it was easier to ssh to my own instance and execute the command.
the problem i have is that i cannot connect to my own instance.
i added my security group id of my ec2-instance to my inbound configuration (ssh on port 22) but this did not work.
it does work when i set my ssh inbound configuration on port 22 from anywhere, but i don't want to do that.
my outbound config allows all traffic.
If you are connecting from another EC2 instance in the same subnet, you'll want to allow the private IP of that EC2 instance, not your local address.

Do you need an Elastic IP to ssh to your instance

I try to set up an ec2-instance using Ansible.
But it fails when I try to ssh for it. The security group is containing port 22.
We reached our maximum of elastisch IP's at the moment. So I want to know if there's something wrong with my script or isn't it possible to ssh to an instance without an elastic IP?
Your instance won't NEED an elastic IP. You'll get a public IP if you put the VM in your default VPC. But note that this IP might and will change if you reboot the instance.
In short, no you won't need an elastic ip, but it's very handy.
Your instance either needs an Elastic IP, or it needs to be in a public VPC subnet with a public IP.
You can also have Ansible go through an instance it has SSH access to in order to get into the VPC and get access to your other instances. Here is a blog post about this process: http://blog.scottlowe.org/2015/12/24/running-ansible-through-ssh-bastion-host/ If you search for "Ansible ssh bastion host" you will find lots of articles on this topic.