Sub topic 5
Public subnets and the internet gateway
This chapter assumes you know CIDR notation, can run AWS CLI commands, and have already created a VPC with subnets and read about route tables. It covers what makes a subnet public and how to build one.
What makes a subnet public
A subnet is public when its route table has a route to an internet gateway (IGW). The subnet's name and its position in the VPC do not matter. An internet gateway is a horizontally scaled, AWS-managed component that you attach to a VPC. It does two jobs:
- It is the target for routes that send internet-bound traffic out of the VPC.
- It translates the private IPv4 address of an instance to its public IPv4 address, and back again, for instances that have a public address.
Three conditions must all hold for an instance to reach the internet directly:
- The IGW is attached to the VPC.
- The subnet's route table has a route such as
0.0.0.0/0to the IGW. - The instance has a public IPv4 address or an Elastic IP, and its security group and network ACL allow the traffic.
Build a public subnet
This example assumes VPC_ID and PUBLIC_SUBNET_ID are set from the earlier subnet chapter. First create and attach the gateway.
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"A VPC can have only one internet gateway attached at a time. Next create a dedicated route table with a default route, and associate it with the subnet.
PUBLIC_RT_ID=$(aws ec2 create-route-table --vpc-id "$VPC_ID" --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id "$PUBLIC_RT_ID" --destination-cidr-block 0.0.0.0/0 --gateway-id "$IGW_ID"
aws ec2 associate-route-table --route-table-id "$PUBLIC_RT_ID" --subnet-id "$PUBLIC_SUBNET_ID"Finally, have the subnet assign a public IPv4 address to new instances.
aws ec2 modify-subnet-attribute --subnet-id "$PUBLIC_SUBNET_ID" --map-public-ip-on-launchThe VPC's implicit 10.0.0.0/16 local route still handles traffic inside the VPC. The more specific route always wins, so only traffic outside the VPC uses 0.0.0.0/0.
Public addresses
An instance's public IPv4 address is not configured inside the operating system. The IGW maps it to the instance's private address. Running ip addr on the instance shows only the private address.
- An auto-assigned public IP is released when the instance stops, and you may get a different one on restart.
- An Elastic IP is a static public address that you own until you release it. Use one when the address must not change.
Common errors
- Instance has no internet access. Check the three conditions in order. The usual cause is a subnet still associated with the main route table, which has no IGW route.
- Route shows
blackhole. The target was deleted or detached. Recreate the IGW and route. - Public IP assigned, but no response. The security group or network ACL blocks the traffic. Security groups deny inbound traffic by default.
- Cannot detach the IGW. It fails while any resource in the VPC still uses a public address. Release or disassociate those addresses first.
An IGW has no hourly charge, but public IPv4 addresses are billed. Place only instances that must be reachable from the internet, such as load balancers, in public subnets.
Exercise
An instance in a subnet has a public IPv4 address, and its security group allows outbound traffic. It still cannot reach the internet, and the IGW is attached to the VPC. What is the most likely missing piece, and how do you fix it?
Show answer
The subnet's route table probably has no 0.0.0.0/0 route to the IGW, for example because the subnet uses the main route table. Create a route table with aws ec2 create-route --route-table-id <rt> --destination-cidr-block 0.0.0.0/0 --gateway-id <igw> and associate it with the subnet using aws ec2 associate-route-table.