VPC Security and Connectivity: Security Groups, NACLs, NAT Gateway, VPC Endpoints, Peering, VPN and Direct Connect
Two Firewalls: Security Groups and NACLs
| Security group | Network ACL (NACL) |
| Works at | Instance level (network interface) | Subnet level |
| Rules | Allow only | Allow and deny |
| Memory | Stateful: replies are allowed automatically | Stateless: you must allow both request and reply |
| Default | Blocks all inbound, allows all outbound | Default NACL allows all |
| Evaluated | All rules together | Rules in number order, first match wins |
| Use | Main firewall for each server | Extra layer, block a bad IP range |
bash
# Web servers: allow HTTP/HTTPS from anywhere, SSH only from the office
aws ec2 authorize-security-group-ingress --group-id sg-web --protocol tcp --port 80 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-web --protocol tcp --port 443 --cidr 0.0.0.0/0
aws ec2 authorize-security-group-ingress --group-id sg-web --protocol tcp --port 22 --cidr 203.0.113.0/24
# Database: only from the web servers' security group (not from an IP range)
aws ec2 authorize-security-group-ingress --group-id sg-db --protocol tcp --port 3306 --source-group sg-web
- Best practice: chain security groups: internet → load balancer SG → app SG → database SG
- Open SSH to your IP only or avoid it with Session Manager
- Never open
0.0.0.0/0 for databases or admin ports
NAT Gateway: Internet for Private Subnets
Servers in a private subnet sometimes need to download updates. A NAT gateway lets them start connections to the internet, while the internet cannot start connections to them.
- Create it in a public subnet with an Elastic IP
- Add a route in the private subnet's table:
0.0.0.0/0 → NAT gateway
- Use one NAT gateway per AZ for high availability
- It costs per hour plus per GB, so delete it after practice and use VPC endpoints to reduce traffic through it
VPC Endpoints: Private Roads to AWS Services
Without an endpoint, a private server reaches S3 through the internet path. With an endpoint, the traffic stays on AWS's private network.
| Type | Services | Notes |
| Gateway endpoint | Amazon S3 and DynamoDB | Free. Added as a route in the route table |
| Interface endpoint (AWS PrivateLink) | Most other AWS services and your own services | Creates a network interface with a private IP. Hourly and data charges |
bash
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc --service-name com.amazonaws.ap-south-1.s3 --route-table-ids rtb-0abc
Connecting VPCs
| Option | What it does | Limits |
| VPC peering | Private link between two VPCs (same or different account or Region) | Not transitive (A-B and B-C does not mean A-C). No overlapping CIDRs. Add routes on both sides |
| Transit Gateway | A central hub that connects many VPCs and on-premises networks | Simplifies large networks |
| PrivateLink | Expose one service privately to other VPCs | One direction, service-level |
Connecting to Your Office or Data Center
| Option | How | Good for |
| Site-to-Site VPN | Encrypted tunnels over the internet | Quick setup, lower cost, backup link |
| AWS Direct Connect | A dedicated private fiber line from your site to AWS | Steady high bandwidth and consistent latency. Takes weeks to set up |
| Client VPN | VPN for individual users on laptops | Remote workers |
| Direct Connect + VPN | Encrypted traffic over the dedicated line | High security needs |
Bastion Host vs Session Manager
- A bastion host is a hardened public server used to jump into private servers. It needs patching and open ports
- Session Manager (Systems Manager) gives shell access with no inbound ports, IAM control and logs. Prefer it
- EC2 Instance Connect Endpoint connects to private instances without public IPs
Seeing the Traffic: Flow Logs
- VPC Flow Logs record information about accepted and rejected traffic (source, destination, port, bytes, action)
- Send them to CloudWatch Logs or S3 and use Athena to query
- Great to find out why a connection is blocked: look for
REJECT
bash
aws ec2 create-flow-logs --resource-type VPC --resource-ids vpc-0abc --traffic-type ALL --log-destination-type cloud-watch-logs --log-group-name /vpc/flow --deliver-logs-permission-arn arn:aws:iam::111111111111:role/FlowLogsRole
Troubleshooting Checklist
| Problem | Check |
| Cannot reach web server from the internet | Public IP, route to IGW, security group port 80/443, NACL, service running |
| Private server cannot download updates | Route to NAT gateway, NAT in public subnet, security group outbound |
| App cannot reach database | Database SG allows app SG on the port, same VPC routing, database available |
| Cannot reach S3 privately | Gateway endpoint route, endpoint policy, bucket policy |
| Peered VPCs cannot talk | Routes on both sides, SG rules, overlapping CIDR |
Practice
- Create two security groups (web and db) and allow db access only from web
- Add a NAT gateway and prove a private instance can run
curl https://aws.amazon.com
- Create an S3 gateway endpoint and check the route table
- Enable flow logs and find a
REJECT entry
- Draw a diagram of your VPC and mark each firewall
Conclusion
Security groups are the main stateful firewall, NACLs are an extra stateless layer. Use NAT gateways for outbound access from private subnets, endpoints for private access to AWS services, peering or Transit Gateway to connect VPCs, and VPN or Direct Connect to reach on-premises. Use flow logs to troubleshoot.
Quick Summary: SG vs NACL; NAT; endpoints; connecting VPCs; on-premises; bastion; flow logs; troubleshooting.