Amazon VPC
An Amazon Virtual Private Cloud (VPC) is your own isolated network inside AWS. You choose its IP address range, split it into subnets, and decide exactly which resources can reach the internet and which stay hidden. Almost everything you run on EC2, RDS, ECS or Lambda-in-a-VPC lives inside one.
Building blocks
- VPC: belongs to one Region and has an IPv4 address range written in CIDR notation, for example
10.0.0.0/16(65,536 addresses). It can also have IPv6 ranges. - Subnet: a slice of the VPC's range in one Availability Zone, such as
10.0.1.0/24(256 addresses, of which AWS reserves 5, leaving 251 usable). - Route table: rules that say where traffic from a subnet goes, based on the destination address.
- Internet gateway (IGW): connects the VPC to the internet in both directions.
- NAT gateway: lets resources in private subnets make outgoing connections (for example to download updates) while blocking incoming ones.
Public and private subnets
There is no "public" checkbox on a subnet. A subnet is public if its route table sends internet traffic (0.0.0.0/0) to an internet gateway, and private if it does not. The standard pattern puts only load balancers and NAT gateways in public subnets, and app servers and databases in private subnets, in at least two AZs.
The route tables behind Figure 1 look like this:
| Route table | Destination | Target |
|---|---|---|
| Public (both public subnets) | 10.0.0.0/16 | local (traffic inside the VPC) |
| Public (both public subnets) | 0.0.0.0/0 | Internet gateway igw-0abc... |
| Private, AZ a | 10.0.0.0/16 | local |
| Private, AZ a | 0.0.0.0/0 | NAT gateway in AZ a nat-0123... |
| Private, AZ b | 0.0.0.0/0 | NAT gateway in AZ b (plus the same local route) |
The local route is created automatically and lets every subnet in the VPC talk to every other one (subject to security groups).
Security groups and network ACLs
| Security group | Network ACL | |
|---|---|---|
| Applies to | An instance or network interface | A whole subnet |
| Rules | Allow rules only | Allow and deny rules, evaluated in number order |
| State | Stateful: replies to allowed traffic are allowed automatically | Stateless: return traffic needs its own rule |
| Typical use | Your main, everyday firewall | An extra coarse layer, such as blocking a known bad IP range |
A powerful feature of security groups is referring to another security group instead of an IP range. A classic three-tier setup:
- alb-sg: allow TCP 443 from
0.0.0.0/0(the public website). - app-sg: allow TCP 8000 only from alb-sg.
- db-sg: allow TCP 5432 only from app-sg.
Even if someone learns the database's private IP, nothing except the app servers can connect to it.
Creating a VPC
The console's Create VPC → VPC and more option builds the whole Figure 1 layout (subnets, route tables, gateways) in one go and is the best way to start. To see the pieces individually, here is the start of the same work with the CLI (IDs are examples):
aws ec2 create-vpc --cidr-block 10.0.0.0/16 \
--tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=school-vpc}]" \
--query "Vpc.VpcId" --output text
aws ec2 create-subnet --vpc-id vpc-0a1b2c3d4e5f67890 \
--cidr-block 10.0.1.0/24 --availability-zone eu-west-1a
aws ec2 create-internet-gateway --query "InternetGateway.InternetGatewayId" --output text
aws ec2 attach-internet-gateway --vpc-id vpc-0a1b2c3d4e5f67890 \
--internet-gateway-id igw-0abc1234def567890
aws ec2 create-route-table --vpc-id vpc-0a1b2c3d4e5f67890
aws ec2 create-route --route-table-id rtb-0123456789abcdef0 \
--destination-cidr-block 0.0.0.0/0 --gateway-id igw-0abc1234def567890
aws ec2 associate-route-table --route-table-id rtb-0123456789abcdef0 \
--subnet-id subnet-0aaa1111bbbb2222c
Every Region also has a default VPC with public subnets in each AZ, so beginners can launch instances immediately. It is fine for practice, but design your own VPC for real projects.
Connecting beyond the VPC
- VPC endpoints: reach AWS services privately without going through the internet or a NAT gateway. Gateway endpoints for S3 and DynamoDB have no extra charge; interface endpoints for other services are billed.
- VPC peering connects two VPCs; Transit Gateway connects many VPCs and on-premises networks through a hub.
- Site-to-Site VPN and Direct Connect link your office or data centre to AWS.
- VPC Flow Logs record accepted and rejected traffic for troubleshooting and security.
NAT gateways are a common surprise cost. Each one is billed for every hour it exists plus every GB it processes, even if you only created it for a tutorial. The "VPC and more" wizard can create one per AZ. Delete NAT gateways (and release their Elastic IPs) when you finish practising, and use gateway endpoints so S3 and DynamoDB traffic skips the NAT.
Key points
- A VPC is your private network in one Region, split into subnets that each live in one AZ.
- A subnet is public if its route table sends 0.0.0.0/0 to an internet gateway; private subnets use a NAT gateway for outbound access.
- Security groups are stateful instance firewalls; network ACLs are stateless subnet filters.
- Chain security groups (ALB → app → database) so only the right tier can connect.
- Delete NAT gateways after practice; use gateway endpoints for S3 and DynamoDB.
Tip: use the ← and → keys to move between pages.
