Subnet
Subnet
A technique that logically divides an IP network to optimize security, management, and performance. Network ranges are defined with CIDR notation; a core concept in cloud VPC design.
Subnet
A technique that logically divides an IP network to optimize security, management, and performance. Network ranges are defined with CIDR notation; a core concept in cloud VPC design.
A subnet (Subnetwork) is a technique that divides a large IP network into smaller logical units. Just as a large building is divided and managed floor by floor, splitting a network into subnets makes traffic management, security policy enforcement, and IP address efficiency possible. It was first defined in RFC 950 in 1985, and it is a core concept of modern cloud infrastructure.
A subnet is expressed in CIDR (Classless Inter-Domain Routing) notation. For example, 10.0.1.0/24 includes 256 IPs in the range 10.0.1.0 ~ 10.0.1.255, and the number of actually usable hosts is 254 (excluding the network address and the broadcast address). In /24, the number indicates the number of bits in the network portion, and the larger the number, the smaller the subnet size.
In cloud environments, a subnet is the basic unit of VPC (Virtual Private Cloud) design. AWS, GCP, and Azure all isolate resources and apply network policies through subnets. For example, if you create a VPC as 10.0.0.0/16 in AWS, you divide it into 10.0.1.0/24 (Public), 10.0.2.0/24 (Private), 10.0.3.0/24 (Database), and so on to implement layered security.
The key to subnet design is balancing scalability and security. A subnet that is too small (/28, 16 IPs) limits resource expansion, and a subnet that is too large (/16, 65,536 IPs) enlarges the broadcast domain and can cause performance degradation. In practice, it is common to use /24 (254 hosts) as the default unit and to configure database or management subnets as /27 (30 hosts) or /28 (14 hosts).
| CIDR | Subnet Mask | Total IPs | Usable Hosts | Use case |
|---|---|---|---|---|
| /16 | 255.255.0.0 | 65,536 | 65,534 | Entire VPC Range |
| /20 | 255.255.240.0 | 4,096 | 4,094 | Large Subnet |
| /24 | 255.255.255.0 | 256 | 254 | Standard Subnet (Most Common) |
| /27 | 255.255.255.224 | 32 | 30 | Small Subnet |
| /28 | 255.255.255.240 | 16 | 14 | Minimum Unit (AWS Minimum) |
In cloud environments, subnets are classified into Public Subnets and Private Subnets according to how they access the internet. This distinction is the foundation of security architecture, and without proper design it can lead to security incidents.
Connected to an Internet Gateway (IGW) and directly accessible from the outside. Web servers, load balancers, and Bastion Hosts (jump servers) are placed here. The routing table has a 0.0.0.0/0 → IGW route configured.
Outbound only through a NAT Gateway. Application servers, databases, and internal services are placed here. Inbound is possible only through resources in the Public Subnet. A 0.0.0.0/0 → NAT Gateway route is configured.
# AWS VPC 및 서브넷 구성 (Terraform)
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16" # 65,536개 IP
enable_dns_hostnames = true
enable_dns_support = true
tags = { Name = "production-vpc" }
}
# Public Subnet (웹 서버, 로드 밸런서용)
resource "aws_subnet" "public_a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24" # 254개 호스트
availability_zone = "ap-northeast-2a"
map_public_ip_on_launch = true # 자동 Public IP 할당
tags = { Name = "public-subnet-a", Tier = "public" }
}
resource "aws_subnet" "public_b" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "ap-northeast-2b"
map_public_ip_on_launch = true
tags = { Name = "public-subnet-b", Tier = "public" }
}
# Private Subnet (애플리케이션 서버용)
resource "aws_subnet" "private_app_a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.10.0/24"
availability_zone = "ap-northeast-2a"
tags = { Name = "private-app-subnet-a", Tier = "private" }
}
# Private Subnet (데이터베이스용 - 더 작은 크기)
resource "aws_subnet" "private_db_a" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.20.0/27" # 30개 호스트 (DB는 적게 필요)
availability_zone = "ap-northeast-2a"
tags = { Name = "private-db-subnet-a", Tier = "database" }
}
# Internet Gateway (Public Subnet용)
resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id
tags = { Name = "main-igw" }
}
# NAT Gateway (Private Subnet의 아웃바운드용)
resource "aws_nat_gateway" "main" {
allocation_id = aws_eip.nat.id
subnet_id = aws_subnet.public_a.id
tags = { Name = "main-nat-gw" }
}
# Public Subnet 라우팅 테이블
resource "aws_route_table" "public" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.main.id
}
tags = { Name = "public-rt" }
}
# Private Subnet 라우팅 테이블
resource "aws_route_table" "private" {
vpc_id = aws_vpc.main.id
route {
cidr_block = "0.0.0.0/0"
nat_gateway_id = aws_nat_gateway.main.id
}
tags = { Name = "private-rt" }
}
import ipaddress
# CIDR 블록 분석
network = ipaddress.ip_network("10.0.0.0/24")
print(f"네트워크 주소: {network.network_address}") # 10.0.0.0
print(f"브로드캐스트: {network.broadcast_address}") # 10.0.0.255
print(f"서브넷 마스크: {network.netmask}") # 255.255.255.0
print(f"총 호스트 수: {network.num_addresses}") # 256
print(f"사용 가능: {network.num_addresses - 2}") # 254
# 사용 가능한 호스트 IP 나열
for ip in list(network.hosts())[:5]:
print(f"호스트 IP: {ip}")
# VPC를 서브넷으로 분할
vpc = ipaddress.ip_network("10.0.0.0/16")
subnets = list(vpc.subnets(new_prefix=24)) # /16을 /24로 분할
print(f"\n/16 VPC를 /24로 분할하면 {len(subnets)}개 서브넷")
print(f"첫 번째 서브넷: {subnets[0]}") # 10.0.0.0/24
print(f"마지막 서브넷: {subnets[-1]}") # 10.0.255.0/24
# IP가 서브넷에 속하는지 확인
def check_ip_in_subnet(ip: str, subnet: str) -> bool:
return ipaddress.ip_address(ip) in ipaddress.ip_network(subnet)
print(f"\n10.0.1.50은 10.0.1.0/24에 속함: {check_ip_in_subnet('10.0.1.50', '10.0.1.0/24')}")
print(f"10.0.2.50은 10.0.1.0/24에 속함: {check_ip_in_subnet('10.0.2.50', '10.0.1.0/24')}")
# AWS VPC 서브넷 설계 예시
def design_vpc_subnets(vpc_cidr: str) -> dict:
"""VPC CIDR를 받아 표준 3계층 서브넷 구조 반환"""
vpc = ipaddress.ip_network(vpc_cidr)
subnets_24 = list(vpc.subnets(new_prefix=24))
return {
"vpc": str(vpc),
"public_a": str(subnets_24[0]), # 10.0.0.0/24
"public_b": str(subnets_24[1]), # 10.0.1.0/24
"private_app_a": str(subnets_24[10]), # 10.0.10.0/24
"private_app_b": str(subnets_24[11]), # 10.0.11.0/24
"private_db_a": str(subnets_24[20]), # 10.0.20.0/24
"private_db_b": str(subnets_24[21]), # 10.0.21.0/24
}
design = design_vpc_subnets("10.0.0.0/16")
for name, cidr in design.items():
print(f"{name}: {cidr}")
# AWS CLI로 서브넷 조회 및 생성
# VPC 내 모든 서브넷 조회
aws ec2 describe-subnets \
--filters "Name=vpc-id,Values=vpc-12345678" \
--query "Subnets[*].{ID:SubnetId,CIDR:CidrBlock,AZ:AvailabilityZone,Public:MapPublicIpOnLaunch}" \
--output table
# Public Subnet 생성
aws ec2 create-subnet \
--vpc-id vpc-12345678 \
--cidr-block 10.0.1.0/24 \
--availability-zone ap-northeast-2a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=public-subnet-a}]'
# Private Subnet 생성
aws ec2 create-subnet \
--vpc-id vpc-12345678 \
--cidr-block 10.0.10.0/24 \
--availability-zone ap-northeast-2a \
--tag-specifications 'ResourceType=subnet,Tags=[{Key=Name,Value=private-subnet-a}]'
# 서브넷에 라우팅 테이블 연결
aws ec2 associate-route-table \
--subnet-id subnet-11111111 \
--route-table-id rtb-22222222
# 서브넷의 사용 가능한 IP 수 확인
aws ec2 describe-subnets \
--subnet-ids subnet-11111111 \
--query "Subnets[0].AvailableIpAddressCount"
# CIDR 계산 (ipcalc 사용)
ipcalc 10.0.0.0/24
# Network: 10.0.0.0/24
# Netmask: 255.255.255.0
# Broadcast: 10.0.0.255
# HostMin: 10.0.0.1
# HostMax: 10.0.0.254
# Hosts/Net: 254
# 서브넷 간 충돌 확인
ipcalc -c 10.0.1.0/24 10.0.2.0/24 # OK (충돌 없음)
ipcalc -c 10.0.1.0/24 10.0.1.128/25 # OVERLAP (충돌)
"Let's set the VPC CIDR to 10.0.0.0/16 and split the subnets by AZ. Public will be 10.0.1.0/24 and 10.0.2.0/24, Private App will be 10.0.10.0/24 and 10.0.11.0/24, and DB will be kept small with /27, like 10.0.20.0/27 and 10.0.20.32/27. Considering the future expansion of the EKS node group, it may also be worth making Private large, like /20."
"Right now all resources are in the Public Subnet, and this is a serious security issue. RDS and internal services must be moved to the Private Subnet, and we need to change things so that they communicate externally only through the NAT Gateway. Security Groups alone provide insufficient layers of defense."
"The subnet IPs are almost used up. It's /24, so there are 254, and 240 are currently in use. We need to create a new subnet 10.0.12.0/24, add it to the auto scaling group, and associate the routing table. From now on, it would be better to design with room to spare from the start, using /20."
If you design it as /28 (14 hosts), you will run out of IPs when the service expands. AWS EKS consumes many IPs per node, so the minimum is /24 and /20 is recommended. A subnet is hard to expand later, so leave room to spare from the start!
If you create both 10.0.0.0/24 and 10.0.0.128/25 in a 10.0.0.0/16 VPC, an overlap error occurs. Subnet CIDRs must not overlap within a VPC. Verify in advance with ipcalc or a spreadsheet before designing.
If you mistakenly attach an Internet Gateway to a Private Subnet's routing table, it becomes a Public Subnet. When changing a routing table, you must always reconfirm the purpose of the subnet.
Use /16 for the VPC, /24 for general subnets, /20 for subnets expected to expand, and /27 for small dedicated ones. Configure at least 2 subnets (Public/Private) per AZ. Choose the CIDR from 10.x.0.0/16, 172.16-31.0.0/16, or 192.168.0.0/16.