AWS VPC packet flow simulator
A connection in a VPC has to get past a security group, a network ACL and a route table on the way out, the same again on the way in, and the network ACLs once more for the reply. Pick a connection, edit the rules, and see every check it passes or the one rule that drops it.
Edit route tables, network ACLs and security groups
Routes: destination target, targets local, igw, nat, vpce, pcx, blackhole. ACL rules: in|out number allow|deny tcp|udp|icmp|all ports cidr. Security group rules: in|out protocol ports cidr or sg:name. Subnets: public 10.0.1.0/24, private 10.0.2.0/24, data 10.0.3.0/24.
- ✓security group app (app server)outbound tcp/443 to 203.0.113.10allowed by: out all all 0.0.0.0/0
- ✓private subnet network ACLoutbound tcp port 443 to 203.0.113.10rule out 100 allow all all 0.0.0.0/0
- ✓private subnet route tableroute to 203.0.113.10best match: 0.0.0.0/0 nat
- ✓public subnet network ACLinbound tcp port 443 from 10.0.2.10rule in 100 allow tcp 443 0.0.0.0/0
- ✓public subnet route table (NAT gateway)route to 203.0.113.10best match: 0.0.0.0/0 igw
- ✓public subnet network ACLoutbound tcp port 443 to 203.0.113.10rule out 100 allow all all 0.0.0.0/0
- ✓NAT gatewaytranslate 10.0.2.10 to 198.51.100.5NAT gateways have no security group; the connection leaves from the gateway's Elastic IP
- ✓Reply · public subnet network ACLinbound tcp port 1024 from 203.0.113.10rule in 120 allow tcp 1024-65535 0.0.0.0/0
- ✓Reply · public subnet network ACLoutbound tcp port 50000 to 10.0.2.10rule out 100 allow all all 0.0.0.0/0
- ✓Reply · private subnet network ACLinbound tcp port 50000 from 203.0.113.10rule in 110 allow tcp 1024-65535 0.0.0.0/0
- ✓Reply · security groupsreplystateful: the reply is allowed
Reachable, both ways. The connection gets through and so does its reply.
- reachable
- reachable
- reachable
- security group db (database)
- reachable
- reachable
- data subnet network ACL
- app server: public IPv4 address
- reachable
| web server | 10.0.1.10, public 198.51.100.20 |
| app server | 10.0.2.10 |
| database | 10.0.3.10 |
| internet host | 203.0.113.10 |
| S3 (through prefix list pl-s3) | 192.0.2.50 |
| host in peered VPC | 10.1.0.20 |
| NAT gateway | 10.0.1.5, Elastic IP 198.51.100.5 |
How to use the simulator
The VPC has three subnets: a public one with a web server and a NAT gateway, a private one with an application server, and a data subnet with a database. Choose a connection by its source, destination, protocol and port, or click one of the examples; the diagram draws its path and the list shows each check in order, including the checks on the reply. Open the editor to change routes, network ACL rules and security group rules, or start from one of the broken configurations.
The path of a packet
A packet leaving an instance in an AWS VPC is checked first by the instance's security group outbound rules, then, if it leaves the subnet, by the subnet's network ACL outbound rules. The subnet's route table decides where it goes: another subnet through the local route, an internet gateway, a NAT gateway, an endpoint or a peering connection. Entering the destination subnet it meets that subnet's network ACL inbound rules, and then the destination's security group inbound rules. The reply travels back through the network ACLs again, while the security groups let it through on their own.
Route tables and longest prefix match
Every subnet is associated with one route table. Every route table has a local route for the VPC's own range, so subnets can always reach each other as far as routing is concerned. For any other destination the table uses the most specific matching route: in the private subnet, 0.0.0.0/0 points to the NAT gateway, but the S3 prefix list added by a gateway endpoint and the peered VPC's 10.1.0.0/16 are more specific and win for those addresses. Add a more specific blackhole route and it beats the default route too: in the blackhole configuration the app's connection to the internet host fails with "best match 203.0.113.0/24 blackhole is a blackhole route".
Security groups and network ACLs
Security groups attach to network interfaces and contain only allow rules; anything not allowed is dropped. They are stateful: once a connection is allowed in one direction, its return traffic is allowed regardless of rules. Rules can name another security group instead of an address range, so the database's group here allows port 5432 from "anything in the app group" rather than from an address range that might change.
Network ACLs attach to subnets, have numbered allow and deny rules evaluated from the lowest number, stop at the first match, and deny anything that matches nothing. They are stateless and only see traffic crossing the subnet boundary. Both layers must allow a connection. In the example, the web server's connection to the database gets past the data subnet's ACL, because its rule for ephemeral ports happens to cover 5432 too, and is stopped by the database's security group: security group db (database).
Ephemeral ports and replies
The client side of a TCP connection uses a temporary ephemeral port, a high number chosen by its operating system, and the server's reply is addressed to it. Because network ACLs are stateless, every subnet a reply enters needs an inbound rule allowing those ports; the AWS documentation suggests 1024-65535 to cover the ranges different systems and NAT gateways use. This is the most common reason a network ACL change breaks something. The NACL without ephemeral ports configuration removes that rule from the private subnet, and app to the internet, app to s3, app to peered vpc all stop working, although the outgoing packets are still allowed.
Internet gateways, public IPs and NAT
A subnet is public if its route table sends internet-bound traffic to an internet gateway. The gateway translates between an instance's private address and its public IPv4 address, so an instance without one cannot use it, in either direction: app server has no public address, so nothing on the internet can address it; the internet gateway only translates public addresses of instances that have one.
Private instances reach the internet through a NAT gateway placed in a public subnet. The private subnet routes 0.0.0.0/0 to it; it sends the traffic on through the internet gateway from its own Elastic IP and passes replies back. Connections can only start from inside. A NAT gateway has no security group, but the public subnet's network ACL applies to traffic going into and out of it, in both directions, which is why the public subnet here allows traffic from the VPC and replies to ports 1024-65535.
Endpoints and peering
A gateway endpoint for S3 or DynamoDB adds a route for the service's prefix list to the route tables you choose, so private instances reach the service without a NAT gateway or a public address; an endpoint policy can restrict what they may do. A peering connection joins two VPCs: each side needs a route to the other's range through the peering connection, and each side's security groups and ACLs must allow the traffic. Peering is not transitive, so a VPC peered with two others does not connect them to each other.
About the model
The VPC, subnets and addresses are fixed; the rules are yours to change. Addresses outside the VPC come from documentation ranges, and the S3 prefix list is represented by one illustrative range. The peered VPC is assumed to route back and to allow the traffic. Replies use client port 50000 and, through the NAT gateway, port 1024, as examples from the ranges in use. IPv6, transit gateways, interface endpoints and load balancers are left out.
VPC networking questions and answers
What is the difference between a security group and a network ACL?
A security group belongs to an instance (its network interface), has only allow rules and is stateful: if a connection is allowed in one direction, its replies are allowed automatically. A network ACL belongs to a subnet, has numbered allow and deny rules checked in order with the first match deciding, and is stateless: replies are checked against the rules like any other packet.
Why do network ACLs need ephemeral port rules?
Because they are stateless, the reply to a connection is a separate packet that must be allowed on its way back. A server's reply goes to the client's ephemeral source port, a high number such as 50000, so the subnet the reply enters needs an inbound rule for those ports, commonly 1024-65535. In this page's example, removing that rule from the private subnet breaks the app's connections to the internet, S3 and the peered VPC.
How does a VPC route table choose a route?
It uses the most specific route that matches the destination address, the longest prefix. A route for 203.0.113.0/24 wins over 0.0.0.0/0 for addresses in that range, and the local route for the VPC's own range covers traffic between subnets. A gateway endpoint adds routes for the service's prefix list.
Why can't an instance in a private subnet reach the internet?
It has no public IPv4 address, and an internet gateway only translates addresses for instances that have one. Private subnets route 0.0.0.0/0 to a NAT gateway instead, which sits in a public subnet and sends traffic out from its own Elastic IP. Here, routing the private subnet straight to the internet gateway gives: app server has only a private address; an internet gateway needs a public one to translate to, so the packet goes nowhere (use a NAT gateway for private instances)
What makes a subnet public?
Its route table: a subnet is public when it has a route to an internet gateway. Instances in it still need a public IPv4 address to be reached from, or to reach, the internet directly.
Does a NAT gateway have a security group?
No. Traffic through a NAT gateway is controlled by the security groups of the instances using it and by the network ACLs of the subnets involved, including the public subnet the NAT gateway sits in, which must allow the traffic both on its way out and on its way back.
Further reading
- Amazon VPC User GuideNetwork ACLsRule numbering, statelessness and ephemeral ports.
- Amazon VPC User GuideSecurity groupsStateful allow rules and references to other groups.
- Amazon VPC User GuideRoute priorityLongest prefix match and how prefix lists take part.
- Amazon VPC User GuideNAT gatewaysPublic NAT gateways, Elastic IPs and the routes they need.