VPC fundamentals
Subnets, route tables, internet and NAT gateways, security groups versus network ACLs, flow logs, IPv6, DNS settings and the tools that troubleshoot reachability.
Exam tasks: 1.1 (network design), 2.3 (network security controls), 3.3, 2.4
The decision: which packets may reach which resource, and by which path? Almost every networking question reduces to a route table, a security group, a network ACL, or a gateway that is missing.
A VPC is a private network in one Region that spans its Availability Zones. A subnet lives in exactly one AZ.
Subnets and routing
- A subnet is public only because its route table has a route to an internet gateway. A private subnet has no such route.
- Each subnet uses one route table. The local route covers the VPC CIDR and cannot be removed. The most specific route wins, and for equal prefixes a static route beats a propagated one.
- AWS reserves the first four addresses and the last address of each subnet, so a /28 has 11 usable addresses.
- You can add secondary CIDR blocks to a VPC. You cannot resize the primary one.
- Gateway route tables let you steer inbound traffic through a security appliance by attaching a route table to an internet gateway or virtual private gateway.
- Managed prefix lists group CIDRs into one named object for route tables and security groups, and can be shared with AWS RAM.
Getting traffic out
| Gateway | For | Remember |
|---|---|---|
| Internet gateway | Two-way internet access for resources with a public or Elastic IP | Horizontally scaled and not a single point of failure |
| NAT gateway | Outbound IPv4 from private subnets | Lives in a public subnet with an Elastic IP, scoped to one AZ. Create one per AZ and route each AZ's private subnets to its own |
| Egress-only internet gateway | Outbound-only IPv6 | The IPv6 equivalent of NAT, with no inbound connections |
| Gateway endpoint | S3 and DynamoDB | Route-table based and free |
| Interface endpoint (PrivateLink) | Most other AWS services and your own services | An ENI with private IPs, charged per hour and GB |
One NAT gateway for every AZ
A single NAT gateway makes every private subnet depend on one AZ, and it adds cross-AZ data charges. For resilience, deploy a NAT gateway in each AZ that has private subnets.
Security groups versus network ACLs
| Security group | Network ACL | |
|---|---|---|
| Applies to | An ENI, so an instance or resource | A subnet |
| State | Stateful | Stateless: allow the return traffic yourself |
| Rules | Allow only | Allow and deny |
| Evaluation | All rules together | In number order, first match wins |
| Default | Deny inbound, allow outbound. The default group allows traffic from itself | The default NACL allows all. A custom NACL denies all until you add rules |
| References | Can reference another security group | CIDR ranges only |
- A network ACL needs ephemeral port rules (1024 to 65535) for return traffic, because it does not track connections.
- To block one abusive IP address, use a network ACL deny or AWS WAF. A security group cannot deny.
- Reference a security group ID instead of CIDR blocks between tiers, so rules follow instances as they scale.
Exam signal
"Block a specific IP range" means network ACL (or WAF). "Allow the web tier to talk to the app tier and nothing else" means security groups that reference each other.
Seeing and testing the network
- VPC Flow Logs record metadata for accepted and rejected traffic at a VPC, subnet or interface, and publish to CloudWatch Logs, S3 or Data Firehose. They do not record traffic to the Amazon DNS server, the instance metadata address, DHCP, or the Windows license server.
- Reachability Analyzer analyzes the configuration between two points and shows which component blocks the path, without sending packets.
- Network Access Analyzer finds unintended access paths, for example resources reachable from the internet.
- Traffic Mirroring copies packets from a Nitro instance's interface to an appliance, for inspection and forensics.
- VPC IPAM plans, tracks and allocates CIDR blocks across accounts and Regions, and finds overlaps.
DNS and IP settings
enableDnsSupportturns on the Amazon-provided resolver.enableDnsHostnamesgives instances DNS names. Both are needed for private hosted zones and for private DNS on interface endpoints.- DHCP option sets set the domain name and DNS servers handed to instances, such as on-premises resolvers.
- An Elastic IP is a static public IPv4 address you can move between instances, and AWS charges for public IPv4 addresses.
- An ENI can hold several private IPs, move between instances in the same AZ, and keeps its MAC address and security groups.
- Dual-stack subnets add IPv6, and IPv6 addresses are public, so security groups and ACLs must allow only what you intend.
Scenarios
A NAT gateway must sit in a public subnet, which means that subnet's route table needs a route to the internet gateway. Without it the NAT gateway has no way out. A deny rule would block more, not less, and Elastic IPs on private instances are not how NAT works. The DNS hostnames setting does not stop outbound routing.
A custom network ACL denies everything not allowed, and since it does not track connections, the reply packets to the client's ephemeral port need an explicit outbound allow. Security groups are stateful, so the reply is already allowed there. The two work together, and internet gateways have no security groups.
Only network ACLs support deny rules, and one change at the subnet covers every instance. Security groups have no deny rules. Removing the internet gateway route takes the whole tier offline, and a NAT gateway is for outbound traffic.
Related
- VPC connectivity: peering, Transit Gateway and PrivateLink.
- VPC endpoints: gateway and interface endpoints in depth.
- Hybrid DNS: Route 53 Resolver endpoints.