AWS: Why does an Internet Gateway perform NAT? - amazon-web-services

An Internet gateway serves two purposes: to provide a target in your VPC route tables for Internet-routable traffic, and to perform network address translation (NAT) for instances that have been assigned public IPv4 addresses.
Source: https://docs.aws.amazon.com/AmazonVPC/latest/UserGuide/VPC_Internet_Gateway.html
If the instances mentioned above have public IPv4 addresses, why does the Internet Gateway need to perform Network Address Translation? It would be great to have an example of a network flow that would be broken for a scenario with
VPC subnet with Internet Gateway attached
instances in that VPC subnet with IP addresses (Elastic IPs in AWS VPC parlance) in the public IP address space
hosts on the Internet unable to send packets to these instances without the Internet Gateway performing NAT

Despite of EC2 ( or to be specific: ENI associated with that EC2) instance having associated with public IP address. It is not aware about it!
This is from the same URL which was shared in original link.
" Your instance is only aware of the private (internal) IP address space defined within the VPC and subnet"
This means all network traffic which is going out/coming in to ENI is via private IP address.
If you look at the diagram on the same page, you will see a router.
This router
Does routing within VPC across all its subnets
Does routing to Internet gateway
EC2/ENI will interact with this router.
After router directs traffic to Internet Gateway (IGW):
IGW have mapping of (public ip - private ip ) of EC2 instances within that VPC.
Steps of EC2 sending traffic to Internet
EC2 with private 10.0.1.1 IP sends request to google.com (curl google.com) or tried to access public aws construct like s3
Let's assume that this is public subnet. So Router associated with this subnet will forward this traffic to default route (0.0.0.0/0) to IGW
IGW looks up its internal table (private ip: public ip). Does NAT translation using that table.
I would have liked to describe your scenario but I did not understand why IGW will not perform NAT.

The instance side of an ENI only has a private address. Public addresses are done by associating an Elastic IP or assigning a public address. Mapping of these public addresses comes from the NAT functionality of the Internet Gateway.
Your instance is only aware of the private (internal) IP address space defined within the VPC and subnet. The Internet gateway logically provides the one-to-one NAT on behalf of your instance, so that when traffic leaves your VPC subnet and goes to the Internet, the reply address field is set to the public IPv4 address or Elastic IP address of your instance, and not its private IP address. Conversely, traffic that's destined for the public IPv4 address or Elastic IP address of your instance has its destination address translated into the instance's private IPv4 address before the traffic is delivered to the VPC.
Quote from the same documentation linked in the question.
The flow for inbound traffic is Public Address -> Internet Gateway -> Private Address.
If the hosts are in a subnet without a route table that has a default route defined through the IGW, then the instance will not be able to return traffic.

Related

NAT gateway removal

There are some servers that are in private subnet and some are in public subnet.
I want to remove nat gateway and make all instances that were in private subnet to get access through internet gateway. Please suggest me the procedure.
When I have added internet gateway and removed Natgateway then started getting API timeout, please suggest me the possible reason for the same.
A "Public Subnet" is defined as a subnet that has a Route Table entry that points to an Internet Gateway.
Thus, if you want instances in the private subnet to have direct access to the Internet via the Internet Gateway, you will need to convert the private subnet into a public subnet. This can be done by adding a Route to the subnet's Route Table that directs traffic with a destination of 0.0.0.0/0 to the Internet Gateway.
Resources in the VPC that need to communicate with the Internet will need to be assigned Public IP addresses. This can be done for existing resources by assigning an Elastic IP address (which is a static IP address). However, there is a limit on the number of Elastic IP addresses available in each region.
Any new Amazon EC2 instances launched can be given a public IP address during launch, so they won't need an Elastic IP address.

How packet gets routed towards IGW of my VPC?

Below is the public route table, that is associated with IGW of my VPC,
where first entry indicates that any packet with destination IP address(private) part of CIDR space 172.31.0.0/16 will be accepted(target-local), by this VPC.
But packets coming from internet have destination IP as public IP(which is assigned to either NAT of public subnet or any instance in public subnet), as per below diagram:
AWS console allows to configure NAT with public IP to private IP translation table. But NAT gw is within public subnet, behind IGW.
VPC is an autonomous system on Internet with its own gateway(IGW). AWS console does not allow assigning public IP to IGW.
 IGW is entry point to VPC... But IGW is not assigned any public IP. How a packet from internet reach to my vpc, if IGW has not been assigned a public IP?
maybe i don't understand your question... but, imagin, all your public IPs sitting on the IGW.. then, he knows to which internal ip it has to translate. the IGW has a NAT table. in general, it looks like "public IP -> internal IP". so, when a packet arrives on your IGW, it rewrites the destination of the IP header to your internal address. this is some sort of configuration you can't see anywhere in your aws console. The oposite, if your server answers to that request, the IGW translate the private address in the source portion of the IP Header
when you create an instance and choose to automatically assign an public ip address, the AWS orchestrator automatically configures the IGW to point that public IP to your Instance.
What if you have a "nonpublic" subnet and you want to reach the internet just from inside the VPC to the outside? then you need a NAT Gateway. The NAT gateway hides all private IPs with just ONE public IP. this means, all packets source address from your nonpublic subnets are translated to ONE public IP. this means, you can initiate a connection TO the internet, but nobody can initiate a connection to your servers. this is called NAT-Overload. with this techique NAT uses the tcp/udp ports as an translation identifier. like this: sourcePrivate-ipX:sourcePort32001 translated to sourcePublicIP:sourcePort32001... then, when the return packet comes, NAT-GW looks into the destination address and port and knows to which private IP it has to send the packet
edit... aaahhh now i understand... you are mixing up IGW and NAT-GW! you only need a NAT-GW in your public subnet if you have private subnets which want to connect to the internet. NAT-GW and Internet GW are 2 pairs of shoes where both of them performing Translations :-)

What does public subnet mean in AWS VPC?

I learnt that private IPv4 address space is:
Class A 10.0.0.0 – 10.255.255.255
Class B 172.16.0.0 – 172.31.255.255
Class C 192.168.0.0 – 192.168.255.255
A private IP address is an IP address that cannot be accessed over the Internet and normally protected by firewall.
169.254.0.0 used by Microsoft machines but not assigned by IANA
In the below AWS VPC architecture(from here):
Public subnet is designated with subnet 10.0.0.0/24, but this address is part of private address space(10.0.0.0 – 10.255.255.255).
Why subnet(10.0.0.0/24) is considered as public subnet?
Note:
In AWS world, associating a subnet to a public route table makes it a public subnet. Route table entry having outbound traffic to IGW indicates a route table, a public route table
All VPCs in AWS are assigned a CIDR range of IP addresses. In the above example, the VPC has been assigned a CIDR of 10.0.0.0/16.
All resources launched in a VPC (eg EC2 instances, RDS databases, Elastic Network Interfaces) are assigned an IP address within that CIDR range.
Within AWS, a public subnet is one that has a Route Table entry that directs traffic destined for 0.0.0.0/0 to an Internet Gateway. This gives the resources on that VPC the ability to communicate with the Internet. The resources will need to be assigned a Public IP address to be able to communicate with the Internet.
A private subnet is defined as a subnet that does not have a Route Table entry that directs traffic to an Internet Gateway. There is no capability for resources in a private subnet to communicate directly with the Internet, and vice versa.
As shown in your diagram, 10.0.0.0/24 is considered public due to its Route Table entry that points to the Internet Gateway.
The concept of "public/private subnets" is separate to the concept of "public/private IP address ranges".
The terminology used by AWS here has nothing to do with IANA or the RFC 2918 up ranges. AWS uses public subnets to define a subnet within the VPC that can be exposed to requests from the internet, like a webserver front end.
The private subnet is more isolated from the internet, it can only receive requests from other hosts on the vpc. This reduces the attack surface of hosts that are not meant to accept requests from the internet directly such as with application backends.
You can read a little more here on the AWS public docs

What public IP address will an instance have which is running in a private VPC in AWS?

One of my instances in a private subnet in a VPC in AWS , is making requests to a API (to fetch some metadata). The API provider is asking for an IP address to whitelist.
I'm not sure which IP address should I be providing since the instance is a part of an auto scaling group. It does have an ENI IP , however I'm not sure if that's the IP which they would need to whitelist. Could it be the NAT gateway IP (since the instances use a NAT gateway to connect to the internet) ?
So you have an instance in private subnet and also in auto scaling group. which needs to be whitelisted. What you can do is that you attach a NAT Gateway to the private subnet. This NAT gateway will have an public elastic IP which you can whitelist from API side.
Private Subnet are not meant to be having a public IP address, assigning public address would nullify the benefit that you would like to get from Private subnet.
However, Many users above about suggested correct option. By having a NAT gateway ( with elastic IP address ) and then configuring the private routing table with NAT gateway would allow the internet traffic ( from allowed IP address configured in private routing table) . Since all outgoing traffic would be going through the NAT gateway you can use the elastic IP address attached with NAT gateway to get whitelisted the API Provider.
You mean to say, your VPC have private subnet and public subnet. Your EC2 instances are in private subnet and you want to whitelist IP address of those EC2 instances. You can't have public IP addresses of EC2 in private instance. If you have private and public subnet architecture then you should set up ELB into public subnet and attach it to auto scaling group of EC2 instances into private subnet.
Check link about auto scaling group.
The NAT gateway has different purpose, you can't share NAT ip for whitelisting. Refer link to see, how Auto Scaling group created and how ELB attached to it.
You should share ELB IP address to provider for whitelisting.

How an EC2 access another EC2 in the same VPC using public IP?

I find this
Can an AWS private IP address server connect to a public IP address server that is in the same VPC?
Both my two EC2s have public IP, and work fine, I have no NAT instance.
How do I
Make sure the security group of the server with public IP accepts inbound traffic from the VPC NAT
Your question is a bit unclear about your current configuration, but let's say you have:
An Amazon VPC with one public subnet
Two instances both in the public subnet
They each have Public IP addresses assigned
Yes, the two instances can communicate directly with each other. It is best that they communicate via the private IP address -- this way, the traffic remains totally within the VPC.
If they communicate via their public IP addresses, then the traffic goes out of the VPC to the AWS edge of the Internet, then back into the VPC. Such traffic is charged at 1c/GB.
All instances within a VPC can communicate directly with each other via their private IP addresses, even if they are in different subnets. However, the Security Group will need to be configured to accept the incoming traffic. This can be configured based on the IP address of the source instance, or by reference to a security group that is associated with the source instance.