Defense in Depth
AWS gives several layers of network control. Use more than one.
Security Groups vs Network ACLs
| Security Group (SG) | Network ACL (NACL) | |
|---|---|---|
| Applies to | An ENI / instance (resource level) | A subnet (all resources in it) |
| State | Stateful: return traffic is allowed automatically | Stateless: you must allow both directions (and ephemeral ports 1024-65535) |
| Rules | Allow only (everything not allowed is denied) | Allow and Deny rules |
| Evaluation | All rules together | In number order, first match wins; the final * rule denies |
| Default | Inbound: deny all. Outbound: allow all | Default NACL allows all. A new custom NACL denies all |
| Source can be | CIDR, another security group, prefix list | CIDR only |
| Typical use | Main, fine-grained firewall | Subnet-level guardrail, block specific bad IP ranges |
Security Group Design: Chains
| Group | Inbound rule | Outbound |
|---|---|---|
alb-sg | 443 from the internet | To app-sg on 8080 |
app-sg | 8080 **from alb-sg** | To db-sg on 5432 and HTTPS to endpoints |
db-sg | 5432 **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/0for 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
- 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 type | How it works | Supports | Cost |
|---|---|---|---|
| Gateway endpoint | A route table target using a prefix list. No ENI | S3 and DynamoDB only | Free |
| Interface endpoint (AWS PrivateLink) | An ENI with a private IP in your subnet and private DNS name | Most AWS services (SQS, SNS, KMS, Secrets Manager, ECR, CloudWatch ...), your own services, partner services | Hourly per AZ + per GB |
| Gateway Load Balancer endpoint | Sends traffic through security appliances | GWLB services | Hourly + per GB |
- An endpoint policy limits what can be done through the endpoint (for example only your buckets)
- Combine with a bucket policy using
aws:SourceVpceso 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.
- 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
| Tool | Use |
|---|---|
| Reachability Analyzer | Tests if path A to B is reachable and points to the blocking component, without sending traffic |
| Network Access Analyzer | Finds unintended network access paths |
| AWS Network Firewall | Managed stateful firewall with IPS and domain filtering, placed in a VPC |
| Traffic Mirroring | Copy packets from ENIs to inspection tools |
| Route 53 Resolver DNS Firewall | Block DNS queries to bad domains |
| Session Manager | No bastion host and no port 22 needed |
Troubleshooting Connectivity
- Route table: does the subnet have a route to the target?
- Security group: does the destination SG allow the source on that port? Does the source SG allow outbound?
- NACL: any deny? Ephemeral ports open?
- OS firewall and application: is the service listening on the right interface?
- DNS: does the name resolve to the right IP?
- Public IP / NAT / IGW for internet access
- Use Reachability Analyzer and Flow Logs for proof
Practice Tasks
- Build the ALB → app → DB security group chain and prove the DB is not reachable from the internet
- Add an S3 gateway endpoint and show that a private instance can list S3 with the NAT route removed
- Write a NACL rule that blocks one IP range and test it
- Enable Flow Logs to S3 and query rejected traffic with Athena
- 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.

