Beginner · Networking and databases: VPC and RDS · topic 15 of 50 · 30 min
VPC security and connectivity
Explanation
Two layers of firewall
A VPC gives you two firewalls. Security groups (SGs) guard each network interface, so each instance. Network ACLs (NACLs) guard each subnet boundary. Most teams do almost everything with security groups and keep NACLs simple, using them for broad blocks such as "deny this attacking IP range".
| Security group | Network ACL | |
|---|---|---|
| Applies to | Network interface (instance) | Whole subnet |
| Rules | Allow only | Allow and deny |
| State | Stateful: replies allowed automatically | Stateless: replies need their own rule |
| Order | All rules checked together | Lowest rule number first; first match wins |
| Default | New SG: no inbound, all outbound | Default NACL allows all; a new custom NACL denies all |
Security groups that reference each other
Instead of IP addresses, a security group rule can name another security group as the source. "Allow port 5432 from sg-app" means any instance that has sg-app attached may connect, even as instances are added or replaced by auto scaling. This is the standard way to build tiers: the load balancer SG allows 443 from the internet, the app SG allows 8080 only from the load balancer SG, and the database SG allows 5432 only from the app SG.
aws ec2 authorize-security-group-ingress --group-id sg-0db111 --protocol tcp --port 5432 --source-group sg-0app222Network ACLs and ephemeral ports
Because NACLs are stateless, you must allow the reply traffic too. When a browser connects to your server on port 443, the reply goes back to a random high port on the client, called an ephemeral port. A NACL that allows inbound 443 must also allow outbound traffic to ports 1024 to 65535, or replies are dropped. Rules are checked from the lowest number up, and the first match decides. If nothing matches, the final rule, shown as *, denies the packet.
| Rule # | Direction | Protocol | Port | Source/Destination | Action |
|---|---|---|---|---|---|
| 90 | Inbound | All | All | 198.51.100.0/24 (attacker) | DENY |
| 100 | Inbound | TCP | 443 | 0.0.0.0/0 | ALLOW |
| 100 | Outbound | TCP | 1024-65535 | 0.0.0.0/0 | ALLOW |
| * | Both | All | All | 0.0.0.0/0 | DENY |
VPC endpoints: reach AWS services privately
- Gateway endpoints: for S3 and DynamoDB only. They add a route in your route table, cost nothing, and keep traffic off the NAT gateway.
- Interface endpoints (powered by AWS PrivateLink): a private network interface in your subnet for many other services, such as Secrets Manager, SQS or ECR. They are charged per hour and per GB. PrivateLink can also expose your own service privately to other VPCs.
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc --service-name com.amazonaws.ap-south-1.s3 --route-table-ids rtb-0privConnecting networks
- VPC peering: a private link between two VPCs (even in other accounts or regions). It is not transitive: if A peers with B and B with C, A still cannot reach C. CIDR ranges must not overlap.
- Transit Gateway: a regional hub. Attach many VPCs and VPN connections to it instead of building a mesh of peerings.
- Site-to-Site VPN: encrypted IPsec tunnels over the internet from your office router to AWS. Quick to set up; each connection has two tunnels for redundancy.
- Direct Connect: a dedicated private line from your data centre to AWS through a partner location. More consistent speed and latency, but it takes weeks to set up and is not encrypted by default, so many teams run a VPN or MACsec over it.
VPC Flow Logs
Flow Logs record metadata about IP traffic on a VPC, subnet or network interface: source and destination address and port, protocol, bytes, and whether it was ACCEPTed or REJECTed. They do not capture packet contents. Send them to CloudWatch Logs or S3 and use them to debug "why can my app not reach the database" or to spot scans.
2 111122223333 eni-0a1b2c3d 198.51.100.7 10.0.1.25 49152 22 6 3 180 1760870000 1760870060 REJECT OKNeed help?
Stuck on a word or an idea? Search this course, or every course, and jump straight to the explanation or a worked question.
Try:
Watch the lesson
VPC security and connectivity · 1/8
A VPC has two firewalls. Security groups protect each instance. Network ACLs protect each subnet.
Prefer a video by a teacher? Find free videos on “aws security groups vs network acl vpc endpoint peering transit gateway explained” on YouTube ↗
In real life: A bank branch network meets the cloud
A co-operative bank in Gujarat moves its loan processing app to AWS. Branch offices connect to the head office, and the head office connects to a Transit Gateway in ap-south-1 with a Site-to-Site VPN (two tunnels) while a Direct Connect line is being installed. App servers reach S3 through a gateway endpoint and Secrets Manager through an interface endpoint, so the database subnets have no internet route at all. Security groups chain load balancer, app and database. A NACL rule denies an IP range seen scanning the public subnet, and Flow Logs of REJECTed traffic go to S3 for the security team.
Practice (0/3 done)
You must block one attacking IP range from reaching every instance in a subnet. Where do you add the rule?
Write evaluate_nacl(rules, packet) for inbound NACL rules. Each rule is a dict with "number", "protocol" ("tcp", "udp" or "all"), "port_from", "port_to", "cidr" and "action" ("allow" or "deny"). The packet has "protocol", "port" and "source" (an IP string). Check rules from the LOWEST number up; the first rule whose protocol, port range and CIDR all match decides. If none match, return "deny". Use the ipaddress module for the CIDR check.
Design the security groups for a three-tier app: an internet-facing Application Load Balancer, Node.js app servers on port 3000, and a PostgreSQL database on port 5432. Write each group's inbound rules, using security group references wherever possible, and say how admins get a shell without SSH.
Quick quiz
Read more (free)
Related help
Before this, learn
This topic builds on these. If something here feels new, open them first.
Related topics
- Managed relational databases: RDS and AuroraNetworking and databases: VPC and RDS · same module
- Choosing a database: DynamoDB, caches and warehousesNetworking and databases: VPC and RDS · same module
- Virtualisation, VRFs and cloud networkingComputer Networking (CCNA level) · Automation, SDN and cloud networking
- Azure networking, Functions, databases and monitoringMicrosoft Azure essentials
- FirewallsCyber Security: from zero to SOC analyst · Identity and network defence
- Firewalls, IPS and VPNsComputer Networking (CCNA level) · Network security
Questions people ask about this
Open a question to see the answer and why. The first ones are this topic's own quiz, so try the quiz above before you peek.
Which statement about network ACLs is true?
Answer: They are stateless and evaluated in rule-number order
NACLs are stateless subnet firewalls; the lowest numbered matching rule decides.
Which services can use a gateway VPC endpoint?
Answer: S3 and DynamoDB
Gateway endpoints exist only for S3 and DynamoDB. Other services use interface endpoints.
VPC A is peered with B, and B is peered with C. Can A reach C through B?
Answer: No, peering is not transitive
Peering connects exactly two VPCs. For hub-style routing use Transit Gateway.
What do VPC Flow Logs NOT contain?
Answer: The packet contents
Flow Logs record metadata only, never the data inside packets.
Which option gives a dedicated private line from your data centre to AWS?
Answer: AWS Direct Connect
Direct Connect is a dedicated connection through a partner location. VPN runs over the public internet.
Which VPC CIDR size is NOT allowed in AWS?Related
Answer: /8
AWS allows VPC blocks from /16 to /28. A /8 is too large.
Which statement about security groups and network ACLs is true?Related
Answer: Security groups are stateful; network ACLs are stateless
Security groups remember connections, so replies are allowed automatically. Network ACLs are stateless, have allow and deny rules, and apply to a subnet.
VNet A is peered with B, and B is peered with C. Can A reach C through B by default?Related
Answer: No, peering is not transitive
Peering connects only the two VNets in the pair. Use a hub with a firewall or direct peering for A to C.
What does a stateful firewall add over a stateless packet filter?Related
Answer: It tracks connections, allowing replies to permitted traffic automatically
It remembers connection state, so you do not need separate rules for replies.
From Firewalls
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.
Next steps
- 3 more topics in “Networking and databases: VPC and RDS” before its exam: Managed relational databases: RDS and Aurora, Choosing a database: DynamoDB, caches and warehouses, DNS and CDN: Route 53 and CloudFront.
- Module 4 exam: Networking and databases, VPC and RDS · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
- Then the next module, Serverless, monitoring and cost control. 50 topics left in the course before the final exam.