Sub topic 16
Putting it together: a multi-tier VPC design
This chapter combines the pieces from earlier chapters into one design. It assumes you know CIDR notation, subnets, route tables, internet and NAT gateways, security groups, network ACLs, VPC endpoints, and flow logs. The goal is a layout where each tier can reach only what it needs.
The three-tier layout
A multi-tier VPC separates workloads by role. Each tier gets its own subnets and its own rules.
| Tier | Subnet type | Default route | Who may connect in |
|---|---|---|---|
| Web (load balancer) | Public | Internet gateway | The internet on 443 |
| App | Private | NAT gateway | Web tier security group |
| Database | Private, no internet route | None, or S3 endpoint only | App tier security group on 5432 |
Design rules
- Spread across Availability Zones. Create each tier's subnet in at least two zones. The chapter's earlier example used one zone to keep commands short. A production design repeats every subnet in a second zone.
- Use one route table per role. Public subnets share a table with an internet gateway route. Private app subnets use a table with a NAT route. Database subnets use a table with no default route.
- Reference security groups, not IP ranges. Allow the database only from the app security group. The rule keeps working when instances change addresses.
- Keep network ACLs simple. Leave them open by default, or add a few broad deny rules. Because they are stateless, you must also allow return traffic on ephemeral ports (1024-65535).
- Use endpoints for AWS services. A gateway endpoint for S3 keeps that traffic off the NAT gateway and reduces data processing charges.
- Turn on flow logs. They show which connections were accepted or rejected, which helps when a rule is too strict.
Building the tier rules
This example adds a web security group and chains the three tiers together. It assumes VPC_ID, APP_SG, and DB_SG already exist from the earlier chapters.
WEB_SG=$(aws ec2 create-security-group --group-name web-sg --description "Web tier" --vpc-id "$VPC_ID" --query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id "$WEB_SG" --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id "$APP_SG" --protocol tcp --port 8080 --source-group "$WEB_SG"
aws ec2 authorize-security-group-ingress --group-id "$DB_SG" --protocol tcp --port 5432 --source-group "$APP_SG"Each tier accepts traffic only from the tier in front of it. No rule lets the internet reach the app or database tier.
Common errors
- Private instances cannot reach the internet. Check that the private route table has a
0.0.0.0/0route to the NAT gateway, and that the NAT gateway sits in a public subnet. - A subnet you meant to be private is reachable. Look at its route table. Any subnet with a route to an internet gateway is public. A subnet with no explicit association uses the main route table.
- One NAT gateway is a single point of failure. If its zone fails, private subnets in other zones lose outbound access. Use one NAT gateway per zone for high availability, at a higher cost.
- Database connections time out. Confirm that the security group rule uses the right port and that no network ACL denies the traffic.
Checking your work
After building, review the design against least privilege. Each subnet should have the minimum routes it needs. Each security group should allow only the ports its tier serves. Flow logs should show rejected traffic only where you expect it.
Exercise
A database subnet has no default route. The app tier must still back up data to S3 from private subnets without using the NAT gateway. What should you add, and where?
Show answer
Create an S3 gateway endpoint and add it to the route table used by the subnets that need S3 access. The endpoint adds a route for the S3 prefix list, so S3 traffic stays on the AWS network and the NAT gateway is not used.
Exercise
Why should the database security group allow the app security group as a source instead of the app subnet's CIDR block?
Show answer
A security group reference follows the instances that belong to the group. It stays correct when instances are replaced or the subnet layout changes, and it blocks other hosts in the same subnet that are not in the app group.