To better understand how AWS VPCs (and NATs in general) work, I was reading this question where the goal was to have the following:
# GOAL
172.31.0.0/16 local
A.B.C.D/32 nat-451b3be9
0.0.0.0/0 igw-b4ac67d0
This is what intrigued me:
Note further that the configuration you're attempting will allow outbound, but never permit inbound connections (initiated from outside) from the A.B.C.D address to anything on this subnet, because the return route is asymmetric through the NAT gateway.
the NAT Gateway is not designed to be created on any subnet for which it provides NAT services. The instances reach external resources via their subnet's route table (points to NAT-GW for instances without public IP, points to IGW for instances with public IP) and the NAT-GW reaches the Internet via its subnet route table (points to IGW).
If an instance is using its own public IP, it must route responses out via the IGW because that's where the inbound traffic is coming from, and it can't try to leave via NAT-GW because the peer on the outside would see the reply coming from the wrong source IP if the traffic got translated.
I'm trying to understand exactly why it would allow outbound but not inbound traffic. Here's what I'm thinking: say that the EC2 instance has an elastic IP and is in a subnet with a routing table as the above. The NAT is then in a separate subnet. Say A.B.C.D
initiated a connection to the EC2's elastic IP. Wouldn't the connection enter the VPC, the routing table would send it through the NAT, which would then go to the instance, and then back out through the NAT? However, since it got sent out through the NAT, the address got translated (as said above) and the peer would drop the packet since it didn't come from the IP of the EC2 instance. Is this the correct understanding? It will still reach the EC2 instance, but response packets will never be received by A.B.C.D
?