Sub topic 16
Putting it together: a multi-tier VPC design
This chapter combines the earlier pieces into one layout: public, application, and data tiers spread over two Availability Zones (AZs). It assumes you can read CIDR notation and know how route tables, internet gateways, NAT gateways, security groups, and network ACLs (NACLs) work. It also assumes you can run AWS CLI commands and have the variables from earlier chapters (VPC_ID, IGW_ID) set in your shell.
What a multi-tier design is for
A multi-tier design splits a workload into layers. Each layer gets its own subnets, route tables, and security groups. A request can only travel from one layer to the next layer, so a compromise of one layer does not expose everything.
A typical three-tier layout has these layers:
| Tier | Typical resources | Internet inbound | Internet outbound |
|---|---|---|---|
| Public | Application Load Balancer (ALB), NAT gateways | Yes, through the internet gateway | Yes |
| Application | EC2 instances, containers | No | Through a NAT gateway only |
| Data | RDS, ElastiCache | No | None |
The difference between the application and data tiers is in their route tables. The application tier has a default route to a NAT gateway. The data tier has no default route, so even a misconfigured security group cannot let a database reach the internet.
The architecture
Step 1: Plan the address space
Give each tier and AZ its own /24 inside a /16, and leave gaps for growth. Grouping by tier makes NACL rules and flow log queries easier to write.
| Subnet | AZ | CIDR |
|---|---|---|
| public-a | us-east-1a | 10.0.0.0/24 |
| public-b | us-east-1b | 10.0.1.0/24 |
| app-a | us-east-1a | 10.0.10.0/24 |
| app-b | us-east-1b | 10.0.11.0/24 |
| data-a | us-east-1a | 10.0.20.0/24 |
| data-b | us-east-1b | 10.0.21.0/24 |
This uses a small part of the /16, so you can add tiers or AZs later. Before you pick the /16, check that it does not overlap any VPC or on-premises network you might peer with or attach to a Transit Gateway. You cannot peer VPCs with overlapping ranges, and fixing an overlap later usually means rebuilding.
The tier-specific numbering (0, 10, 20) lets one NACL or route rule match a whole tier. For example, "10.0.10.0/23" covers both application subnets.
Step 2: Create the subnets
This helper function creates a tagged subnet and prints its ID.
mk_subnet() {
aws ec2 create-subnet --vpc-id "$VPC_ID" --cidr-block "$1" \
--availability-zone "$2" \
--tag-specifications "ResourceType=subnet,Tags=[{Key=Name,Value=$3}]" \
--query Subnet.SubnetId --output text
}
PUBLIC_A=$(mk_subnet 10.0.0.0/24 us-east-1a public-a)
PUBLIC_B=$(mk_subnet 10.0.1.0/24 us-east-1b public-b)
APP_A=$(mk_subnet 10.0.10.0/24 us-east-1a app-a)
APP_B=$(mk_subnet 10.0.11.0/24 us-east-1b app-b)
DATA_A=$(mk_subnet 10.0.20.0/24 us-east-1a data-a)
DATA_B=$(mk_subnet 10.0.21.0/24 us-east-1b data-b)A subnet is public or private only because of its route table. Naming the subnets by tier helps you remember what each one should be.
Step 3: Route tables
You need these route tables:
- One public route table. It has
0.0.0.0/0to the internet gateway and is associated with both public subnets. The earlier internet gateway chapter showed the commands. - One private route table per AZ. Each has
0.0.0.0/0to the NAT gateway in the same AZ and is associated with that AZ's application subnet. - One data route table. It has only the implicit local route and is associated with both data subnets.
Create a NAT gateway in each public subnet, as in the NAT chapter. Using one NAT gateway per AZ keeps outbound traffic working if an AZ fails, and it avoids cross-AZ data charges. A single shared NAT gateway costs less but becomes a single point of failure for the whole application tier. Many teams accept that for development and use one per AZ in production.
The data tier is the simplest to create:
DATA_RT_ID=$(aws ec2 create-route-table --vpc-id "$VPC_ID" \
--query RouteTable.RouteTableId --output text)
aws ec2 associate-route-table --route-table-id "$DATA_RT_ID" --subnet-id "$DATA_A"
aws ec2 associate-route-table --route-table-id "$DATA_RT_ID" --subnet-id "$DATA_B"A new route table contains only the local route, which is what you want here.
Step 4: Chain security groups
Security groups carry most of the least-privilege design. Each tier allows traffic only from the security group of the tier above it, not from an IP range. Instances can then be added or replaced without changing any rule.
ALB_SG=$(aws ec2 create-security-group --group-name alb-sg \
--description "Load balancer" --vpc-id "$VPC_ID" \
--query GroupId --output text)
APP_SG=$(aws ec2 create-security-group --group-name app-sg \
--description "App tier" --vpc-id "$VPC_ID" \
--query GroupId --output text)
DB_SG=$(aws ec2 create-security-group --group-name db-sg \
--description "Data tier" --vpc-id "$VPC_ID" \
--query GroupId --output text)
aws ec2 authorize-security-group-ingress --group-id "$ALB_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 "$ALB_SG"
aws ec2 authorize-security-group-ingress --group-id "$DB_SG" \
--protocol tcp --port 5432 --source-group "$APP_SG"
# Remove the default allow-all outbound rule from the database group.
aws ec2 revoke-security-group-egress --group-id "$DB_SG" \
--ip-permissions 'IpProtocol=-1,IpRanges=[{CidrIp=0.0.0.0/0}]'Security groups are stateful, so return traffic is allowed automatically. Removing the database group's outbound rule is safe for that reason: replies to allowed inbound connections still go out. The database can no longer start connections of its own. This is a second barrier behind the missing default route.
Step 5: Add NACLs as a coarse backstop
NACLs are stateless and apply to a whole subnet, so you must allow return traffic on ephemeral ports (1024-65535) in both directions. Because of that, most designs leave the default NACL, which allows everything, and use security groups for day-to-day control.
Add custom NACLs when you want a rule that no instance-level change can undo. A common example is a data-tier NACL that allows TCP 5432 only from 10.0.10.0/23 and denies everything else. Write the inbound and outbound rules together, and test them with flow logs before relying on them. A missing ephemeral-port rule shows up as connections that hang.
Step 6: Keep AWS service traffic off the NAT gateway
NAT gateways charge per GB processed. Traffic from the application tier to S3 or DynamoDB can use a gateway endpoint instead, which has no hourly or per-GB charge.
aws ec2 create-vpc-endpoint \
--vpc-id "$VPC_ID" \
--vpc-endpoint-type Gateway \
--service-name com.amazonaws.us-east-1.s3 \
--route-table-ids "$PRIVATE_RT_A_ID" "$PRIVATE_RT_B_ID"Here PRIVATE_RT_A_ID and PRIVATE_RT_B_ID are the two private route tables from Step 3. The endpoint adds a prefix-list route to each table. For services that only offer interface endpoints, such as Systems Manager or Secrets Manager, create interface endpoints in the application subnets. Give them a security group that allows TCP 443 from APP_SG. Private DNS is on by default for those endpoints, so the usual service hostnames resolve to private addresses.
Step 7: Turn on observability
Enable flow logs on the VPC before you have an incident, not after.
aws ec2 create-flow-logs \
--resource-type VPC \
--resource-ids "$VPC_ID" \
--traffic-type ALL \
--log-destination-type s3 \
--log-destination arn:aws:s3:::my-flow-log-bucketFlow logs record ACCEPT and REJECT results for each flow. A rejected flow into the data tier from anything but 10.0.10.0/23 shows you a misconfiguration or a probe. Remember that flow logs show which rule layer is involved only indirectly: a REJECT could come from a security group or a NACL, so compare the flow's source and destination against both.
Common mistakes
- Subnet in the wrong tier. Check the route table association of each subnet. The name tag does not control routing.
- Single NAT gateway with per-AZ route tables. If both private tables point to one NAT gateway in AZ a, losing AZ a breaks outbound traffic in AZ b too.
- Hanging connections through NACLs. Stateless rules need ephemeral-port rules in both directions.
- CIDR rules in security groups. Referencing security group IDs follows instances as they scale. IP ranges do not.
- Forgetting the endpoint route table list. A gateway endpoint only works for subnets whose route tables it was added to.
Verifying the design
Test each boundary with a short check:
- From the internet, only the ALB's port 443 answers.
- From an application instance,
curl https://example.comworks through the NAT gateway. - From an application instance, a connection to the database on 5432 works. From a different security group, it times out.
- From a database instance (through Session Manager, which needs interface endpoints for
ssm,ssmmessages, andec2messages), outbound connections fail.
Exercise
A team places an RDS database in a subnet whose route table has no 0.0.0.0/0 route. The database security group allows TCP 5432 from the app security group. Developers say the database is fine, but nightly jobs on the app instances can no longer download updates from the internet. What is the likely cause, and why is the database not affected?
Show answer
The jobs run on application instances whose route table is probably missing the 0.0.0.0/0 route to a NAT gateway, for example because the route points to a NAT gateway in an AZ that failed, or because the subnet was associated with the data route table by mistake. The database is not affected because it never needs the internet. The route to the app tier is the local VPC route, which exists in every route table, and the security group allows 5432 from the app tier.
Exercise
Why does this design reference security group IDs between tiers instead of CIDR ranges, and when would you add a NACL rule anyway?
Show answer
A security group reference matches any instance in the source group, so rules stay correct when instances are added, replaced, or moved to new IPs. A NACL is worth adding when you need a subnet-wide rule that cannot be bypassed by editing an instance's security group, such as allowing only 10.0.10.0/23 to reach the data subnets on TCP 5432, with explicit ephemeral-port rules for return traffic.