Cannot initiate a successful connection from private subnet to public subnet - amazon-ec2

I have an instance in private subnet. I have two more instances in public subnet including a NAT instance. From NAT and another public instance I am able to ping/ssh to the instance in private subnet successfully but the reverse case is not working. First I was trying to find out why the private instance is not connecting to the internet but then I realized it is not even connecting to public subnet. Security rules are allowing all traffic(inbound and outbound) for all the instances and even the routing tables are correct. What is the reason for this? Is there anything that is stopping the private subnet from connecting to the public subnet, because as I understood within a VPC all instances can communicate to each other no matter what subnet they belong to?

Related

How to access a public ip enabled instance from a another instance where only private ip is available

I have two instances in AWS where we have public ip and private ip for one instance and the other instance has only private ip.
Both are in the same VPC and same subnet. However I’m not able to access/ping the public ip from the instance 2. I can ping/access it using private ip. But how can we achieve it using public ip.
I have tried using the icmp rules in the security group but still not accessible.

Ipv4 Public ip cant be reached through HTTP

I tried all the combinations required while creating an EC2 instance. While creating an instance, I have choosen a new VPC which I had created which has an internet gateway and has both public and private subnet IDs. After giving proper security group, still then I am not able to connect to the instance.

Two similar instances with different internet access availabilities

I have created one ec2 centos instance and then launched another one from that but in the second one , I have disables the public IP so it doesn't have a public IP address.
The instances are in same subnet having the same security group, and roles. The first instance have ínternet access but the second one doesn't have. Is this related to assigning a public IP?
How can I have internet access in an instance without a public IP?
You have two options here:
[1] The first option is to use Elastic IP: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/elastic-ip-addresses-eip.html
[2] If you want to have an Internet access without public IP, you need to provision a NAT Gateway and configure route to it.
People generally do this, they create a VPC, create two subnet in it (one Public and One Private), in the Private subnet they launch their instances, and in the public subnet they create a NAT Gateway, and configure the route in the route table so that the instances in the private subnet have a route to internet via NAT Gateway.
[1] NAT Gateway: https://docs.aws.amazon.com/vpc/latest/userguide/vpc-nat-gateway.html
Instances without public IP addresses can access the Internet in one of two ways:
Instances without public IP addresses can route their traffic through a NAT gateway or a NAT instance to access the Internet. These instances use the public IP address of the NAT gateway or NAT instance to traverse the Internet. The NAT gateway or NAT instance allows outbound communication but doesn’t allow machines on the Internet to initiate a connection to the privately addressed instances.
For VPCs with a hardware VPN connection or Direct Connect connection, instances can route their Internet traffic down the virtual private gateway to your existing datacenter. From there, it can access the Internet via your existing egress points and network security/monitoring devices.

Unable to ssh into private subnet using Elastic IP

I have setup a VPC with 2 subnets. One of them is public other one is private.
I am able to ssh from an instance in public subnet to an instance in private subnet using the private IP address of machine. However, if I use the elastic IP address of private instance, the connection times out.
For example, lets call EC2 instance in public subnet as "PUB" and instance in private subnet as PRV.
PRV has Elastic IP say "EIP" and private address, say PRV_IP.
When I ssh from public instance to private one using the private IP address, connection is successful, however if I use private IP, connection fails.
That is,
ssh -i private_key ec2-user#EIP (succeeds).
ssh -i private_key ec2-user#PRV_IP (fails, connection timesout).
Can someone explain me why connection is failing with elastic IP?
As you have seen, attaching Elastic IPs to an instance in private subnet is a worthless exercise. This happens because each subnet can have exactly one default route and that will either point to the igw object (public subnet) or the NAT instance/gateway(private subnet).
If you are binding an elastic IP to a machine in the private subnet, the inbound traffic would arrive at the instance, but the outbound reply traffic would be routed back through the NAT instance, which would discard it.
That's why when you are trying to do SSH, your client machine is able to send a request to the instance but the instance is not returning back the response, hence the timeout.

Are EIP's required for internet traffic?

Sorry if this is a dumb question but I can't find any definitive answers. I setup a VPC with a private subnet and a public subnet. THe private subnet has a NAT'd instance to route for internet traffic. The public subnet is configured to go out of the IGW.
-I created a Bastion host to login into SSH
-I created Test instance on private subnet and connect from Bastion Host. Because of the route to the NAT instance and port 80/443 is allowed and ICMP, i can ping and access files on the internet.
-I create instance on public subnet without EIP. Since it has a route to the IGW, shouldn't I have internet access? I created a test security group to allow all traffic from all sources and i still can't ping or get http links.
A public subnet instance with a public DNS name should be accessible over the internet provided you have right VPC security group configured. You do not need an EIP.
Public DNS name of EC2 instance changes when you stop and start the instance. to avoid this, you can assign an EIP to the instance so that the IP address remains same across the instance stop/start cycles.
You need to answer these questions:
Does your public subnet instance has a public DNS hostname? Run curl -s http://169.254.169.254/latest/meta-data/public-hostname on your instance to see the public DNS hostname.
Do you have VPC security group configured properly to allow incoming internet access ?

Resources