VPC Security: Security Groups, Network ACLs, VPC Endpoints, PrivateLink and Flow Logs

AWS Solutions Architect 45 min min read Updated: Nov 17, 2026 Intermediate
VPC Security: Security Groups, Network ACLs, VPC Endpoints, PrivateLink and Flow Logs
Intermediate Topic 2 of 4

Defense in Depth

AWS gives several layers of network control. Use more than one.

text Internet ─► CloudFront / WAF / Shield ─► ALB (SG) ─► NACL on subnet ─► instance Security Group ─► app private endpoints keep AWS API traffic off the internet

Security Groups vs Network ACLs

Security Group (SG)Network ACL (NACL)
Applies toAn ENI / instance (resource level)A subnet (all resources in it)
StateStateful: return traffic is allowed automaticallyStateless: you must allow both directions (and ephemeral ports 1024-65535)
RulesAllow only (everything not allowed is denied)Allow and Deny rules
EvaluationAll rules togetherIn number order, first match wins; the final * rule denies
DefaultInbound: deny all. Outbound: allow allDefault NACL allows all. A new custom NACL denies all
Source can beCIDR, another security group, prefix listCIDR only
Typical useMain, fine-grained firewallSubnet-level guardrail, block specific bad IP ranges
Remember: To block one IP address, use a NACL deny (security groups cannot deny). To allow web servers to talk to databases, use security group references.

Security Group Design: Chains

bash ALB_SG=$(aws ec2 create-security-group --group-name alb-sg --description "ALB" --vpc-id $VPC --query GroupId --output text) APP_SG=$(aws ec2 create-security-group --group-name app-sg --description "App" --vpc-id $VPC --query GroupId --output text) DB_SG=$(aws ec2 create-security-group --group-name db-sg --description "DB" --vpc-id $VPC --query GroupId --output text) aws ec2 authorize-security-group-ingress --group-id $ALB_SG --protocol tcp --port 443 --cidr 0.0.0.0/0 aws ec2 authorize-security-group-ingress --group-id $APP_SG --protocol tcp --port 8080 --source-group $ALB_SG aws ec2 authorize-security-group-ingress --group-id $DB_SG --protocol tcp --port 5432 --source-group $APP_SG
GroupInbound ruleOutbound
alb-sg443 from the internetTo app-sg on 8080
app-sg8080 **from alb-sg**To db-sg on 5432 and HTTPS to endpoints
db-sg5432 **from app-sg**None needed
  • Reference security groups, not IP ranges, so autoscaled instances work automatically
  • Keep rules minimal. Do not open 0.0.0.0/0 for SSH or databases. Use Session Manager
  • Security groups are per VPC and cannot reference groups in another Region
  • Changes apply immediately to running instances
  • Limits: 60 inbound + 60 outbound rules per group by default, 5 groups per interface (adjustable)

Network ACL Example

bash # Block a bad IP range for the whole subnet, then allow everything else aws ec2 create-network-acl-entry --network-acl-id acl-0abc --ingress --rule-number 50 --protocol -1 --cidr-block 198.51.100.0/24 --rule-action deny aws ec2 create-network-acl-entry --network-acl-id acl-0abc --ingress --rule-number 100 --protocol -1 --cidr-block 0.0.0.0/0 --rule-action allow aws ec2 create-network-acl-entry --network-acl-id acl-0abc --egress --rule-number 100 --protocol -1 --cidr-block 0.0.0.0/0 --rule-action allow
  • Use rule numbers with gaps (100, 200, 300) so you can insert later
  • If you restrict outbound traffic, allow ephemeral ports 1024-65535 for return traffic

VPC Endpoints: Private Access to AWS Services

Without endpoints, a private instance reaching S3 must go through a NAT Gateway to the internet. Endpoints keep this traffic inside the AWS network, improve security, and can cut NAT costs.

Endpoint typeHow it worksSupportsCost
Gateway endpointA route table target using a prefix list. No ENIS3 and DynamoDB onlyFree
Interface endpoint (AWS PrivateLink)An ENI with a private IP in your subnet and private DNS nameMost AWS services (SQS, SNS, KMS, Secrets Manager, ECR, CloudWatch ...), your own services, partner servicesHourly per AZ + per GB
Gateway Load Balancer endpointSends traffic through security appliancesGWLB servicesHourly + per GB
bash # Gateway endpoint for S3 attached to the private route table aws ec2 create-vpc-endpoint --vpc-id $VPC --service-name com.amazonaws.ap-south-1.s3 --vpc-endpoint-type Gateway --route-table-ids $PRIV_RT # Interface endpoint for Secrets Manager with private DNS aws ec2 create-vpc-endpoint --vpc-id $VPC --service-name com.amazonaws.ap-south-1.secretsmanager --vpc-endpoint-type Interface \ --subnet-ids $APP_A $APP_B --security-group-ids $EP_SG --private-dns-enabled
json { "Statement": [{ "Effect": "Allow", "Principal": "*", "Action": ["s3:GetObject", "s3:PutObject"], "Resource": "arn:aws:s3:::acme-data-prod-ap-south-1/*" }] }
  • An endpoint policy limits what can be done through the endpoint (for example only your buckets)
  • Combine with a bucket policy using aws:SourceVpce so the bucket is reachable only from your endpoint
  • AWS PrivateLink also lets you expose your own service (behind an NLB) to other VPCs and accounts without peering or overlapping CIDR problems

VPC Flow Logs

Flow Logs record metadata of IP traffic (source, destination, ports, protocol, bytes, ACCEPT or REJECT) for a VPC, subnet or network interface. They do not capture packet contents.

bash aws ec2 create-flow-logs --resource-type VPC --resource-ids $VPC --traffic-type ALL \ --log-destination-type s3 --log-destination arn:aws:s3:::acme-flowlogs/vpc/ \ --max-aggregation-interval 60
text 2 123456789012 eni-0abc 10.0.10.15 10.0.20.7 51234 5432 6 10 840 1700000000 1700000060 ACCEPT OK 2 123456789012 eni-0abc 203.0.113.9 10.0.10.15 40000 22 6 1 60 1700000000 1700000060 REJECT OK
  • Send to S3 (cheap, query with Athena), CloudWatch Logs (alarms and Insights) or Kinesis Data Firehose
  • Use to find blocked traffic, unexpected connections, top talkers
  • A REJECT from only one direction often means a NACL problem (stateless). SG problems show REJECT on inbound only

Other Network Security Tools

ToolUse
Reachability AnalyzerTests if path A to B is reachable and points to the blocking component, without sending traffic
Network Access AnalyzerFinds unintended network access paths
AWS Network FirewallManaged stateful firewall with IPS and domain filtering, placed in a VPC
Traffic MirroringCopy packets from ENIs to inspection tools
Route 53 Resolver DNS FirewallBlock DNS queries to bad domains
Session ManagerNo bastion host and no port 22 needed

Troubleshooting Connectivity

  1. Route table: does the subnet have a route to the target?
  2. Security group: does the destination SG allow the source on that port? Does the source SG allow outbound?
  3. NACL: any deny? Ephemeral ports open?
  4. OS firewall and application: is the service listening on the right interface?
  5. DNS: does the name resolve to the right IP?
  6. Public IP / NAT / IGW for internet access
  7. Use Reachability Analyzer and Flow Logs for proof

Practice Tasks

  1. Build the ALB → app → DB security group chain and prove the DB is not reachable from the internet
  2. Add an S3 gateway endpoint and show that a private instance can list S3 with the NAT route removed
  3. Write a NACL rule that blocks one IP range and test it
  4. Enable Flow Logs to S3 and query rejected traffic with Athena
  5. Use Reachability Analyzer between two instances and fix a missing rule

Conclusion

Security groups are stateful allow-only firewalls on instances, best used with group references; NACLs are stateless subnet rules with allow and deny. Use gateway endpoints for S3 and DynamoDB, interface endpoints and PrivateLink for other services, and Flow Logs plus Reachability Analyzer to troubleshoot.

Quick Summary: SG vs NACL table; SG chain CLI; NACL ephemeral ports; gateway/interface/GWLB endpoints + policies; PrivateLink; Flow Logs; Reachability Analyzer; troubleshooting order.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators