Sub topic 5
VPCs, subnets, and IP addresses for instances
This chapter assumes you know what CIDR blocks and ports are, and that you have read the chapter on Regions and Availability Zones. It covers the network an instance lives in and the addresses it receives.
The VPC and its subnets
A Virtual Private Cloud (VPC) is a private network that belongs to your account and exists in one Region. You give it an IPv4 CIDR block, such as 10.0.0.0/16. Every instance you launch goes into a VPC.
A subnet is a slice of the VPC's address range. Each subnet lives in exactly one Availability Zone. When you launch an instance, you choose a subnet, and that choice decides the instance's Availability Zone.
A subnet is public or private because of its route table:
- A public subnet has a route to an internet gateway, which is the VPC component that connects it to the internet.
- A private subnet has no such route. Its instances can reach the internet only through something like a NAT gateway, and the internet cannot start connections to them.
AWS reserves five addresses in every subnet: the first four and the last one. A /24 subnet therefore has 251 usable addresses, not 256.
Addresses an instance can have
An instance can have several kinds of IP address. They behave differently.
| Address | Scope | Lifetime |
|---|---|---|
| Private IPv4 | Inside the VPC | Stays with the instance until it is terminated |
| Public IPv4 (automatic) | Internet | Released on stop, so a new one is assigned on start |
| Elastic IP | Internet | Stays with your account until you release it |
| IPv6 | Internet, if enabled | Stays with the network interface |
The private address comes from the subnet's range. Other instances in the VPC use it to reach this instance.
The automatic public IPv4 address is assigned only if the subnet is set to auto-assign public IPs or you request one at launch. Inside the VPC, the internet gateway translates between the public and private addresses. The instance's operating system only ever sees the private one.
Because the automatic public address changes after a stop and start, do not hard-code it in DNS records or allow lists. The chapter on Elastic IPs covers the fix.
Seeing the addresses
You can look at the network details of an instance with the CLI:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[].Instances[].[SubnetId,VpcId,PrivateIpAddress,PublicIpAddress]' \
--output tablePublicIpAddress is empty for an instance in a private subnet, or one launched without a public address.
Common errors
- No connection to a public IP. The subnet may lack a route to the internet gateway, or the instance may have no public address. Check the route table before you check the security group.
- Launch fails with an address error. The subnet has run out of free addresses. Pick a larger subnet or another one.
- Wrong Availability Zone. You chose a subnet in a zone you did not intend. The subnet fixes the zone, so you must relaunch.
Exercise
An instance in a public subnet has an automatically assigned public IPv4 address. You stop it and start it again. What happens to its private and public addresses?
Show answer
The private IPv4 address stays the same. The automatic public IPv4 address is released on stop, so the instance gets a different one when it starts, unless you attached an Elastic IP.
Exercise
A subnet uses the CIDR block 10.0.5.0/24. How many addresses can you assign to instances, and why?
Show answer
251. A /24 holds 256 addresses, and AWS reserves five of them in every subnet.