Sub topic 14
Troubleshooting blocked connections
This chapter assumes you know TCP/IP basics (CIDR, ports, protocols), how EC2 instances sit in VPC subnets, and how to run AWS CLI commands. It also assumes you have read the earlier chapters on stateful filtering, rule evaluation, security group references, and network ACLs. Here you will turn that model into a repeatable way to find out why a connection fails.
Start with the symptom
A failed connection usually shows up in one of three ways. Each points to a different part of the path.
| Symptom | Typical meaning |
|---|---|
| Client hangs, then times out | A packet was silently dropped. Common causes are a missing security group rule, a network ACL deny, a missing route, or a client-side firewall. |
| "Connection refused" returns immediately | The packet reached the instance, and nothing is listening on that port (or a host firewall rejected it). The security group is not the cause. |
| Connects, then fails at a later step | TCP works. Look at TLS, authentication, or the application. |
This distinction saves time. Security groups drop traffic without replying. So a security group problem looks like a timeout. If you get an immediate refusal, the network path is fine, and you should check the service itself.
The path a packet takes
A connection between two instances passes through several checks. Any one of them can block it.
For a client in your VPC, check both ends. The client's outbound rules must allow the traffic, and the server's inbound rules must allow it. Return traffic is handled by connection tracking, so you do not need a matching rule for the reply.
A systematic checklist
Work from the outside in. Change nothing until you have identified the layer that fails.
- Confirm the target. Check that you are using the right IP address (private or public), port, and protocol. A surprising number of problems are a wrong address or a stopped instance.
- Confirm the service is listening. On the instance, check for a listener on the port.
- Check the inbound security group rules on the destination's network interface.
- Check the outbound rules on the source's network interface.
- Check routing: route table, internet gateway, NAT gateway, and public IP assignment for internet traffic.
- Check network ACLs on both subnets, including ephemeral ports for return traffic.
- Check host firewalls such as
iptables,nftables,firewalld, or Windows Firewall.
Check that the service is listening
Run this on the server. It shows which addresses and ports have listeners.
sudo ss -tlnpLook for your port. A listener bound to 127.0.0.1:8080 accepts only local connections, so remote clients fail even with perfect security group rules. A listener bound to 0.0.0.0:8080 or a private address accepts remote traffic.
Inspect the security groups actually attached
Rules apply per network interface, and an interface can have several groups. Do not assume which groups are attached. Ask AWS.
aws ec2 describe-instances \
--instance-ids i-0123456789abcdef0 \
--query 'Reservations[].Instances[].NetworkInterfaces[].{Eni:NetworkInterfaceId,Groups:Groups[].GroupId,PrivateIp:PrivateIpAddress}'Then read the rules of each group.
aws ec2 describe-security-group-rules \
--filters Name=group-id,Values=sg-0123456789abcdef0 \
--query 'SecurityGroupRules[].{Egress:IsEgress,Proto:IpProtocol,From:FromPort,To:ToPort,Cidr:CidrIpv4,Ref:ReferencedGroupInfo.GroupId}'Remember that the rules of all attached groups are combined. A port is open if any one rule in any attached group allows it. A port is closed only if no rule in any attached group allows it.
Common rule mistakes to look for
- Wrong port or protocol. A rule for TCP 8080 does not allow UDP 8080, and it does not allow TCP 80.
- Source CIDR does not match the client. A
/32for your office address stops working when your ISP changes it. Compare the rule with your current public address. - Private versus public source. Traffic between instances in the same VPC arrives from the private address. A rule that allows the sender's public IP does not match it.
- Reference to the wrong group. A rule that references the web tier's group admits only interfaces that have that group attached. It does not admit an address range, and it does not match traffic sent to a public address of the instance.
- Missing outbound rule. If you replaced the default allow-all egress rule, the client needs an explicit outbound rule for the destination port.
- Rule added to the wrong group. The group you edited is not attached to the interface in question.
- Different VPC or peering limits. A security group reference works across peered VPCs only under specific conditions. Use a CIDR range if the reference fails.
Check the client's public address
When the source is an administrator's workstation, find the address AWS actually sees.
curl -s https://checkip.amazonaws.comCompare the output with the CIDR on the rule. If you are behind a corporate proxy or VPN, the visible address may differ from your local one.
Use tools that show where the drop happens
Reading rules by hand works for small cases. For a complicated path, use a tool that evaluates it for you.
VPC Reachability Analyzer
Reachability Analyzer examines the configuration between a source and a destination and reports whether the path is reachable. If it is not, it names the component that blocks it, for example a specific security group with no matching rule. It analyzes configuration, so it does not send packets. This makes it safe, and it also means it cannot detect a stopped service or a host firewall.
aws ec2 create-network-insights-path \
--source i-0aaaaaaaaaaaaaaaa \
--destination i-0bbbbbbbbbbbbbbbb \
--protocol tcp \
--destination-port 8080
aws ec2 start-network-insights-analysis \
--network-insights-path-id nip-0123456789abcdef0Then fetch the result with describe-network-insights-analyses. The NetworkPathFound field is true or false, and the Explanations section names the blocking component. Check the current pricing, because analyses are billed per run.
VPC Flow Logs
Flow logs record accepted and rejected traffic at the network interface. Look for records with REJECT for the destination address and port you are testing.
2 123456789010 eni-0123456789abcdef0 198.51.100.7 10.0.1.25 51234 8080 6 4 240 1700000000 1700000060 REJECT OKA REJECT here means a security group or network ACL refused the packet. Flow logs do not say which one. Use these rules to tell them apart:
- If the traffic is rejected at the destination interface, check that interface's inbound security group rules and the subnet's network ACL.
- If you see
ACCEPTinbound but no matching reply, suspect the application, host firewall, or a return-path problem. - If you see
REJECTon return traffic only, suspect a network ACL, since security groups are stateful and do not reject replies to allowed connections.
Flow logs are delayed by several minutes, so allow time before concluding that nothing was recorded.
Separate security groups from network ACLs
Both produce timeouts, so you must rule one out. Security groups are stateful and allow-only. Network ACLs are stateless, are ordered by rule number, and can deny.
A reliable test: if inbound traffic reaches the instance (flow logs show ACCEPT) but the client never sees a reply, the reply is probably blocked by an outbound network ACL that does not allow the ephemeral port range (1024-65535 for most clients). Security groups would never block the reply to an allowed connection.
Also check the default network ACL. It allows all traffic. A custom network ACL denies everything until you add rules, so a new custom ACL attached to a subnet can break a previously working setup.
Edge cases that cause confusion
- Rule changes apply quickly but not to existing connections the way you may expect. A new allow rule takes effect almost immediately. When you remove a rule, connections already tracked may continue until they end, because tracking state outlives the rule in some cases. Idle tracked connections also expire, so a long-lived connection can drop after inactivity. Test a removal with a new connection.
- Untracked traffic. Some flows are not tracked, for example traffic that a rule allows from all addresses on all ports. For those, the reply is allowed by the rule itself and not by tracking.
- Several interfaces on one instance. Each interface has its own groups. Traffic arriving at the second interface's address is judged by that interface's groups.
- Special addresses. Traffic to the instance metadata service and to the Amazon DNS resolver is not filtered by security groups.
- Load balancers. With an Application Load Balancer or Network Load Balancer, the targets must allow traffic from the load balancer's security group (or its private addresses) on the target port, and on the health check port if it differs. A failing health check marks targets unhealthy, and clients see 502 or 503 errors. A Network Load Balancer with client IP preservation presents the client's address to the target, so the rule must allow the client range.
- Deleting a group in use. You cannot delete a group that another group's rule references or that an interface uses. The error names the dependency.
A worked example
A web instance at 10.0.1.25 must reach an application instance on port 8080. The connection times out.
- On the application instance,
ss -tlnpshows a listener on0.0.0.0:8080. The service is fine. describe-security-group-rulesfor the application group shows a rule allowing TCP 8080 from10.0.1.0/24. The web instance is inside that range, so the inbound side looks correct.- Reachability Analyzer reports
NetworkPathFound: falseand names the application subnet's network ACL. - The ACL has an inbound allow for port 8080 and no outbound rule for ports 1024-65535. The reply is blocked.
- Adding an outbound allow for the ephemeral range fixes the connection.
Notice that the security group was correct and was not the cause. Checking each layer in order found the real fault.
Summary of what to check
| Layer | Question to ask | Tool |
|---|---|---|
| Application | Is it listening on the right address and port? | `ss -tlnp` |
| Host firewall | Does the OS allow the port? | `iptables -L`, `firewall-cmd` |
| Security group inbound | Does any attached group allow this source, protocol and port? | `describe-security-group-rules` |
| Security group outbound | Does the client's group allow the destination? | `describe-security-group-rules` |
| Network ACL | Are both directions and ephemeral ports allowed? | `describe-network-acls` |
| Routing | Is there a route and a public IP if needed? | `describe-route-tables` |
| Whole path | Which component blocks it? | Reachability Analyzer, flow logs |
Exercise
A client times out connecting to TCP 443 on a server. Flow logs show ACCEPT for the inbound packets at the server's interface, but the client never receives a reply. The server's security group allows 443 from the client. Which layer is the most likely cause, and why can it not be the security group?
Show answer
The most likely cause is a network ACL on the server's subnet (or the client's subnet) that blocks the outbound reply, typically because it lacks an allow rule for the ephemeral port range. Security groups are stateful, so once the inbound connection is allowed, the reply is permitted automatically and a security group cannot block it. Network ACLs are stateless and must allow the return traffic explicitly. A host firewall or the application not responding are the other possibilities to rule out.
Exercise
A client gets "connection refused" immediately when connecting to port 8080 on an instance. Should you edit the security group first? What do you check instead?
Show answer
No. A security group drops packets silently, which produces a timeout. An immediate refusal means the packet reached the instance and nothing accepted it. Check that the service is running and listening on port 8080 on a reachable address (not only 127.0.0.1) with ss -tlnp, and check the host firewall.