Design a VPC for a three-tier application. Explain subnets, NAT, security groups and NACLs.
Layout (per region, across at least 2–3 Availability Zones)
- VPC with a CIDR such as
10.0.0.0/16, planned so it does not overlap with other VPCs or on-premises networks you may peer with. - Public subnets (one per AZ): route
0.0.0.0/0to an Internet Gateway. Contain only internet-facing load balancers and NAT Gateways. - Private application subnets: ECS tasks, EKS nodes, EC2 instances. Outbound internet through a NAT Gateway in the same AZ (one per AZ for resilience and to avoid cross-AZ charges).
- Private data subnets: RDS/Aurora, ElastiCache, MSK; no internet route at all.
- VPC endpoints: Gateway endpoints for S3 and DynamoDB (free), Interface endpoints (PrivateLink) for ECR, Secrets Manager, STS, CloudWatch and others, so traffic stays on the AWS network and NAT data-processing costs drop.
Security groups vs NACLs
- Security groups: stateful (return traffic is allowed automatically), attached to network interfaces, allow rules only, can reference other security groups. Primary control:
alb-sgallows 443 from the internet;app-sgallows 8080 only fromalb-sg;db-sgallows 5432 only fromapp-sg. - Network ACLs: stateless (you must allow return traffic on ephemeral ports), subnet-level, ordered allow and deny rules. Coarse guardrails, for example blocking a known bad CIDR.
Connectivity
VPC peering (simple, non-transitive), Transit Gateway (hub for many VPCs and on-premises), PrivateLink (expose one service privately), Site-to-Site VPN or Direct Connect to data centres.
Follow-up questions interviewers ask
Try answering each one before you open it.
How do private instances reach S3 without going through NAT?
Add an S3 Gateway VPC endpoint to the private subnets' route tables; S3 traffic is then routed privately and is free of NAT processing charges. Bucket policies can require aws:SourceVpce so the bucket is reachable only through that endpoint. For other services use Interface endpoints (PrivateLink).
Security group or NACL to block traffic between two application subnets?
Prefer security groups: they express intent per workload ("only the order service may call the payment service on 8080") and follow instances as they scale. NACLs are subnet-wide and stateless, which makes them error-prone; use them for broad deny rules or compliance guardrails.