Sub topic 5
VPCs, subnets, and IP addresses for instances
This chapter assumes you know what CIDR notation means (for example, that 10.0.0.0/16 describes a block of addresses). It also assumes you know the difference between a public and a private IP address, and that you have read the chapter on Regions and Availability Zones.
The VPC as the instance's network
A Virtual Private Cloud (VPC) is a private, logically isolated network that you define inside one Region. You choose its IPv4 CIDR block, for example 10.0.0.0/16, and every instance you launch gets its address from that range. A VPC spans all Availability Zones (AZs) in its Region. Nothing inside it is reachable from the internet until you add a path.
An instance does not attach to a VPC directly. It attaches to a subnet, and the subnet determines the instance's AZ, address range, and routing.
Subnets and route tables
A subnet is a slice of the VPC's CIDR block that lives in exactly one AZ. It cannot span AZs. To survive an AZ failure, you create subnets in at least two AZs and spread instances across them.
AWS reserves five addresses in every subnet: the first four and the last one. A /24 has 256 addresses but only 251 are usable.
Whether a subnet is "public" or "private" depends on its route table, not on a setting called "public":
- A public subnet has a route that sends
0.0.0.0/0to an internet gateway (IGW). - A private subnet has no route to an IGW. Its instances can reach the internet outbound only through a NAT gateway placed in a public subnet.
Each route table also contains an implicit local route that lets all subnets in the VPC talk to each other.
Building a public subnet with the CLI
The default VPC in each Region already has this setup: a 172.31.0.0/16 range, one /20 subnet per AZ, an attached IGW, and automatic public IPv4 assignment. To see the parts, build the same thing yourself.
VPC_ID=$(aws ec2 create-vpc \
--cidr-block 10.0.0.0/16 \
--tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=tutorial-vpc}]' \
--query Vpc.VpcId --output text)
SUBNET_ID=$(aws ec2 create-subnet \
--vpc-id "$VPC_ID" \
--cidr-block 10.0.1.0/24 \
--availability-zone us-east-1a \
--query Subnet.SubnetId --output text)
# New instances in this subnet get a public IPv4 address by default
aws ec2 modify-subnet-attribute \
--subnet-id "$SUBNET_ID" --map-public-ip-on-launch
IGW_ID=$(aws ec2 create-internet-gateway \
--query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway \
--internet-gateway-id "$IGW_ID" --vpc-id "$VPC_ID"
RTB_ID=$(aws ec2 create-route-table \
--vpc-id "$VPC_ID" \
--query RouteTable.RouteTableId --output text)
aws ec2 create-route \
--route-table-id "$RTB_ID" \
--destination-cidr-block 0.0.0.0/0 \
--gateway-id "$IGW_ID"
aws ec2 associate-route-table \
--route-table-id "$RTB_ID" --subnet-id "$SUBNET_ID"The subnet becomes public only at the last two commands. Without the route and the association, the subnet would use the VPC's main route table, which has only the local route.
The addresses an instance can have
An instance can hold several kinds of address. Each one behaves differently across the lifecycle.
| Address | Where it comes from | Visible inside the OS | Survives stop and start | Released when |
|---|---|---|---|---|
| Primary private IPv4 | Subnet CIDR, assigned at launch | Yes | Yes | Instance terminates |
| Auto-assigned public IPv4 | AWS public pool | No | No, a new one is assigned | Instance stops or terminates |
| Elastic IP | Your account, allocated by you | No | Yes, while associated | You release it |
| IPv6 (optional) | Subnet's IPv6 CIDR | Yes | Yes | Instance terminates |
Two points cause most confusion:
- The public IPv4 address is not configured on the instance. The internet gateway performs one-to-one NAT between the public and private addresses. Running
ip addron the instance shows only the private address. To find the public one from inside, use the metadata service. - Use the private IP for traffic between instances in the same VPC. It stays inside AWS's network and does not incur public IPv4 charges.
AWS bills for every public IPv4 address, whether auto-assigned or Elastic, for each hour it is in use. A forgotten address costs money.
Controlling public IP assignment at launch
The subnet's map-public-ip-on-launch setting is only the default. A launch can override it per instance with the network-interface option:
aws ec2 run-instances \
--image-id "$AMI_ID" \
--instance-type t3.micro \
--key-name my-key \
--network-interfaces \
"AssociatePublicIpAddress=true,DeviceIndex=0,SubnetId=$SUBNET_ID,Groups=sg-0123456789abcdef0"Set AssociatePublicIpAddress=false to launch a private-only instance in a public subnet. Security groups go inside --network-interfaces here, because the API rejects instance-level --security-group-ids together with an explicit interface.
Inspecting addresses and capacity
aws ec2 describe-instances \
--filters Name=tag:Name,Values=tutorial-web \
--query 'Reservations[].Instances[].[InstanceId,SubnetId,PrivateIpAddress,PublicIpAddress]' \
--output table
aws ec2 describe-subnets \
--subnet-ids "$SUBNET_ID" \
--query 'Subnets[].[SubnetId,CidrBlock,AvailabilityZone,AvailableIpAddressCount]' \
--output tablePublicIpAddress is empty (shown as None) for stopped instances and for instances without a public address. AvailableIpAddressCount tells you how much room is left before launches fail.
Common errors and edge cases
- SSH times out on an instance that has a public IP. Check three things in order: the subnet's route table has
0.0.0.0/0to an IGW, the security group allows your source address on port 22, and the instance really has a public IPv4 address. InsufficientFreeAddressesInSubnet. The subnet is full. A subnet's CIDR cannot be resized, so plan for growth. Use a larger block, or create another subnet.- Overlapping CIDRs. Two VPCs with overlapping ranges cannot be peered. Choose non-overlapping ranges early, since you cannot change a VPC's primary CIDR later.
- Wrong AZ. An instance cannot move to another subnet after launch. To relocate, create an AMI and launch a new instance in the target subnet.
- A security group from another VPC. Security groups belong to one VPC. Referencing one from a different VPC fails at launch.
- Private instance cannot install packages. It has no route to the internet. Add a NAT gateway and a
0.0.0.0/0route to it from the private subnet's route table. NAT gateways carry hourly and data-processing charges.
Exercise
An instance in a subnet with auto-assign public IPv4 enabled shows 203.0.113.25 as its public address. You stop it and start it again. What happens to its private and public addresses, and what would you use if the public address must stay fixed?
Show answer
The private IPv4 address stays the same, because it belongs to the instance until termination. The auto-assigned public address is released on stop, and a different one is assigned on start. To keep a fixed public address, allocate an Elastic IP and associate it with the instance.
Exercise
You created a subnet and an internet gateway and attached the gateway to the VPC. An instance launched in the subnet with a public IP still cannot be reached from the internet. Which resource is missing, and which command adds it?
Show answer
A route sending 0.0.0.0/0 to the internet gateway, in a route table associated with the subnet. Create it with aws ec2 create-route --route-table-id RTB --destination-cidr-block 0.0.0.0/0 --gateway-id IGW, then run aws ec2 associate-route-table for the subnet if the table is not yet associated.