Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Servers, nginx and Docker

An EC2 instance in a public subnet still cannot reach the internet

Your error

dnf, curl or pip on the instance wait and then time out, but other instances in the same subnet work1 more line it can show

Fix it fast

Most common cause, 1 of 3

The instance has no public IPv4 address

Give the running instance an Elastic IP.

Fix
ALLOC=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
aws ec2 associate-address --instance-id i-0123456789abcdef0 --allocation-id "$ALLOC"
# for instances launched later in this subnet
aws ec2 modify-subnet-attribute --subnet-id subnet-0123456789abcdef0 --map-public-ip-on-launch
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

An instance in a public subnet gets internet access through the internet gateway only when it has a public IPv4 address (auto-assigned or an Elastic IP) or an IPv6 address. Nondefault subnets assign none by default, so outbound requests hang. Attach an Elastic IP or route through a NAT gateway, then check the 0.0.0.0/0 route and the return-traffic rules.

  • Applies to Amazon VPC (IPv4, internet gateway), Amazon EC2 instances on Amazon Linux 2023, AWS CLI 2.42
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Ask EC2 for the instance's public address and subnet. An empty public address answers the question at once; a present one sends you to the route and rule checks.

Run this first
aws ec2 describe-instances --instance-ids i-0123456789abcdef0 \
  --query 'Reservations[].Instances[].[PublicIpAddress,PrivateIpAddress,SubnetId,VpcId]' --output text

Then find the line you see in this list and open its fix:

  1. 1The instance has no public IPv4 addressPublicIpAddress comes back as None, and the subnet's MapPublicIpOnLaunch is false
  2. 2The subnet is not really public: no 0.0.0.0/0 route to an internet gatewayThe instance has a public address, but its subnet's route table (or the VPC's main table) has only the local route
  3. 3A custom network ACL or a trimmed security group drops the repliesPublic address and route are both in place, names still resolve on the instance, yet HTTP and HTTPS requests hang

What does the error look like?

  • dnf, curl or pip on the instance wait and then time out, but other instances in the same subnet work
  • The console shows no Public IPv4 address for the instance, although its subnet routes 0.0.0.0/0 to an internet gateway

Cause 1 of 3

The instance has no public IPv4 address

You seePublicIpAddress comes back as None, and the subnet's MapPublicIpOnLaunch is false

Check

An EC2 instance without a public IPv4 address cannot use the internet gateway for IPv4, even in a subnet whose route table points 0.0.0.0/0 at that gateway. The gateway does one-to-one address translation for the instance, so it needs a public address to translate to. Default subnets hand one out at launch; subnets you create do not, unless their auto-assign setting is on.

Check
aws ec2 describe-subnets --subnet-ids subnet-0123456789abcdef0 \
  --query 'Subnets[].[MapPublicIpOnLaunch,DefaultForAz]'

Fix

Give the running instance an Elastic IP. For instances you launch later, turn on auto-assign for the subnet or choose a public IP in the launch settings. If the instance should stay private, move it to a private subnet whose default route goes to a NAT gateway. AWS bills every public IPv4 address, Elastic IPs and auto-assigned ones alike, so pick the option you need.

Fix
ALLOC=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
aws ec2 associate-address --instance-id i-0123456789abcdef0 --allocation-id "$ALLOC"
# for instances launched later in this subnet
aws ec2 modify-subnet-attribute --subnet-id subnet-0123456789abcdef0 --map-public-ip-on-launch

Verify

describe-instances now shows a public address, and a request from the instance gets an HTTP status line back.

Verify
curl -sS -m 10 -I https://aws.amazon.com | head -n 1

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 3

The subnet is not really public: no 0.0.0.0/0 route to an internet gateway

You seeThe instance has a public address, but its subnet's route table (or the VPC's main table) has only the local route

Check

A subnet counts as public only when its route table sends internet traffic to an internet gateway. A subnet with no explicit association uses the VPC's main route table, which in a nondefault VPC has no such route, and a nondefault VPC has no internet gateway until you attach one. A 0.0.0.0/0 route in the blackhole state points at a gateway that is no longer attached.

Check
aws ec2 describe-route-tables --filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
  --query 'RouteTables[].Routes[].[DestinationCidrBlock,GatewayId,State]'
# empty output: the subnet uses the main route table
aws ec2 describe-route-tables --filters Name=vpc-id,Values=vpc-0123456789abcdef0 Name=association.main,Values=true \
  --query 'RouteTables[].Routes[].[DestinationCidrBlock,GatewayId]'
aws ec2 describe-internet-gateways --filters Name=attachment.vpc-id,Values=vpc-0123456789abcdef0

Fix

When describe-internet-gateways lists nothing, create a gateway and attach it first. Then put the default route into whichever route table this subnet is really associated with.

Fix
aws ec2 create-route --route-table-id rtb-0123456789abcdef0 \
  --destination-cidr-block 0.0.0.0/0 --gateway-id igw-0123456789abcdef0

Verify

The route list shows 0.0.0.0/0 with an igw- target, and the curl test from the instance answers.

Verify
aws ec2 describe-route-tables --route-table-ids rtb-0123456789abcdef0 --query 'RouteTables[].Routes[].[DestinationCidrBlock,GatewayId,State]'

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 3

A custom network ACL or a trimmed security group drops the replies

You seePublic address and route are both in place, names still resolve on the instance, yet HTTP and HTTPS requests hang

Check

Names keep resolving here because neither network ACLs nor security groups can block the Amazon DNS resolver, so DNS working proves nothing about the path. A custom network ACL is stateless and denies whatever no numbered rule allows; for requests the instance starts, it needs an inbound rule for the ephemeral ports the replies come back to. A security group whose default allow-all outbound rule was removed blocks the request before it leaves.

Check
getent hosts aws.amazon.com
aws ec2 describe-network-acls --filters Name=association.subnet-id,Values=subnet-0123456789abcdef0 \
  --query 'NetworkAcls[].Entries[].[RuleNumber,Egress,Protocol,PortRange.From,PortRange.To,CidrBlock,RuleAction]' --output table
aws ec2 describe-security-groups --group-ids sg-0123456789abcdef0 --query 'SecurityGroups[].IpPermissionsEgress'

Fix

In the subnet's ACL, allow outbound TCP 80 and 443 and inbound TCP 1024-65535 for the replies (the Amazon Linux kernel uses 32768-61000, and AWS suggests 1024-65535 to cover every client). Give the security group an outbound rule for the ports the instance must reach.

Fix
aws ec2 create-network-acl-entry --network-acl-id acl-0123456789abcdef0 --ingress \
  --rule-number 140 --protocol tcp --port-range From=1024,To=65535 \
  --cidr-block 0.0.0.0/0 --rule-action allow
aws ec2 create-network-acl-entry --network-acl-id acl-0123456789abcdef0 --egress \
  --rule-number 110 --protocol tcp --port-range From=443,To=443 \
  --cidr-block 0.0.0.0/0 --rule-action allow

Verify

The curl test prints a status line within a second or two instead of waiting for the timeout.

Verify
curl -sS -m 10 -I https://aws.amazon.com | head -n 1

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Decide per subnet whether instances get public addresses, set MapPublicIpOnLaunch in your infrastructure code, and name subnets public or private to match their route tables.
  • Give private workloads a NAT gateway route instead of public addresses; AWS lists that as one way to reduce how many public IPv4 addresses you hold.
  • Leave the default network ACL in place unless you need subnet-level rules, and when you add one, write the ephemeral-port return rule in the same change.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: Dispatch Pro, Raheeb, Loade Driver.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.