Skip to content

Beginner · Networking and databases: VPC and RDS · topic 15 of 50 · 30 min

VPC security and connectivity

Explanation

Security groups are stateful allow-only firewalls on each instance; network ACLs are stateless subnet firewalls with numbered allow and deny rules. VPC endpoints let private subnets reach AWS services without the internet. Peering, Transit Gateway, Site-to-Site VPN and Direct Connect join VPCs and offices, and Flow Logs record who talked to whom.

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 groupNetwork ACL
Applies toNetwork interface (instance)Whole subnet
RulesAllow onlyAllow and deny
StateStateful: replies allowed automaticallyStateless: replies need their own rule
OrderAll rules checked togetherLowest rule number first; first match wins
DefaultNew SG: no inbound, all outboundDefault NACL allows all; a new custom NACL denies all
Think of it like this: The NACL is the society gate guard with a printed list: he checks every person going in and out against numbered lines and stops at the first line that matches. The security group is the flat's own door with a smart lock: it knows who it let in, so it lets them back out without checking again.

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.

bash
aws ec2 authorize-security-group-ingress --group-id sg-0db111 --protocol tcp --port 5432 --source-group sg-0app222
The database accepts PostgreSQL traffic only from instances in the app security group.

Network 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 #DirectionProtocolPortSource/DestinationAction
90InboundAllAll198.51.100.0/24 (attacker)DENY
100InboundTCP4430.0.0.0/0ALLOW
100OutboundTCP1024-655350.0.0.0/0ALLOW
*BothAllAll0.0.0.0/0DENY

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.
bash
aws ec2 create-vpc-endpoint --vpc-id vpc-0abc --service-name com.amazonaws.ap-south-1.s3 --route-table-ids rtb-0priv
A gateway endpoint for S3 in Mumbai, added to the private route table.

Connecting 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.

output
2 111122223333 eni-0a1b2c3d 198.51.100.7 10.0.1.25 49152 22 6 3 180 1760870000 1760870060 REJECT OK
One flow log record: someone at 198.51.100.7 tried SSH (port 22, protocol 6 = TCP) and was rejected.
Careful: Common mistakes: (1) Adding a NACL inbound allow but forgetting the outbound ephemeral ports, so connections hang. (2) Trying to write a deny rule in a security group; SGs have no deny, use a NACL. (3) Expecting VPC peering to be transitive. (4) Peering two VPCs that both use 10.0.0.0/16. (5) Paying NAT gateway data charges for S3 traffic that a free gateway endpoint could carry.

Need 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

SG = instance NACL = subnet

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)

Task 1 · Question

You must block one attacking IP range from reaching every instance in a subnet. Where do you add the rule?

Task 2 · Code · Python

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.

Task 3 · Practice

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

Question 1

1.Which statement about network ACLs is true?

Question 2

2.Which services can use a gateway VPC endpoint?

Question 3

3.VPC A is peered with B, and B is peered with C. Can A reach C through B?

Question 4

4.What do VPC Flow Logs NOT contain?

Question 5

5.Which option gives a dedicated private line from your data centre to AWS?

Read more (free)

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.

From VPC basics: CIDR, subnets and gateways

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.

From Virtualisation, VRFs and cloud networking

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.

From Azure networking, Functions, databases and monitoring

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

  1. 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.
  2. Module 4 exam: Networking and databases, VPC and RDS · 14 questions · 25 min, without help. 5 topics of this module still to finish first.
  3. Then the next module, Serverless, monitoring and cost control. 50 topics left in the course before the final exam.

← Previous: VPC basics: CIDR, subnets and gateways