VPC Security and Connectivity: Security Groups, NACLs, NAT Gateway, VPC Endpoints, Peering, VPN and Direct Connect

AWS Technical Essentials 55 min min read Updated: Nov 17, 2026 Beginner
VPC Security and Connectivity: Security Groups, NACLs, NAT Gateway, VPC Endpoints, Peering, VPN and Direct Connect
Beginner Topic 2 of 3

Two Firewalls: Security Groups and NACLs

Security groupNetwork ACL (NACL)
Works atInstance level (network interface)Subnet level
RulesAllow onlyAllow and deny
MemoryStateful: replies are allowed automaticallyStateless: you must allow both request and reply
DefaultBlocks all inbound, allows all outboundDefault NACL allows all
EvaluatedAll rules togetherRules in number order, first match wins
UseMain firewall for each serverExtra 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.

TypeServicesNotes
Gateway endpointAmazon S3 and DynamoDBFree. Added as a route in the route table
Interface endpoint (AWS PrivateLink)Most other AWS services and your own servicesCreates 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

OptionWhat it doesLimits
VPC peeringPrivate 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 GatewayA central hub that connects many VPCs and on-premises networksSimplifies large networks
PrivateLinkExpose one service privately to other VPCsOne direction, service-level

Connecting to Your Office or Data Center

OptionHowGood for
Site-to-Site VPNEncrypted tunnels over the internetQuick setup, lower cost, backup link
AWS Direct ConnectA dedicated private fiber line from your site to AWSSteady high bandwidth and consistent latency. Takes weeks to set up
Client VPNVPN for individual users on laptopsRemote workers
Direct Connect + VPNEncrypted traffic over the dedicated lineHigh 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

ProblemCheck
Cannot reach web server from the internetPublic IP, route to IGW, security group port 80/443, NACL, service running
Private server cannot download updatesRoute to NAT gateway, NAT in public subnet, security group outbound
App cannot reach databaseDatabase SG allows app SG on the port, same VPC routing, database available
Cannot reach S3 privatelyGateway endpoint route, endpoint policy, bucket policy
Peered VPCs cannot talkRoutes on both sides, SG rules, overlapping CIDR

Practice

  1. Create two security groups (web and db) and allow db access only from web
  2. Add a NAT gateway and prove a private instance can run curl https://aws.amazon.com
  3. Create an S3 gateway endpoint and check the route table
  4. Enable flow logs and find a REJECT entry
  5. 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.

Get Newsletter

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