← Solutions Architect Associate study path

Architecture fundamentals · 13 min

Subnets, route tables and NAT gateways in Amazon VPC

In this lesson you will learn how to plan the range of a VPC and its subnets, which route makes a subnet public, private or isolated and how to choose between a shared NAT gateway, one per zone or a regional one.

Amazon VPC is the service you use to create your own network in AWS, that is, the space where your servers connect to each other and to the internet. AWS defines a VPC as a virtual network dedicated to your account and logically isolated from the others, so that nobody from another account gets into it unless you allow it. Inside the VPC there are subnets, which are parts of the network where you launch your resources, and each subnet lives in a single Availability Zone and can't span two. For example, an online store creates a VPC and, inside it, a subnet in each zone for its web servers and another one in each zone for its database.

In a network, each machine has an IP address, which is a number that identifies it, like a house address, so that information reaches it and not another one. IPv4 addresses, the most common ones, are written as four numbers separated by dots, such as 10.0.0.5. The SAA-C03 exam asks you to design this network in tasks 3.4 and 1.2, so you need to know which address range you give the VPC, how many subnets you create, which route each one has and how it reaches the internet.

Accounts created after December 4, 2013 come with a default VPC in each Region, with one public subnet per zone and an internet gateway, which is the door between the VPC and the internet. That VPC is useful for quick tests, but for an architecture of your own you create your own VPC. For example, the store doesn't use the default VPC, because all the subnets there are public and its database would end up in one of them.

Which address range does the VPC get?

When you create a VPC you give it an IPv4 block, which is the group of addresses your resources will be able to use, and you write it in CIDR notation: a starting address followed by a slash and a number, such as 10.0.0.0/16. The bigger the number after the slash, the smaller the block. A VPC block can go from /16 (65,536 addresses) to /28 (16 addresses), so a /8, which is bigger than a /16, can't be used.

AWS recommends taking the block from the private ranges in (10.0.0.0/8, 172.16.0.0/12 and 192.168.0.0/16) and avoiding 172.17.0.0/16, which is used by services such as . For example, the store gives its VPC the block 10.0.0.0/16, which is inside 10.0.0.0/8 and leaves it 65,536 addresses to distribute.

This block can't overlap, that is, share addresses, with the networks you are going to connect, such as your or other VPCs, because two networks with the same range can't route to each other: when a message goes to 10.0.0.5, the network doesn't know which of the two to send it to. Also, the block doesn't grow if you run out of addresses. In that case you associate secondary blocks with the VPC, up to 5 per VPC by default counting the primary one, a limit that can be raised to 50. For example, if the store's office already uses 10.0.0.0/16 and the store wants to connect it to its VPC, the VPC needs another block, such as 172.16.0.0/16.

Subnet plan by tier and by zone

The subnets are carved out of the VPC block. A common way to do it is by tiers, which are groups of servers with the same function, where each tier is repeated in each Availability Zone.

Plan for a 10.0.0.0/16 VPC in three zones and three tiers: public /24, application /20 and data /24, with the free space below

A plan with three tiers (public, application and data) repeated in three zones has nine subnets in total. Repeating each tier in each zone is what lets the application keep running if a zone goes down, and the space in the block that the subnets don't use stays free for growth. For example, the store puts in the public tier what receives customers from the internet, in the application tier the servers that build the pages and process the orders, and in the data tier its database, and it repeats all three in each of its three zones.

Reserved addresses in each subnet

When you calculate the size of a subnet you have to subtract five addresses, because AWS reserves the first four and the last one for the network to work. In 10.0.0.0/24, those addresses are the following:

  • The .0, which is the network address, the one that names the whole subnet.
  • The .1, which is the VPC .
  • The .2, which is the .
  • The .3, which is reserved for future use by AWS.
  • The .255, which is the address.

That is why a /24, which has 256 addresses, leaves you 251 usable addresses, and a /28, 11. For example, if the store is going to have 20 servers in a subnet, a /28 isn't enough and a /24 leaves it room to grow.

Which subnet size?

You need a subnet for 28 EC2 instances, each with a private address. What is the smallest size that is enough?

What makes a subnet public?

Each subnet is associated with a route table, which is the list of instructions that says where to send each message that leaves the subnet. If you don't assign one, it uses the VPC's main route table. Each route in that table has a destination, which is an IP range, and a target, which is where that traffic is sent. For example, a route can say "send everything that goes to the internet through the internet gateway".

Every route table has the local route, which covers the VPC range, and that is why all the subnets in a VPC communicate with each other without you configuring anything. If a matches several routes, the most specific one wins (longest prefix match), which is the one with the smallest block. For example, a packet going to 10.0.1.8 matches 10.0.0.0/16 and 0.0.0.0/0, and 10.0.0.0/16 wins, so it stays inside the VPC.

Three route tables: the public one with 0.0.0.0/0 to the internet gateway, the application one with 0.0.0.0/0 to a NAT gateway and the data one with only the local route

Since every table has the local route, what sets one subnet apart from another is the 0.0.0.0/0 route, which covers everything that isn't the VPC, that is, the internet. Depending on that route, a subnet is one of three types:

  • The public subnet, where the route goes to an internet gateway and whose resources reach the internet if they also have a public IPv4 address or an address. For example, that is where the store puts what receives its customers.
  • The private subnet, where the route goes to a NAT gateway and whose resources reach the internet, although nobody outside can open a connection to them. For example, that is where the store's servers that download updates but shouldn't receive direct visits are.
  • The isolated subnet, where that route doesn't exist and whose resources only communicate with resources in the same VPC. For example, that is where the store's database is, which only talks to its servers.

The internet gateway is redundant, because AWS keeps it with spares, and scales on its own, and you can only attach one to each VPC. That is why an option with "two internet gateways for high availability" is incorrect.

What type of subnet is it?

0 of 4

Tap the column based on each subnet's route table.

  1. 10.0.0.0/16 local and 0.0.0.0/0 to igw-...

  2. 10.0.0.0/16 local and 0.0.0.0/0 to nat-...

  3. Only 10.0.0.0/16 local

  4. A new unassociated subnet, in a VPC whose main route table only has the local route

On the exam, a subnet is recognized by what the question asks of it. For example, "a subnet that has no internet access" is an isolated subnet, with no 0.0.0.0/0 route.

How many NAT gateways do you need?

A NAT gateway is the service that lets the resources in a private subnet reach the internet without letting anyone outside open a connection to them. A zonal NAT gateway lives in one Availability Zone and is redundant within it. That is why, if you have private subnets in three zones, you have three ways to give them internet access:

  • Design A, with a shared NAT gateway, which is the cheapest in hours, but which leaves the instances in the other zones without internet if its zone goes down, and whose traffic crosses zones to reach it while it works.
  • Design B, with one NAT gateway per zone, where each private subnet points in its route table to the NAT gateway in its zone, which is what AWS recommends for and which needs one public subnet and one route table per zone.
  • Design C, with a regional NAT gateway, which is a single one that extends to the zones where you have resources, with no public subnet, although it can take up to 60 minutes to reach a new zone and doesn't exist in private mode.

Three NAT designs: one shared in zone A, one per zone and a regional one that covers the three zones without a public subnet

For example, if the store uses design A with its NAT gateway in zone A and that zone fails, its servers in zones B and C keep running but can no longer download updates, while with design B or C each zone keeps its way out.

Cost of each design

In the AWS pricing example for US East (Ohio), a NAT gateway costs 0.045 USD per hour plus 0.045 USD per GB processed, that is, for each GB of data that goes through it. The regional NAT gateway charges the hour for each zone where it is present, so with three zones it costs the same in hours as design B. For example, the store with three zones pays for one NAT hour in design A and three NAT hours in design B or with the regional one, for each hour of the day.

This comparison decides the answer on the exam. A requirement such as "must keep downloading updates even if a zone fails" calls for one NAT gateway per zone or the regional one, while "the lowest cost" with no availability requirement accepts a shared one.

What can you practice in your AWS account?

Amazon VPC is in the free plan of the new sign-up experience, the account you open with Google, GitHub, Apple, Amazon or an email address, so subnets, route tables and NAT gateways work in both AWS accounts (in us-east-2 if you use the new experience). However, the NAT gateway charges by the hour from the moment you create it and uses up your credit, so it is worth deleting it in the same session you try it. AWS Transit Gateway and AWS Direct Connect aren't available in the new experience. For example, you can build the store's VPC with its nine subnets and its three route tables, create a NAT gateway to try out the private subnet and delete it when you finish.

Review question

The instances in a private subnet in three zones reach the internet through a single NAT gateway in zone A. One day zone A fails and the instances in zones B and C can't download patches. Which change prevents this from happening again with the least route management work?

Next