Sub topic 5
Public subnets and the internet gateway
This chapter assumes you know CIDR notation, can read a route table, and have a VPC with at least one subnet, as built in the earlier chapters. It covers what makes a subnet "public", how the internet gateway (IGW) works, and the errors that most often leave an instance unreachable.
What "public" means
AWS has no "public" flag on a subnet. A subnet is public when all of these hold:
- An internet gateway is attached to the VPC.
- The subnet's route table has a route to that gateway, normally
0.0.0.0/0. - The instance has a public IPv4 address or an Elastic IP.
- Security groups and network ACLs allow the traffic.
If any one is missing, the instance cannot talk to the internet directly. The route in step 2 is the defining part. A subnet with that route is public. A subnet without it is private, whatever its name or tags say.
What the internet gateway does
An internet gateway is a horizontally scaled, redundant VPC component. It has no bandwidth limit that you manage, and there is no hourly charge for it. It does two jobs:
- It is a route target for internet-bound traffic from your subnets.
- It performs one-to-one network address translation (NAT) for instances that have public IPv4 addresses.
The second job explains a detail that surprises many people. The instance's network interface only knows its private address, such as 10.0.1.25. Run ip addr on it and you will not see the public IP. The IGW rewrites the source address to the public IP on outbound packets and rewrites the destination back to the private IP on inbound packets.
A VPC can have only one internet gateway attached at a time, and a gateway can be attached to only one VPC.
Building a public subnet
The commands below continue from the VPC and subnets created earlier in this tutorial. They assume VPC_ID and PUBLIC_SUBNET_ID are set in your shell.
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"
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"
aws ec2 modify-subnet-attribute --subnet-id "$PUBLIC_SUBNET_ID" --map-public-ip-on-launchEach step has a purpose:
create-internet-gatewaymakes a detached gateway. It does nothing until you attach it.attach-internet-gatewaybinds it to the VPC.create-routeadds the default route. The destination0.0.0.0/0matches anything that no more specific route matches. The local route for the VPC CIDR is more specific, so traffic between your subnets still stays inside the VPC.associate-route-tablelinks the table to the subnet. Without this step, the subnet keeps using the main route table.modify-subnet-attribute --map-public-ip-on-launchmakes new instances in the subnet receive an auto-assigned public IPv4 address.
Use a dedicated route table
You could add the IGW route to the VPC's main route table. Every subnet without an explicit association uses the main table, so any subnet you create later would become public by accident. Keep the main table free of internet routes and associate a dedicated table only with the subnets that should be public.
Public IP addresses
An instance needs a public IPv4 address for IPv4 internet traffic through the IGW. There are two kinds:
| Kind | How you get it | Behavior |
|---|---|---|
| Auto-assigned public IP | Subnet setting or a launch option | Released when the instance stops or terminates. A restart gives a different address. |
| Elastic IP | `aws ec2 allocate-address`, then associate | Stays yours until you release it. You can move it between instances. |
Use an Elastic IP when something outside your control, such as a DNS record or a partner's allow list, depends on a fixed address. Public IPv4 addresses, including those in use, are charged hourly. An Elastic IP that is allocated but not attached also costs money, so release the ones you no longer need.
You can override the subnet default at launch. Passing --associate-public-ip-address or --no-associate-public-ip-address to aws ec2 run-instances takes precedence over the subnet's map-public-ip-on-launch setting.
Verifying the setup
Check that the route exists and is active:
aws ec2 describe-route-tables \
--route-table-ids "$PUBLIC_RT_ID" \
--query 'RouteTables[0].Routes'You should see two routes: the local route for 10.0.0.0/16, and a 0.0.0.0/0 route with your GatewayId and state active. If the state is blackhole, the target no longer exists, usually because the gateway was detached or deleted.
To confirm an instance has the right addressing, inspect it:
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[0].Instances[0].[SubnetId,PrivateIpAddress,PublicIpAddress]' \
--output textA None in the public IP column explains most "my instance can't reach the internet" reports.
Troubleshooting checklist
Work through the layers in order. The first failing item is usually the cause.
- Gateway attached? Run
aws ec2 describe-internet-gatewaysand confirm the attachment state isavailablefor your VPC. - Route present and associated? Confirm the subnet is explicitly associated with the table that has the
0.0.0.0/0route. The console's "Subnet associations" tab shows this. A subnet you thought was associated may be using the main table. - Public address present? Check the instance for a public IPv4 address or Elastic IP.
- Security group allows it? Inbound rules must permit the port you connect to, such as TCP 22 or 443. Security groups are stateful, so return traffic is allowed automatically. Outbound rules matter too, and the default group allows all outbound.
- Network ACL allows it? The default network ACL allows everything. A custom one is stateless, so you must allow inbound traffic and the outbound ephemeral ports (1024-65535) used for replies.
- Operating system firewall or service? Confirm the application is listening on the port and that no host firewall blocks it.
Common errors
- Created the IGW but never attached it. The route creation will fail, or the route will not work as expected. Attach first.
- Route added to the wrong table. The table exists and has the route, but your subnet is not associated with it.
- Public IP assigned after the fact. The auto-assign setting applies only at launch. Instances launched before you changed the subnet attribute do not get a public address. Associate an Elastic IP instead.
- Expecting a secondary network interface to have a public IP. Auto-assigned public IPs apply only to an instance launched with a single network interface. With more than one interface, associate Elastic IPs yourself.
- Detaching the gateway fails. AWS refuses to detach an IGW while any resource in the VPC still has a public IP mapped to it. Release or disassociate those addresses first.
Design guidance
Keep the public tier small. A typical layout puts load balancers and NAT gateways in public subnets and everything else in private ones. Create one public subnet per Availability Zone, all associated with the same public route table, because a route table can be shared by many subnets. Because the IGW is a regional, redundant resource, a single gateway serves every AZ in the VPC and is not a single point of failure.
If an instance only needs to reach the internet and does not need to accept connections from it, you do not need to give it a public IP. Place it in a private subnet and use a NAT gateway, which later chapters cover.
Exercise
An instance launched in a subnet whose route table has 0.0.0.0/0 -> igw-abc123 cannot be reached from the internet on port 443. Its security group allows inbound TCP 443 from 0.0.0.0/0, and the network ACL is the default one. describe-instances shows PublicIpAddress as None. What is the cause, and what are two ways to fix it?
Show answer
The instance has no public IPv4 address, so the internet gateway has nothing to translate and the instance is not addressable from the internet. The subnet's auto-assign setting was probably off at launch. Fix it by associating an Elastic IP with the instance (allocate-address, then associate-address), or by launching a replacement with --associate-public-ip-address, or with the subnet's map-public-ip-on-launch turned on. Changing the subnet setting alone does not affect the running instance.
Exercise
Why is it risky to add the 0.0.0.0/0 route to the VPC's main route table?
Show answer
Subnets with no explicit route table association use the main table. Any subnet you create later would silently inherit the internet route and become public. A dedicated route table, associated only with the intended public subnets, avoids this.