What does public subnet mean in AWS VPC? - amazon-web-services

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

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.

Is it possible to create a VPC with only a public subnet and NAT for EC2 instances without elastic IPs?

I need to migrate a big cluster of VMs to AWS. Some of these have Public IPs, others only local IPs. Is it possible to create mixed subnetwork of Public and Private IPs within a VPC such that instances with a Public IP access the Internet via an Elastic IP and the Internet Gateway and such that instances that don't have a Public IP go to the Internet over NAT. The local IP instances must be in a single subnetwork such as 192.168.1.0/24.
Is it possible how to do this? and what would be an optimal approach?
By definition, a public subnet has a route to an Internet Gateway. Anything in that subnet that needs to access the Internet will need a public IP (not necessarily an Elastic IP, just a public IP) assigned to it.
If a subnet's traffic is routed to an internet gateway, the subnet is known as a public subnet.
Similarly, by definition a private subnet is a subnet without an Internet Gateway, which relies on route to a NAT Gateway to access the Internet.
If a subnet doesn't have a route to the internet gateway, the subnet
is known as a private subnet.
Per your question, no, there's no way to have a single route table that routes traffic to both an Internet Gateway and a NAT Gateway based on the presence or lack of a public IP address on the source server.

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.

AWS Public Subnet Internet Access for Non Public IP Instances

I have a public subnet in AWS and I have 3 instances in it ...
WebApp01 (Elastic IP - 54.23.61.239 for example)
WebApp02 (Private IP - 192.168.0.24)
WebApp03 (Private IP - 192.168.0.25)
And my route table is setup as
192.168.0.0/16 -> local
0.0.0.0/0 -> Internet Gateway
I can see that the instance that has the public IP has internet access but the instances that don't have public IP are not able to access the internet.
How can I give internet access to the other instances inside the Public Subnet ?
I'm a newbie in networking and any help will be appreciated.
Just FYI : I know that creating a NAT, and then creating a separate route table with 0.0.0.0 -> NAT and associating that route with the Private Subnet gives internet access to the instances in the private subnet, but I cant figure out how to give internet access to the non public IP instances in the public subnet. Please help !
You will need to assign public IP addresses to your instances that do not have one or add an EIP in order for them to access the Internet.
An AWS Internet Gateway is a special type of NAT Gateway (1 - 1 address mapping). Without a public IP address there is nothing for the Internet Gateway to map to the EC2 instance - one public IP maps to one private IP inside the Internet Gateway.
Although you add a NAT Gateway to a public subnet with an Internet Gateway,
the NAT Gateway does provide address translation (NAT) to instances in the public subnet - only to instances in the private subnets. The default route in each private subnet points to the NAT Gateway.
If you want these instances to be protected by only having private IP addresses then you will need to move the instances to a private subnet (one with a NAT Gateway).
An instance in a public subnet without a public IP address is orphaned from the Internet. The instance can talk to other instances in the VPC (the Intranet) but cannot talk to the Internet.
There is a lot of confusion on what are AWS VPC subnets. There are three types. 1) Public subnet (one that has an IGW). 2) Private subnet (one that has a NAT Gateway or NAT instance, or neither). 3) Hybrid (one that has a VGW routing to a data center or similar).

AWS: Why does an Internet Gateway perform NAT?

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.