← Solutions Architect Associate study path

Secure architectures · 13 min

Subnets, security groups and network ACLs in the VPC

In this lesson you will learn how a three-tier VPC with public and private subnets is built, how security groups are chained, how a network ACL is different and why a response can get blocked in it.

A network is a set of connected computers that can send data to each other, and each one has an . A VPC (Amazon Virtual Private Cloud) is your isolated network inside AWS: nobody else sees it, and you decide what comes in and what goes out. A subnet is a range of addresses in that VPC within an , that is, a piece of the network where you put certain resources.

Separating an application into subnets and filtering the traffic between them is the part of security that decides what traffic you let through. For example, an online store puts what customers reach from the internet in one subnet and the orders database in another, and only lets the first one talk to the second.

Public and private subnets

Whether a subnet is public or private depends on a single route. A route table is the list that says where a subnet's traffic goes out depending on where it is headed, and an internet gateway is the door that connects the VPC to the internet. If a subnet's route table sends 0.0.0.0/0, which means any address, to an internet gateway, the subnet is public, and if it doesn't have that route, it is private. For example, the subnet where the store receives its customers has that route and is public, and the subnet of its database doesn't have it, so nobody on the internet can reach it.

Turn the internet gateway and the NAT gateway on and off

A VPC with a public subnet and a private one. The public one holds the web server and the NAT gateway; the private one, an instance that needs to download patches. Turn the two doors to the internet on and off.

Internet gateway
NAT gateway
  • From the internet to the web serverPublic subnetAllowed
  • From the private instance to the internetTo download patchesAllowed
  • From the internet to the private instanceA connection that starts outsideBlocked
  • Between the two subnetsLocal routeAllowed

What happened?

The public subnet has its 0.0.0.0/0 route to the internet gateway, so the web server gets visits. The private subnet goes out through the NAT gateway, which sits in the public subnet, and nobody outside can open a connection to it.

The three-tier VPC

Exam questions that describe a secure web application usually use a three-tier VPC, where each Availability Zone has a public subnet, an application subnet and a data subnet.

Three-tier VPC across three zones: public subnets with the load balancer and NAT gateways, application subnets with the instances, and isolated data subnets with the database

Only the and the NAT gateways go in the public subnet. The , which are the servers where the program runs, go in the application subnet, without a public IP, and they reach the internet through the NAT gateway in their zone, which lets the instances open outbound connections without anyone outside being able to open a connection to them. Finally, the goes in the data subnet, with no route to the internet at all. For example, a store customer reaches the load balancer in the public subnet, the load balancer passes the request to an instance in the application subnet, and that instance looks up the orders in the data subnet; the instance can download updates through the NAT gateway, but nobody on the internet can connect to it directly.

The VPC security best practices call for that same design and add other controls. Among them are subnets in multiple Availability Zones, so the application keeps working if a zone fails, security groups for the instances, network ACLs at the subnet level and IAM to manage the VPC. They also recommend VPC Flow Logs to see the traffic, Network Access Analyzer to find access that nobody meant to open, AWS Network Firewall for filtering and Amazon GuardDuty for detecting threats. For example, with VPC Flow Logs the store can see which addresses tried to connect to its instances and whether those connections were accepted or rejected.

Security groups

A security group is a list of rules that says who can connect to a resource and on which , and it filters the traffic of the resources associated with it, such as an instance, a load balancer or a database. It works on each resource's and not on the subnet, and it stands out for three characteristics:

  • Allow rules, which are the only rules it has, because deny rules don't exist in a security group.
  • Connection state, which the group remembers, so if it let a request in it lets its response out without needing another rule.
  • References to other groups, which let a rule point to another security group instead of an IP range.

Three chained security groups: the load balancer's accepts HTTPS from the internet, the application's accepts its port only from the load balancer's group and the database's accepts PostgreSQL only from the application's group

The third characteristic is what chains the security groups of the three tiers of the VPC. HTTPS is the secure way a browser talks to a site, and PostgreSQL is a type of database, each with its own port. Only the load balancer's inbound rule uses an IP range (0.0.0.0/0), while the application's points to the load balancer's group and the database's points to the application's group. That is why, when launches a new instance with the application's group, the database accepts it without you changing anything, and an instance in the same subnet that doesn't have that group stays out. For example, during a special sale the store adds more instances, and the new ones can look up orders from the first minute because they carry the application's group.

Network ACLs

A network ACL filters what goes in and out of an entire subnet, and each subnet has exactly one. Almost everything about it works differently than in a security group:

  • Its rules, which can allow or deny.
  • Its evaluation order, which goes from the lowest-numbered rule to the highest, where the first rule that matches decides.
  • Its lack of state, which forces you to write a rule for the response as well.
  • Its initial behavior, where the VPC's default ACL allows everything and an ACL you create denies everything until you add rules to it.

Because of those differences, AWS treats the two filters as different mechanisms. The security group is the primary mechanism for controlling network access, and the network ACL is a secondary control, one more layer of defense that is used to deny something to an entire subnet. For example, if an internet address is attacking the store, a deny rule in the public subnet's network ACL blocks it for everything in that subnet, which a security group can't do because it only has allow rules.

Security group or network ACL?

0 of 4

Tap the filter that meets each requirement.

  1. Block an IP range that is attacking all the instances in a subnet

  2. The database accepts connections only from the application instances, even if Auto Scaling changes their IPs

  3. A stateful filter, so you don't write response rules

  4. A second barrier in case someone launches an instance with the wrong group

Why does a response get stuck in the network ACL?

The network ACL's lack of state has a consequence the exam asks about: a request can get in and its response can fail to get out, because the ACL checks the response as if it were new traffic.

An HTTPS request comes in through the network ACL and the security group; the response goes out through the group without a rule and the outbound network ACL checks it as new traffic toward a high port on the client

The client opens the connection from one of its high ports, for example 50123, to port 443 on the server, and on the way in, the ACL and the security group allow 443. On the way out, the security group remembers the connection and lets the response through. The ACL, on the other hand, doesn't remember anything, so to it the response is new traffic toward port 50123 on the client, and if its outbound rule doesn't allow it, the response never arrives.

That high port on the client is called an ephemeral port, because the client's computer picks it when it opens the connection and releases it when it closes it. The VPC documentation uses the range 1024-65535 in its examples of ACL rules for those ports. For example, if the store writes an outbound rule in its ACL that allows that range, the pages reach its customers' browsers again. TCP, which shows up in the rules, is the most common way to send data over the internet, and HTTPS travels on top of it.

What is missing?

The server's security group allows inbound TCP 443. The network ACL of its subnet allows inbound TCP 443 and outbound TCP 443, and nothing else. The site doesn't load. What do you change?

In a new experience account

In a new experience account, the VPC, security groups, network ACLs, NAT gateways and Flow Logs are included in the free plan. However, a NAT gateway charges every hour from the moment you create it and uses up your , so delete it in the same session you use it. For example, if you build the store's VPC to practice, create the NAT gateway, check that the private instance downloads updates and delete it before you close the console. AWS Network Firewall is only on the paid plan, and GuardDuty isn't available without the advanced features.

How does it show up on the exam?

Questions on this topic usually ask for something not to be reachable from the internet or for a filter to let only certain traffic through, and these phrases in the question point to the answer:

If the question says The answer
"The database must not be accessible from the internet" A subnet with no route to the internet gateway and a security group that only accepts the application
"The private instances need to download updates" A NAT gateway in a public subnet, and one per zone if they ask to withstand the failure of a zone
"Deny an IP" or "stateless" A network ACL
"Only from the load balancer" or "at the instance level" A security group
"SSH open to 0.0.0.0/0" in an option It is usually a mistake, because Session Manager gives access to the instance without opening inbound ports

SSH is a way to get into a server from another computer to type commands on it, and opening it to 0.0.0.0/0 lets anyone on the internet try. For example, if an option leaves the store's database in a public subnet so the team can check it more easily, that option is wrong even if the rest looks correct.

Review question

An application has a public Application Load Balancer, EC2 instances in private subnets and an Amazon RDS database. An analysis found that any instance in the VPC can connect to port 5432 on the database. Which change limits access to only the application instances, including the ones Auto Scaling launches later?

Next