Lesson 257 of 296
VPC and security groups
What you will learn
- What a VPC is and how IP ranges are written
- Public and private subnets
- Internet gateways, NAT gateways and route tables
- Security groups vs network ACLs
- VPC endpoints
- A standard three-tier network design
On this page
VPC
A VPC (Virtual Private Cloud) is your own private, isolated network inside AWS. You choose its IP address range using CIDR notation:
| CIDR | Meaning | Addresses |
|---|---|---|
10.0.0.0/16 | The first 16 bits are fixed (10.0); the rest vary | 65,536 |
10.0.1.0/24 | The first 24 bits are fixed (10.0.1) | 256 (AWS reserves 5 in each subnet) |
0.0.0.0/0 | Any IPv4 address (the whole internet) | All |
The smaller the number after the slash, the bigger the range.
Subnets
A subnet is a slice of the VPC's range in one availability zone.
- Public subnet: its route table sends internet traffic (
0.0.0.0/0) to an internet gateway. Resources there can have public IPs. Put only load balancers, NAT gateways and bastions here. - Private subnet: no direct route from the internet. Put application servers, containers, databases here.
Routing to and from the internet
| Component | Purpose |
|---|---|
| Internet gateway (IGW) | Connects the VPC to the internet, both directions, for resources with public IPs in public subnets |
| NAT gateway | Lets resources in private subnets make outbound connections (download packages, call Stripe) without being reachable from the internet. Lives in a public subnet. Charged per hour and per GB, so it can become a significant cost |
| Route table | Rules for where traffic goes, attached to subnets |
Security groups
A security group is a virtual firewall around a resource (an EC2 instance, ECS task, RDS database, load balancer):
- Allow rules only (no deny rules); everything not allowed is blocked.
- Stateful: if an inbound request is allowed, the response is automatically allowed out (and vice versa).
- Rules can reference another security group as the source: "allow port 5432 from the app's security group". This is the key pattern: no IP addresses to maintain, and only the app can reach the database.
A three-tier example:
| Security group | Inbound rules |
|---|---|
alb-sg (load balancer) | 443 from 0.0.0.0/0 (and 80, to redirect to HTTPS) |
app-sg (ECS tasks) | 3000 from alb-sg only |
db-sg (RDS) | 5432 from app-sg only |
Network ACLs
Network ACLs are a second, optional layer at the subnet level:
| Security group | Network ACL | |
|---|---|---|
| Applies to | Individual resources | Whole subnets |
| Rules | Allow only | Allow and deny, evaluated in number order |
| State | Stateful | Stateless: return traffic must be allowed explicitly |
| Typical use | Main control | Coarse extra protection, blocking specific IP ranges |
A common interview question: "security groups are stateful and allow-only; NACLs are stateless and can deny."
VPC endpoints
Private subnets calling AWS services (S3, DynamoDB, SQS, Secrets Manager, ECR) would normally go through the NAT gateway and over the public internet endpoints. VPC endpoints keep this traffic inside AWS's network:
- Gateway endpoints for S3 and DynamoDB: free, added to route tables.
- Interface endpoints (PrivateLink) for most other services: a private network interface in your subnet, charged per hour and GB.
Benefits: security (no internet path, endpoint policies), and often lower NAT costs.
The standard design
VPC 10.0.0.0/16 (eu-west-2)
AZ a: public 10.0.0.0/24 [ALB, NAT] private-app 10.0.10.0/24 [ECS tasks] private-data 10.0.20.0/24 [RDS primary]
AZ b: public 10.0.1.0/24 [ALB, NAT] private-app 10.0.11.0/24 [ECS tasks] private-data 10.0.21.0/24 [RDS standby]
Internet gateway; NAT per AZ (or one to save cost); gateway endpoints for S3 and DynamoDBOnly the load balancer faces the internet. Administration goes through Systems Manager Session Manager instead of open SSH ports. VPC Flow Logs record network traffic for troubleshooting and security analysis.
How to say it in an interview
"A VPC is an isolated private network with a CIDR range, split into subnets per availability zone. Public subnets route to an internet gateway and hold only load balancers and NAT gateways; applications and databases live in private subnets and reach out through NAT or, better, VPC endpoints, gateway endpoints being free for S3 and DynamoDB. Security groups are stateful, allow-only firewalls on resources, and I chain them by reference: the load balancer accepts 443 from anywhere, the app only from the load balancer's group, the database only from the app's group. Network ACLs are stateless subnet-level rules that can also deny. I spread across two or three AZs and avoid open SSH by using Session Manager."
Key words
| Word | Meaning |
|---|---|
| VPC | Your isolated network in AWS |
| CIDR | IP range notation like 10.0.0.0/16 |
| Subnet | A part of the VPC in one AZ |
| Public / private subnet | With / without a route to the internet gateway |
| Internet gateway / NAT gateway | Two-way internet access / outbound-only for private subnets |
| Security group | Stateful allow-only firewall on a resource |
| Network ACL | Stateless subnet firewall with allow and deny |
| VPC endpoint | Private access to AWS services without the internet |
Now prove it
This lesson is complete when you pass the quiz and all three practice levels.