DevOps.Academy
CoursesCurriculumInterview prepPricing
◔My learningSign in
DevOps.Academy

Free, hands-on DevOps lessons. Made with care in India.

System status

Learn

All coursesLinux roadmapCurriculumInterview questionsCheat sheetsMy learning

Academy

PricingCertificates

Legal

PrivacyTermsContact

devops.rajeev.pro

AWS for DevOpsLesson 4 of 17
Your progress · 0%
On this page
  1. Why a network of your own
  2. Plan the addresses
  3. Subnets in two AZs
  4. What makes a subnet public
  5. NAT: the way out for private subnets
  6. Two firewalls
  7. Endpoints, peering and flow logs
  8. Clean up
↖ Course roadmap
Lesson 4 · VPC

VPC: your own private network

Build a real VPC with public and private subnets in two AZs, read its route tables, and see how internet gateways, NAT, security groups, network ACLs and endpoints fit together.

35 min read · intermediate · hands-on

By the end of this lesson you'll be able to

0 of 6 complete

Why a network of your own

In lesson 3 the server landed in the default VPC, where the default subnets can assign public addresses. That is convenient for a first test, but a database should not accept connections directly from the internet. Teams need a network plan that separates public entry points from private application and data resources.

A Virtual Private Cloud (VPC) is an isolated network address space in one AWS Region. You choose its IP range, divide it into subnets in Availability Zones, and attach route tables and gateways to control paths. The route table and resource settings determine reachability; the word "private" in VPC does not automatically make every resource private.

This lesson builds two public and two private subnets across two Availability Zones. A public load balancer uses the public subnets; app servers and databases belong in the private ones. You'll trace the route tables and see how a gateway endpoint can reach S3 without general internet access.

The target for this lesson, built in a real account and deleted afterwards:

SubnetAZRangeCan the internet reach it?
public 1ap-south-1a10.0.0.0/20yes, through the internet gateway
public 2ap-south-1b10.0.16.0/20yes
private 1ap-south-1a10.0.128.0/20no
private 2ap-south-1b10.0.144.0/20no

Load balancers go in the public subnets; app servers and databases in the private ones (lesson 5 uses this).

Plan the addresses

Before creating subnets, choose address ranges that do not overlap with each other or with networks that must connect later. Otherwise a router cannot tell which network owns an address, and a VPN or peering connection may fail even when both sides look healthy.

A CIDR block is a range written as a network address and a prefix length. The prefix says how many bits identify the network; the remaining bits identify addresses inside it. A smaller prefix number leaves more host bits and therefore more addresses. AWS reserves five addresses in each IPv4 subnet, so the number of usable addresses is lower than the raw range size.

The table gives the raw address counts:

CIDRAddressesTypical use
10.0.0.0/1665,536a whole VPC (the largest AWS allows)
10.0.0.0/204,096one subnet
10.0.0.0/24256a small subnet

AWS reserves 5 addresses in every subnet (the first four and the last), so a /24 gives 251 usable. Use the private ranges 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16, and give every VPC a range that doesn't overlap the others or your office network, or you can never connect them later.

Here the VPC uses 10.0.0.0/16, and each subnet gets a /20 slice. This leaves room for multiple subnets while keeping each one large enough for the demo. In production, plan for expected growth and every connected network before creating the VPC; changing address plans later can require replacing resources. A common mistake is assigning the same RFC 1918 range to both sides of a VPN and expecting routes to distinguish them.

Subnets in two AZs

Why two Availability Zones? An AZ is one or more data centres in a region. Now and then one has a power or network problem. If all your servers sit in one AZ, that problem takes your whole app down. Put them in two AZs and the other one keeps serving. So every layer of a production network comes in pairs: a subnet in each AZ, and servers spread across both.

Public or private subnet? A subnet doesn't have a "public" switch. What makes it public is its route table: if it has a route to an internet gateway, machines in it with a public IP can be reached from the internet. A private subnet has no such route. Its machines can still reach out (to download updates) through a NAT gateway, but nobody on the internet can connect in. Load balancers go in public subnets. App servers and databases go in private ones.

Public subnets have a route to the internet gateway. Private subnets don't, so the internet can't reach the app servers.© Diagram: DevOps Academy

Public subnets have a route to the internet gateway. Private subnets don't, so the internet can't reach the app servers.© Diagram: DevOps Academy

VPC → Your VPCs → Create VPC → VPC and more builds the whole network in one go, and draws it as you type:

(1) VPC and more. (2) A name prefix for everything it creates. (3) The VPC's address range.© Screenshot: DevOps Academy

(1) VPC and more. (2) A name prefix for everything it creates. (3) The VPC's address range.© Screenshot: DevOps Academy

(1–3) Two AZs, each with a public and a private subnet. (4) No NAT gateway: it costs money by the hour. (5) A free S3 gateway endpoint.© Screenshot: DevOps Academy

(1–3) Two AZs, each with a public and a private subnet. (4) No NAT gateway: it costs money by the hour. (5) A free S3 gateway endpoint.© Screenshot: DevOps Academy

Everything the wizard did for you, step by step. Each of these is one API call you could also script.© Screenshot: DevOps Academy

Everything the wizard did for you, step by step. Each of these is one API call you could also script.© Screenshot: DevOps Academy

The VPC's Resource map tab shows how the pieces connect:

(1) Subnets → (2) their route tables → (3) where traffic can leave: the internet gateway or the S3 endpoint.© Screenshot: DevOps Academy

(1) Subnets → (2) their route tables → (3) where traffic can leave: the internet gateway or the S3 endpoint.© Screenshot: DevOps Academy

(1) A public subnet's range. (2) A private one. Same size, different route tables.© Screenshot: DevOps Academy

(1) A public subnet's range. (2) A private one. Same size, different route tables.© Screenshot: DevOps Academy

What makes a subnet public

The name "public subnet" does not come from a checkbox or from the subnet's CIDR. Its route table determines where traffic can go. A route table is a set of destination ranges and next hops. When an instance sends a packet, the VPC uses the most specific matching route to choose where it goes.

For internet access, a public subnet needs a default route (0.0.0.0/0) to an internet gateway. The VPC must have that gateway attached, and an instance also needs a public IPv4 address plus a security group that allows the intended inbound traffic. A route alone does not make the instance reachable.

The screenshots contrast the public route table with a private one:

The public route table: 0.0.0.0/0 (everything not local) goes to the internet gateway.© Screenshot: DevOps Academy

The public route table: 0.0.0.0/0 (everything not local) goes to the internet gateway.© Screenshot: DevOps Academy

A private route table: only local traffic and S3 through the endpoint. No route to the internet at all.© Screenshot: DevOps Academy

A private route table: only local traffic and S3 through the endpoint. No route to the internet at all.© Screenshot: DevOps Academy
RouteMeans
10.0.0.0/16 → localin every route table: any subnet can reach any other inside the VPC
0.0.0.0/0 → igw-…the internet gateway: makes the subnet public, both directions
pl-… → vpce-…S3 through the gateway endpoint, without the internet

An instance in a public subnet also needs a public IP to be reachable; one in a private subnet has only a private address, and nothing outside the VPC can start a connection to it.

Use public subnets for internet-facing load balancers and private subnets for application servers and databases. Keep private routes private unless the workload needs outbound access. A common mistake is to add an internet-gateway route and assume that is sufficient; confirm address assignment and security-group rules as well.

NAT: the way out for private subnets

Private servers may need to download operating-system updates or call an external API, but they should not accept unsolicited connections from the internet. A Network Address Translation (NAT) gateway lets instances initiate outbound IPv4 connections while hiding their private source addresses behind a public address. It sits in a public subnet; a private subnet's default route points to it.

The packet leaves the private instance, follows 0.0.0.0/0 → nat-…, and reaches the NAT gateway. The gateway rewrites the source address for the outbound connection and tracks the reply so it can send the response back to the instance. It does not create an inbound path for a new internet connection. NAT gateways are zonal or regional options in the VPC wizard; their availability and pricing differ, so check current AWS details before choosing.

NAT gateways cost money even when idle

A NAT gateway is billed per hour and per GB processed, plus its public IPv4 address: tens of dollars a month for one sitting idle. That's why the wizard defaults to None and this lesson didn't create one. For learning, skip it; in production, size it on purpose, and use gateway endpoints for S3 and DynamoDB traffic.

Use a NAT gateway when private workloads need general outbound internet access and the cost is justified. For AWS services such as S3 and DynamoDB, a gateway VPC endpoint may avoid routing that traffic through NAT. For a small lab that needs no outbound access, choose None. A common mistake is creating one NAT gateway per AZ for a tutorial and forgetting that each gateway has an hourly charge.

How do I set up a NAT gateway for a private subnet in Amazon VPC?Amazon Web Services · Watch on YouTube ↗

Two firewalls

Security groupNetwork ACL
Attached toan instance (its network interface)a subnet
Statestateful: a reply to allowed traffic is allowedstateless: rules needed in both directions
Rulesallow onlyallow and deny, evaluated in number order
Defaultnothing in, everything outthe default ACL allows everything

(1) Rule 100 allows everything. (2) The * rule denies whatever no numbered rule matched.© Screenshot: DevOps Academy

(1) Rule 100 allows everything. (2) The * rule denies whatever no numbered rule matched.© Screenshot: DevOps Academy

A VPC's default security group only lets in traffic from other members of itself.© Screenshot: DevOps Academy

A VPC's default security group only lets in traffic from other members of itself.© Screenshot: DevOps Academy

Stateful or stateless, in practice. Your server sends a request to an update server on port 443. The reply comes back to a random high port on your server (say 49731). A security group remembers the outgoing request, so it lets the reply in without any extra rule. A network ACL remembers nothing: it checks the reply like any other incoming packet, so you'd need an inbound rule allowing the whole range of those high ports (1024–65535). That's why ACLs are easy to get wrong.

Most teams leave network ACLs at their defaults and filter with security groups, because a security group can name another security group as the source ("only from the load balancer"), which lesson 5 uses.

Endpoints, peering and flow logs

  • Gateway endpoints (S3 and DynamoDB only) add a route, cost nothing, and keep that traffic off the NAT gateway. The wizard created one: academy-vpce-s3.
  • Interface endpoints (PrivateLink) put a private IP for an AWS service (ECR, Secrets Manager, SSM) inside your subnet. They cost per hour, and they let private subnets work with no internet access at all.
  • VPC peering connects two VPCs one-to-one; ranges must not overlap. Transit Gateway is a hub for many VPCs and VPN connections, when peering every pair gets out of hand.
  • VPC Flow Logs record accepted and rejected connections per interface, to CloudWatch Logs or S3: the first thing to turn on when "the network is blocking it" (lesson 13).

Clean up

Delete the VPC from Your VPCs → Actions → Delete VPC; the console deletes its subnets, route tables, internet gateway and endpoint with it. From the CLI you delete them yourself, in order:

$ aws ec2 delete-vpc-endpoints --vpc-endpoint-ids vpce-…
$ aws ec2 detach-internet-gateway --internet-gateway-id igw-… --vpc-id vpc-…
$ aws ec2 delete-internet-gateway --internet-gateway-id igw-…
$ aws ec2 delete-subnet --subnet-id subnet-…                 # each of the four
$ aws ec2 delete-route-table --route-table-id rtb-…          # each non-main table
$ aws ec2 delete-vpc --vpc-id vpc-…
aws ec2 delete-vpc-endpoints --vpc-endpoint-ids vpce-…
  • awsthe AWS command line
In production

Plan non-overlapping CIDRs for every VPC and environment up front, use at least two AZs, keep databases and app servers in private subnets, give each team or environment its own VPC (or account), turn on Flow Logs, and build it all with Terraform or CloudFormation (lesson 16) rather than the wizard.

⌘
Mini mission

Build and read a two-tier network

Create a VPC with 2 AZs, 2 public and 2 private subnets, no NAT gateway and an S3 gateway endpoint. Prove from the route tables which subnets are public, then delete the whole VPC and confirm it's gone.

Input: An AWS account with the console and AWS CLIOutput: Route tables showing 0.0.0.0/0 to an internet gateway only for the public subnets, then no academy-vpc left
Reveal one safe solution
$ # 1. Console: VPC → Create VPC → VPC and more, name academy, 10.0.0.0/16, 2 AZs, 2 public, 2 private,
$ #    NAT None, S3 Gateway → Create VPC
$ V=$(aws ec2 describe-vpcs --filters Name=tag:Name,Values=academy-vpc --query 'Vpcs[0].VpcId' --output text)
$ aws ec2 describe-route-tables --filters Name=vpc-id,Values=$V \
    --query 'RouteTables[].[Tags[?Key==`Name`]|[0].Value, Routes[?DestinationCidrBlock==`0.0.0.0/0`]|[0].GatewayId]' --output text
$ # 2. Console: Your VPCs → select academy-vpc → Actions → Delete VPC → type delete
$ aws ec2 describe-vpcs --filters Name=tag:Name,Values=academy-vpc --query 'length(Vpcs)'   # 0 when it's gone
aws ec2 describe-route-tables --filters Name=vpc-id,Values=$V \
  • awsthe AWS command line
aws ec2 describe-vpcs --filters Name=tag:Name,Values=academy-vpc --query 'length(Vpcs)'
  • --querypick fields from the JSON response

Expect one line per route table: only academy-rtb-public shows an igw-… ID; the private tables and the main table show None, because they have no route to the internet. Without a NAT gateway this network costs nothing, so you can rebuild it whenever you like.

Try it yourself

0 of 6 steps done

Run each task in your own account or terminal, then tick it off.

Knowledge check

Quick check

Question 1 of 5

What makes a subnet public?

Go deeper

Official reading for this lesson

  • DocsHow Amazon VPC worksAWS Docs ↗
  • DocsSubnet CIDR blocksAWS Docs ↗
  • DocsNAT gatewaysAWS Docs ↗
  • DocsCompare security groups and network ACLsAWS Docs ↗
  • DocsVPC Flow LogsAWS Docs ↗

Or get every quiz answer right and it completes itself.

Something unclear, wrong or out of date in this lesson? Tell me, and I'll fix it.

← PreviousEC2: your first server in the cloudNext lesson →Scaling and load balancing