Design Cloud Network Segmentation
Structure VPCs with isolated public/private/data subnets, replace NAT gateway with VPC endpoints for AWS service access, implement security groups with minimal allow rules, and peer VPCs with explicit route controls — preventing lateral movement between environments and reducing attack surface.
Why This Is Best Practice
Adopted by: OWASP Cloud-Native Application Security Top 10 C7 (Insecure Network Controls). CIS AWS Foundations Benchmark v3.0 mandates VPC flow logs, restricted security groups, and no default VPC in production. AWS Well-Architected Security Pillar defines the three-tier subnet model (public/private/data) as the standard. Netflix, Stripe, and Capital One all use multi-VPC architectures with Transit Gateway for environment isolation — documented in their engineering blogs.
Impact: The 2021 Colonial Pipeline ransomware spread from an IT network to OT systems because there was no network segmentation between them — the cloud equivalent is flat VPC design where a compromised web server can directly reach the database. CIS Benchmark analysis (2022) found 35% of AWS accounts have security groups with 0.0.0.0/0 ingress on ports beyond 443/80. AWS's own "Customer Security Incidents" post-mortems cite flat VPC architecture as a contributing factor in 40% of analyzed breach scenarios.
Why best: A flat network (all instances in one subnet, permissive security groups) means a single compromised instance can reach all other instances and services. Tiered subnets + tight security groups implement the principle of least network access — a compromised web server in the public subnet cannot reach the database because the database security group only accepts traffic from the application security group, and the application subnet has no route to the internet.
Sources: OWASP Cloud-Native Top 10 C7; CIS AWS Foundations Benchmark v3.0; AWS VPC security best practices; Netflix multi-account architecture (2019)
Steps
Three-tier VPC structure: public, private, data subnets:
# Terraform: three-tier VPC
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
version = "5.1.2"
name = "production"
cidr = "10.0.0.0/16"
azs = ["us-east-1a", "us-east-1b", "us-east-1c"]
# Public: load balancers only
public_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]
# Private: application servers (no direct internet access)
private_subnets = ["10.0.11.0/24", "10.0.12.0/24", "10.0.13.0/24"]
# Data: databases (no internet route at all)
database_subnets = ["10.0.21.0/24", "10.0.22.0/24", "10.0.23.0/24"]
enable_nat_gateway = true
single_nat_gateway = false # one per AZ for HA
enable_dns_hostnames = true
enable_flow_log = true
flow_log_destination_type = "cloud-watch-logs"
}
Replace NAT gateway with VPC endpoints for AWS services:
# VPC endpoints: private subnets access S3, DynamoDB, Secrets Manager
# without traversing the public internet or NAT gateway
# Gateway endpoint (S3, DynamoDB — free)
resource "aws_vpc_endpoint" "s3" {
vpc_id = module.vpc.vpc_id
service_name = "com.amazonaws.us-east-1.s3"
vpc_endpoint_type = "Gateway"
route_table_ids = module.vpc.private_route_table_ids
}
# Interface endpoint (Secrets Manager — has per-hour cost but eliminates internet path)
resource "aws_vpc_endpoint" "secrets_manager" {
vpc_id = module.vpc.vpc_id
service_name = "com.amazonaws.us-east-1.secretsmanager"
vpc_endpoint_type = "Interface"
subnet_ids = module.vpc.private_subnets
security_group_ids = [aws_security_group.vpc_endpoints.id]
private_dns_enabled = true
}
Security groups: source = security group, not CIDR range:
# Application layer: only accepts traffic from ALB security group
resource "aws_security_group" "app" {
name = "app-layer"
vpc_id = module.vpc.vpc_id
ingress {
from_port = 8080
to_port = 8080
protocol = "tcp"
security_groups = [aws_security_group.alb.id] # NOT: cidr_blocks = ["10.0.0.0/8"]
}
egress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = [aws_security_group.db.id] # only to DB
}
egress {
from_port = 443
to_port = 443
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"] # HTTPS out for API calls
}
}
# Database layer: only accepts from app security group
resource "aws_security_group" "db" {
name = "db-layer"
vpc_id = module.vpc.vpc_id
ingress {
from_port = 5432
to_port = 5432
protocol = "tcp"
security_groups = [aws_security_group.app.id] # only from app
}
# NO egress: databases don't initiate outbound connections
}
Separate VPCs per environment with Transit Gateway:
# Each environment in its own VPC — no route between prod and dev
# Transit Gateway for controlled cross-VPC routing
resource "aws_ec2_transit_gateway" "main" {
description = "Production network hub"
default_route_table_association = "disable" # explicit route tables only
default_route_table_propagation = "disable"
}
# Attach production VPC — only routes to shared-services VPC
resource "aws_ec2_transit_gateway_vpc_attachment" "production" {
transit_gateway_id = aws_ec2_transit_gateway.main.id
vpc_id = module.production_vpc.vpc_id
subnet_ids = module.production_vpc.private_subnets
}
# NO direct route from production to development
# Development VPC can only route to shared-services, not production
Enable VPC Flow Logs for network forensics:
resource "aws_flow_log" "production" {
vpc_id = module.vpc.vpc_id
traffic_type = "ALL" # capture both ACCEPT and REJECT
iam_role_arn = aws_iam_role.flow_log.arn
log_destination = aws_cloudwatch_log_group.flow_logs.arn
# Enable enhanced metadata fields
log_format = "$${version} $${account-id} $${vpc-id} $${subnet-id} $${instance-id} $${srcaddr} $${dstaddr} $${srcport} $${dstport} $${protocol} $${packets} $${bytes} $${start} $${end} $${action} $${log-status}"
}
# CloudWatch Insights query for rejected connections (security investigation)
# fields srcAddr, dstAddr, dstPort, action
# | filter action = "REJECT"
# | stats count(*) by srcAddr, dstAddr, dstPort
# | sort count desc
# | limit 20
Rules
- Delete the default VPC in all AWS regions — it has permissive default security groups and no flow logs; workloads should never use it.
- Security group egress rules must be explicit —
allow all egress (0.0.0.0/0) on application servers allows exfiltration of any data to any destination.
- VPC peering with "allow all traffic" route effectively merges the two VPCs — peer with specific route table entries to specific CIDRs only.
- Flow logs must capture REJECT records — accepted traffic shows normal operation; rejected traffic shows attack attempts and misconfiguration.
Common Mistakes
- Single VPC for all environments — dev, staging, and production in the same VPC means a developer with staging access can reach production databases via internal IPs.
- Security groups with
0.0.0.0/0 ingress on port 22/3389 — SSH/RDP open to the internet is the #1 CIS benchmark failure; use Systems Manager Session Manager instead.
- Using NACLs as the primary security layer — NACLs are stateless and hard to maintain; use security groups (stateful) as the primary control, NACLs only for coarse subnet-level deny.
- Not monitoring VPC flow logs — enabling flow logs without shipping them to SIEM or running queries provides no security value; automate anomaly detection on reject rates.
1---2name: design-cloud-network-segmentation3description: Use when designing cloud network architecture — structuring VPCs with public/private subnets, using VPC endpoints for AWS service access, and implementing security groups as stateful firewalls to contain blast radius.4source: 'OWASP Cloud-Native Application Security Top 10 C7 (owasp.org/www-project-cloud-native-application-security-top-10/); AWS VPC security best practices; CIS AWS Foundations Benchmark v3.0; NIST SP 800-125B'5tags: [security, owasp, cloud, vpc, network-segmentation, security-groups, aws, developer]6metadata:7 source: https://skillmd.com/skills/jeffreytse/design-cloud-network-segmentation8---910# Design Cloud Network Segmentation1112Structure VPCs with isolated public/private/data subnets, replace NAT gateway with VPC endpoints for AWS service access, implement security groups with minimal allow rules, and peer VPCs with explicit route controls — preventing lateral movement between environments and reducing attack surface.1314## Why This Is Best Practice1516**Adopted by:** OWASP Cloud-Native Application Security Top 10 C7 (Insecure Network Controls). CIS AWS Foundations Benchmark v3.0 mandates VPC flow logs, restricted security groups, and no default VPC in production. AWS Well-Architected Security Pillar defines the three-tier subnet model (public/private/data) as the standard. Netflix, Stripe, and Capital One all use multi-VPC architectures with Transit Gateway for environment isolation — documented in their engineering blogs.17**Impact:** The 2021 Colonial Pipeline ransomware spread from an IT network to OT systems because there was no network segmentation between them — the cloud equivalent is flat VPC design where a compromised web server can directly reach the database. CIS Benchmark analysis (2022) found 35% of AWS accounts have security groups with `0.0.0.0/0` ingress on ports beyond 443/80. AWS's own "Customer Security Incidents" post-mortems cite flat VPC architecture as a contributing factor in 40% of analyzed breach scenarios.18**Why best:** A flat network (all instances in one subnet, permissive security groups) means a single compromised instance can reach all other instances and services. Tiered subnets + tight security groups implement the principle of least network access — a compromised web server in the public subnet cannot reach the database because the database security group only accepts traffic from the application security group, and the application subnet has no route to the internet.1920Sources: OWASP Cloud-Native Top 10 C7; CIS AWS Foundations Benchmark v3.0; AWS VPC security best practices; Netflix multi-account architecture (2019)2122## Steps23241. **Three-tier VPC structure: public, private, data subnets**:2526 ```hcl27 # Terraform: three-tier VPC28 module "vpc" {29 source = "terraform-aws-modules/vpc/aws"30 version = "5.1.2"3132 name = "production"33 cidr = "10.0.0.0/16"3435 azs = ["us-east-1a", "us-east-1b", "us-east-1c"]3637 # Public: load balancers only38 public_subnets = ["10.0.1.0/24", "10.0.2.0/24", "10.0.3.0/24"]39 # Private: application servers (no direct internet access)40 private_subnets = ["10.0.11.0/24", "10.0.12.0/24", "10.0.13.0/24"]41 # Data: databases (no internet route at all)42 database_subnets = ["10.0.21.0/24", "10.0.22.0/24", "10.0.23.0/24"]4344 enable_nat_gateway = true45 single_nat_gateway = false # one per AZ for HA46 enable_dns_hostnames = true47 enable_flow_log = true48 flow_log_destination_type = "cloud-watch-logs"49 }50 ```51522. **Replace NAT gateway with VPC endpoints for AWS services**:5354 ```hcl55 # VPC endpoints: private subnets access S3, DynamoDB, Secrets Manager56 # without traversing the public internet or NAT gateway5758 # Gateway endpoint (S3, DynamoDB — free)59 resource "aws_vpc_endpoint" "s3" {60 vpc_id = module.vpc.vpc_id61 service_name = "com.amazonaws.us-east-1.s3"62 vpc_endpoint_type = "Gateway"63 route_table_ids = module.vpc.private_route_table_ids64 }6566 # Interface endpoint (Secrets Manager — has per-hour cost but eliminates internet path)67 resource "aws_vpc_endpoint" "secrets_manager" {68 vpc_id = module.vpc.vpc_id69 service_name = "com.amazonaws.us-east-1.secretsmanager"70 vpc_endpoint_type = "Interface"71 subnet_ids = module.vpc.private_subnets72 security_group_ids = [aws_security_group.vpc_endpoints.id]73 private_dns_enabled = true74 }75 ```76773. **Security groups: source = security group, not CIDR range**:7879 ```hcl80 # Application layer: only accepts traffic from ALB security group81 resource "aws_security_group" "app" {82 name = "app-layer"83 vpc_id = module.vpc.vpc_id8485 ingress {86 from_port = 808087 to_port = 808088 protocol = "tcp"89 security_groups = [aws_security_group.alb.id] # NOT: cidr_blocks = ["10.0.0.0/8"]90 }9192 egress {93 from_port = 543294 to_port = 543295 protocol = "tcp"96 security_groups = [aws_security_group.db.id] # only to DB97 }9899 egress {100 from_port = 443101 to_port = 443102 protocol = "tcp"103 cidr_blocks = ["0.0.0.0/0"] # HTTPS out for API calls104 }105 }106107 # Database layer: only accepts from app security group108 resource "aws_security_group" "db" {109 name = "db-layer"110 vpc_id = module.vpc.vpc_id111112 ingress {113 from_port = 5432114 to_port = 5432115 protocol = "tcp"116 security_groups = [aws_security_group.app.id] # only from app117 }118 # NO egress: databases don't initiate outbound connections119 }120 ```1211224. **Separate VPCs per environment with Transit Gateway**:123124 ```hcl125 # Each environment in its own VPC — no route between prod and dev126 # Transit Gateway for controlled cross-VPC routing127128 resource "aws_ec2_transit_gateway" "main" {129 description = "Production network hub"130 default_route_table_association = "disable" # explicit route tables only131 default_route_table_propagation = "disable"132 }133134 # Attach production VPC — only routes to shared-services VPC135 resource "aws_ec2_transit_gateway_vpc_attachment" "production" {136 transit_gateway_id = aws_ec2_transit_gateway.main.id137 vpc_id = module.production_vpc.vpc_id138 subnet_ids = module.production_vpc.private_subnets139 }140141 # NO direct route from production to development142 # Development VPC can only route to shared-services, not production143 ```1441455. **Enable VPC Flow Logs for network forensics**:146147 ```hcl148 resource "aws_flow_log" "production" {149 vpc_id = module.vpc.vpc_id150 traffic_type = "ALL" # capture both ACCEPT and REJECT151 iam_role_arn = aws_iam_role.flow_log.arn152 log_destination = aws_cloudwatch_log_group.flow_logs.arn153154 # Enable enhanced metadata fields155 log_format = "$${version} $${account-id} $${vpc-id} $${subnet-id} $${instance-id} $${srcaddr} $${dstaddr} $${srcport} $${dstport} $${protocol} $${packets} $${bytes} $${start} $${end} $${action} $${log-status}"156 }157158 # CloudWatch Insights query for rejected connections (security investigation)159 # fields srcAddr, dstAddr, dstPort, action160 # | filter action = "REJECT"161 # | stats count(*) by srcAddr, dstAddr, dstPort162 # | sort count desc163 # | limit 20164 ```165166## Rules167168- Delete the default VPC in all AWS regions — it has permissive default security groups and no flow logs; workloads should never use it.169- Security group egress rules must be explicit — `allow all egress` (`0.0.0.0/0`) on application servers allows exfiltration of any data to any destination.170- VPC peering with "allow all traffic" route effectively merges the two VPCs — peer with specific route table entries to specific CIDRs only.171- Flow logs must capture REJECT records — accepted traffic shows normal operation; rejected traffic shows attack attempts and misconfiguration.172173## Common Mistakes174175- **Single VPC for all environments** — dev, staging, and production in the same VPC means a developer with staging access can reach production databases via internal IPs.176- **Security groups with `0.0.0.0/0` ingress on port 22/3389** — SSH/RDP open to the internet is the #1 CIS benchmark failure; use Systems Manager Session Manager instead.177- **Using NACLs as the primary security layer** — NACLs are stateless and hard to maintain; use security groups (stateful) as the primary control, NACLs only for coarse subnet-level deny.178- **Not monitoring VPC flow logs** — enabling flow logs without shipping them to SIEM or running queries provides no security value; automate anomaly detection on reject rates.