unable to ssh to EC2 instance inside public subnet in custom vpc - amazon-web-services

I have a vpc with 2 private and 2 public subnets. I have added ec2 instance in a public subnet.
The EC2 is given a private ip according to CIDR of VPN and public subnet.
Route table contains a row of 0.0.0.0/0 with internet gateway
the public subnet is associated with this route table.
EC2 is also healthy
I am unable to troubleshoot how to ssh to my instance.
I am new to VPC and this is my first time working with my own so I might have missed something.
Inbound rules on sg
inbound rules on NACL

Several things to check:
Make sure the EC2 has a public IP assigned (enable auto-assign IPv4 address on the public subnet)
NACL outbound rule allows your IP/ any IP to port 22/ any port since NACLs are stateless
The internet gateway is attached to the correct VPC
The subnet is associated with the correct route table that has internet gateway
The EC2 is provisioned under the correct public subnet that is associated with the route table
The SG that allows port 22 inbound is attached to the EC2

Related

what is the usage of CIDR notation route rule in aws

I have created a VPC and internet gate way. And attached internet gateway to the vpc. Create two subnets one is public subnet and another is private subnet. And created a route table to route the traffic. In route table added a route rule 0.0.0.0/0 to IGW(internet gatway). To test this I launch a EC2 instance in public subnet. I have generated a public IP address example : (554.6.8.24). Using that IP address do ssh and I am connecting from local machine to EC2 instance.
My question is I am connecting to the EC2 instance from my local machine using public IP. What is the point of adding route rule in 0.0.0.0/0 to IGW(internet gatway) in route table?. With out route rule can I connect to Ec2?
If an Internet Gateway is not present in an Amazon VPC, then that VPC has no connection to the Internet. This can be very useful for creating private networks.
If you want to connect a VPC to the Internet, then you need to:
Create an Internet Gateway for that VPC
Create a Route Table that sends traffic to the Internet Gateway
Attach the Route Table to a Subnet (thereby making that subnet a Public Subnet since it routes to the Internet Gateway)
Put resources inside the Public Subnet (eg EC2 instances)
In contrast, Private subnets do not have a Route Table entry that points to an Internet Gateway. Therefore, those subnets cannot access the Internet. This is an added layer of security to prevent access to private resources.
The only difference between a Public Subnet and a Private Subnet is having the Route Table entry that points to an Internet Gateway. Without that entry, a subnet will remain private.

AWS EC2 instance in private subnet unable to connect to internet via NAT gateway

I have two EC2 instances, one on public subnet(10.1.1.0/24) and one on private subnet (10.1.2.0/24)
EC2 instance in public subnet works fine with internet connectivity and configured services.
There is a NAT Gateway configured in the public subnet 10.1.1.0/24 with Elastic IP.
Default route on private subnet is [0.0.0.0/0 --> NAT Gateway in public subnet].
I have a custom network ACL on private subnet, what I observed is when source and destination for inbound/outbound rules is set to 10.1.1.0/24 (public subnet) external connectivity breaks, but ssh works.
Only when source/destination are set to 'any' does internet connectivity restore.
I am trying to understand what exact source/destination should be used in network ACLs on private subnet for internet connectivity to work.
To allow instance from private subnet access internet using NAT gateway, your NACLs or SG must allow internet traffic. The NAT does not change the internet traffic into private VPC traffic. So if you remove 0.0.0.0/0 the internet connectivity stops.
This is explained in AWS docs on Instances cannot access the internet:
Ensure that the network ACLs that are associated with the private subnet and public subnets do not have rules that block inbound or outbound internet traffic.
Therefore, you must allow 0.0.0.0/0 (or some IP range if you want only part of internet traffic) in your NACL and SG for the instances in private subnet.
As a side note, generally NACLs are not modified, and default settings are usually used. Instead security groups are the first choice of controlling access to and from the instances.

I can't connect through RDP to my EC2 Windows instance

NETWORK:
I have a private subnet and a public subnet. A VPC with IG attached. There is a NAT Gateway in the public subnet. I also have 2 route table, the public route table routes everything to the IG, the private route table routes everything to the NAT Gateway.
EC2:
My EC2 Windows instance is in the private subnet with a EIP and a Public DNS. It has a security group that allows all fron internet inbound/outbound but i cant even ping neither rdp
All the machine or instances in the private subnet cannot be connected externally hence the name private subnet. If you want to connect to your instance externally you must place it in the public subnet (the subnet that is connected to the internet gateway). Eventhough, you attach an elastic ip (which is a public ip) but it is routed through a NAT. The EIP must go through the internet gateway only.
If you really want to connect you need to VPN to your VPC (using AWS VPC VPN) and then RDP to your instance. Or have a gate instance in your public subnet.
Verify that the route table for the subnet has a route that sends all traffic destined outside the VPC to the internet gateway for the VPC. For more information, see Creating a Custom Route Table (Internet Gateways) in the Amazon VPC User Guide.

Unable to get AWS NAT gateway working for API with IP whitelist

Our aim is to get our Elastic Beanstalk setups to route traffic through a NAT gateway as we require for certain traffic connecting to API's which require IP whitelisting. Rather than make modifications to the current setup, I have created a separate/isolated VPC & EC2 instance to familiarise and test the setup. However I am yet to get the setup working as desired.
Here is the setup
VPC (vpc-77049811) with CIDR of 10.0.0.0/16
Internet gateway (igw-4d4b212a) assigned to mentioned VPC
Subnet (subnet-096d8a53) with CIDR of 10.0.1.0/24
NAT Gateway (nat-00bb49204627de7e6) attached to mentioned subnet and assigned Elastic IP
Route table attached to mentioned VPC and associate with mentioned subnet
1x EC2 Instance assigned to VPC and its own Elastic IP and Disabled Source/Destination Check
Route Table Setup
10.0.0.0/16 local
0.0.0.0/0 igw-4d4b212a
With the above setup, and am able to log into the server and make a curl request to get the servers public IP address (curl icanhazip.com). As soon as I add a rule to the route table for the url's resolved IP's to route through the NAT gateway though, I am unable to ping or request the curl request as it will timeout.
Rules added to route table which do not work
45.63.64.111/32 nat-00bb49204627de7e6
144.202.71.30/32 nat-00bb49204627de7e6
Not sure if I've overlooked something here or maybe I have misunderstood the concept and use cases for the NAT gateway?
This is public IP 45.63.64.111. You need IGW to reach to this traffic.
You either do that by directly redirecting your traffic to IGW
OR
You do that by directing to NAT then further directing that traffic to IGW
Directing to IGW part is missing.
Nat gateway is used for EC2 in private subnets (which does not have IGW attached to it). In scenario above, EC2 is in public subnet so ideally it does not need NAT.
Here is what I would do to use NAT-
1. Place EC2 in private subent. and have a Route table where all outgoing traffic to nat-gateway.
2. Nat-gateway which is in public subnet will forward your traffic to IGW.
It seems like you have misunderstood the purpose of a NAT.
Its purpose is to provide outbound internet access to instances in a private subnet without allowing any inbound connectivity - i.e. a subnet where the routing table does NOT have an entry for:
0.0.0.0/0 igw-4d4b212a
If you want to restrict access from your EC2 instance to specific IP addresses, put your NAT in the public subnet, create a private subnet, and put your instance in the private subnet. Then add the two routes to the route table associated with the private subnet:
45.63.64.111/32 nat-00bb49204627de7e6
144.202.71.30/32 nat-00bb49204627de7e6
If you simply want to restrict access of your EC2 instance to a couple of IP addresses, you can only create routes for those addresses:
45.63.64.111/32 igw-4d4b212a
144.202.71.30/32 igw-4d4b212a
Be aware that with this last option, your instance can be reached from the internet if you have rules open in your security groups.

AWS VPC NAT Not Working

I've setup a VPC on AWS and I'm trying to make one of my subnets private. I have:
Created my private subnet.
Created my NAT Gateway and associated my subnet to it.
Created a Route Table dest: 0.0.0.0/0 target: NAT Gateway
Created an Instance in my private subnet.
I can get to the instance in #4 by going to a different instance with a public IP then SSHing to its private IP. Once on the instance it has no internet connectivity.
Am I expected to update the route tables on the instance anything?
Turns out I was creating my NAT Gateway in my private subnet; should have been done in my public subnet. When I was creating it I assumed the subnet I was providing was the one I wanted to be NATed not where to create it.
Re-reading the docs here helped discover my error:
http://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/vpc-nat-gateway.html#nat-gateway-creating
thx.
Had an issue with my NAT instance as well.
It ends up I didn't set up the security group of the NAT instance correctly.
My NAT instance security group was allowing port 22 only.
Once I add port 80 (HTTP) and port 443 (HTTPS) to the security group, my instance in private subnet can connect to the internet.
Some points to be noted when creating a NAT instance:
The NAT instance needs to be in the public subnet.
The source and destination check of the NAT instance must be disabled.
At least allow port 80/443 on the security group of the NAT instance.
Associate outbound route of private subnet to NAT instance in route table.
In my case it wasn't working because I had the NAT in the private subnet instead of the public one.
Did you follow all the instructions here? In particular make sure you disable Source/Destination checks on the NAT instance.
Alternatively, AWS now has Managed NAT Gateways.
In my case, there was a misconfiguration of private instance security group: I limited access to internet by CIDR block of public subnet, when it should be enabled to access everything.