Learn to Code
All tracks
Learn to Code

Free lessons from scratch to pro — every language, databases and DevOps.

AboutContactPrivacy PolicyTerms of Use

Your progress is saved in this browser. © 2026 Learn to Code

CL
Cloud (AWS, GCP, Azure)
0/12 completed
Beginner
What is cloud computing?AWS, Google Cloud and Azure comparedAccounts, billing alerts and the CLI
Intermediate
Identity and access (IAM)Virtual machinesObject storageNetworking: VPCs, subnets and load balancersManaged databases
Advanced
Serverless: functions, queues and eventsContainers in the cloud
Pro
Infrastructure as codeCost control, security and reliability
Tracks / Cloud (AWS, GCP, Azure) / Intermediate

Networking: VPCs, subnets and load balancers

Every cloud resource with an IP address lives in a virtual private cloud (VPC): your own isolated network, divided into subnets, with route tables, gateways and firewalls deciding what can talk to what. Good network design keeps databases and internal services off the internet, while letting the right traffic in. This lesson builds the classic layout used by most web applications.

💡 Networking concepts from the Linux course (Networking commands) apply directly: IP addresses, CIDR ranges, ports and DNS.

The classic layout

VPC 10.0.0.0/16  (ap-south-1)
├── AZ a
│   ├── public subnet   10.0.1.0/24   → load balancer, NAT gateway
│   └── private subnet  10.0.11.0/24  → app servers, database
└── AZ b
    ├── public subnet   10.0.2.0/24   → load balancer
    └── private subnet  10.0.12.0/24  → app servers, database standby

Internet ⇄ Internet gateway ⇄ public subnets ⇄ private subnets ──(outbound only)──▶ NAT gateway ▶ Internet
Piece Job
VPC Your private network, with an address range like 10.0.0.0/16 (65,536 addresses)
Subnet A slice of the VPC in one availability zone, e.g. 10.0.1.0/24 (256 addresses)
Public subnet Has a route to the internet gateway: things here can have public IPs
Private subnet No route from the internet. Servers here can't be reached from outside
NAT gateway Lets private servers make outbound connections (updates, APIs) without being reachable
Route table Rules: "traffic for 0.0.0.0/0 goes to the internet gateway"
Security group A firewall on each resource (stateful: replies are allowed automatically)

Only the load balancer faces the internet. App servers and databases sit in private subnets, reachable only from inside the VPC.

CIDR in one minute

10.0.0.0/16 means "the first 16 bits are fixed": addresses 10.0.0.0–10.0.255.255. Bigger number after the slash = smaller network:

CIDR Addresses Typical use
/16 65,536 A whole VPC
/24 256 (AWS reserves 5) A subnet
/32 1 A single IP in a firewall rule
0.0.0.0/0 Everything "The internet"

Pick VPC ranges that don't overlap with your office network or other VPCs you might connect later.

Building it

bash
vpc=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 --tag-specifications 'ResourceType=vpc,Tags=[{Key=Name,Value=shop-vpc}]' --query Vpc.VpcId --output text)
pub_a=$(aws ec2 create-subnet --vpc-id "$vpc" --cidr-block 10.0.1.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
pub_b=$(aws ec2 create-subnet --vpc-id "$vpc" --cidr-block 10.0.2.0/24 --availability-zone ap-south-1b --query Subnet.SubnetId --output text)
priv_a=$(aws ec2 create-subnet --vpc-id "$vpc" --cidr-block 10.0.11.0/24 --availability-zone ap-south-1a --query Subnet.SubnetId --output text)
priv_b=$(aws ec2 create-subnet --vpc-id "$vpc" --cidr-block 10.0.12.0/24 --availability-zone ap-south-1b --query Subnet.SubnetId --output text)

igw=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id "$igw" --vpc-id "$vpc"
rt=$(aws ec2 create-route-table --vpc-id "$vpc" --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id "$rt" --destination-cidr-block 0.0.0.0/0 --gateway-id "$igw" > /dev/null
aws ec2 associate-route-table --route-table-id "$rt" --subnet-id "$pub_a" > /dev/null
aws ec2 associate-route-table --route-table-id "$rt" --subnet-id "$pub_b" > /dev/null

aws ec2 describe-subnets --filters "Name=vpc-id,Values=$vpc" \
  --query "sort_by(Subnets, &CidrBlock)[].[CidrBlock, AvailabilityZone, AvailableIpAddressCount]" --output table
echo "--- public route table:"
aws ec2 describe-route-tables --route-table-ids "$rt" --query "RouteTables[0].Routes[].[DestinationCidrBlock, GatewayId]" --output text | sed -E 's/igw-[0-9a-f]+/igw-…/'
printf '%s\n' "$vpc" "$pub_a" "$pub_b" "$priv_a" "$priv_b" > ids.txt
Output
----------------------------------------
|            DescribeSubnets           |
+---------------+---------------+------+
|  10.0.1.0/24  |  ap-south-1a  |  251 |
|  10.0.11.0/24 |  ap-south-1a  |  251 |
|  10.0.12.0/24 |  ap-south-1b  |  251 |
|  10.0.2.0/24  |  ap-south-1b  |  251 |
+---------------+---------------+------+
--- public route table:
10.0.0.0/16	local
0.0.0.0/0	igw-…

local means "anywhere inside the VPC": every VPC route table has it, so all subnets can reach each other. The public subnets additionally send everything else to the internet gateway. Private subnets keep only the local route (plus, in real setups, 0.0.0.0/0 → NAT gateway for outbound access).

💡 Real projects rarely type all this by hand. Infrastructure as code (Terraform, CloudFormation) builds the whole layout from one file, as you'll see later. Clicking through the console's "VPC and more" wizard also creates it in one go.

Security groups that reference each other

Instead of allowing IP ranges, allow other security groups. "The database accepts PostgreSQL only from the app servers" stays correct however many app servers there are:

bash
read -r vpc pub_a pub_b priv_a priv_b < <(tr '\n' ' ' < ids.txt)
lb_sg=$(aws ec2 create-security-group --vpc-id "$vpc" --group-name lb-sg --description "Load balancer" --query GroupId --output text)
app_sg=$(aws ec2 create-security-group --vpc-id "$vpc" --group-name app-sg --description "App servers" --query GroupId --output text)
db_sg=$(aws ec2 create-security-group --vpc-id "$vpc" --group-name db-sg --description "Database" --query GroupId --output text)

aws ec2 authorize-security-group-ingress --group-id "$lb_sg" --protocol tcp --port 443 --cidr 0.0.0.0/0 > /dev/null
aws ec2 authorize-security-group-ingress --group-id "$app_sg" --protocol tcp --port 8080 --source-group "$lb_sg" > /dev/null
aws ec2 authorize-security-group-ingress --group-id "$db_sg" --protocol tcp --port 5432 --source-group "$app_sg" > /dev/null

echo "group   port  allowed from"
for g in "$lb_sg" "$app_sg" "$db_sg"; do
  aws ec2 describe-security-groups --group-ids "$g" \
    --query "SecurityGroups[0].[GroupName, IpPermissions[0].FromPort, IpPermissions[0].IpRanges[0].CidrIp || IpPermissions[0].UserIdGroupPairs[0].GroupId]" --output text
done | sed "s/$lb_sg/lb-sg/; s/$app_sg/app-sg/" | awk '{ printf "%-7s %-5s %s\n", $1, $2, $3 }'
echo "$lb_sg" > lb_sg.txt
Output
group   port  allowed from
lb-sg   443   0.0.0.0/0
app-sg  8080  lb-sg
db-sg   5432  app-sg

The internet reaches only the load balancer on 443; the load balancer reaches the apps on 8080; only the apps reach the database on 5432.

Load balancers

A load balancer spreads requests across healthy servers, terminates HTTPS, and stops sending traffic to servers that fail their health check:

Type AWS Google Cloud Azure Works at
HTTP(S), path/host routing ALB (Application LB) Application Load Balancer Application Gateway Layer 7
TCP/UDP, very high performance NLB (Network LB) Network Load Balancer Load Balancer Layer 4
Global, with CDN and WAF CloudFront + ALB Global external ALB Front Door Edge
bash
read -r vpc pub_a pub_b priv_a priv_b < <(tr '\n' ' ' < ids.txt)
lb=$(aws elbv2 create-load-balancer --name shop-alb --type application --scheme internet-facing --subnets "$pub_a" "$pub_b" \
  --security-groups "$(cat lb_sg.txt)" --query "LoadBalancers[0].LoadBalancerArn" --output text)
tg=$(aws elbv2 create-target-group --name shop-app --protocol HTTP --port 8080 --vpc-id "$vpc" \
  --health-check-path /health --health-check-interval-seconds 15 --query "TargetGroups[0].TargetGroupArn" --output text)
aws elbv2 create-listener --load-balancer-arn "$lb" --protocol HTTP --port 80 \
  --default-actions Type=forward,TargetGroupArn="$tg" --query "Listeners[0].[Protocol, Port]" --output text
aws elbv2 describe-load-balancers --load-balancer-arns "$lb" \
  --query "LoadBalancers[0].[Type, Scheme, length(AvailabilityZones)]" --output text
aws elbv2 describe-target-groups --target-group-arns "$tg" --query "TargetGroups[0].[HealthCheckPath, Port]" --output text
Output
HTTP	80
application	internet-facing	2
/health	8080
  • Listener: what the load balancer accepts (in production, HTTPS on 443, with a free certificate from AWS Certificate Manager, plus an HTTP listener that redirects to HTTPS).
  • Target group: where requests go (app servers on 8080), and the health check that decides which targets are healthy.
  • An auto-scaling group registers new VMs in the target group automatically.

Point your domain at the load balancer's DNS name with a CNAME or alias record (Route 53, Cloud DNS, Azure DNS, or your registrar).

Connecting networks

Need Solution
Two VPCs talk privately VPC peering, or a Transit Gateway for many VPCs
Office ⇄ cloud Site-to-site VPN, or a dedicated line (Direct Connect, Cloud Interconnect, ExpressRoute)
Private access to S3 and other cloud services without the internet VPC endpoints (AWS), Private Service Connect (GCP), Private Endpoints (Azure)
Admins reach private servers Session Manager / IAP / Azure Bastion (no SSH ports open)

VPC endpoints also save money: traffic to S3 through a gateway endpoint doesn't go through the (per-gigabyte-priced) NAT gateway.

Google Cloud and Azure differences

  • Google Cloud VPCs are global: one VPC spans all regions, and each subnet is regional (not per zone). Firewall rules belong to the VPC and target instances by network tags or service accounts.
  • Azure VNets are regional; subnets can span zones. Firewall rules are network security groups (NSGs), attached to subnets or network interfaces.
bash
gcloud compute networks create shop-vpc --subnet-mode=custom
gcloud compute networks subnets create app --network=shop-vpc --region=asia-south1 --range=10.0.11.0/24
gcloud compute firewall-rules create allow-lb-to-app --network=shop-vpc --allow=tcp:8080 \
  --source-ranges=130.211.0.0/22,35.191.0.0/16 --target-tags=app

az network vnet create --resource-group demo-rg --name shop-vnet --address-prefix 10.0.0.0/16 \
  --subnet-name app --subnet-prefixes 10.0.11.0/24
az network nsg rule create --resource-group demo-rg --nsg-name app-nsg --name allow-https \
  --priority 100 --destination-port-ranges 443 --access Allow --protocol Tcp

Practice

  1. How many usable addresses does a /26 subnet have on AWS (remember the 5 reserved)?
  2. Draw the layout for an app with a public website, a private API and a database, across two AZs.
  3. Write the security group rules for: web servers reachable on 443 from anywhere, an admin panel on 8443 reachable only from your office IP.
  4. Why should a database never be in a public subnet, even with a strong password?
✎ Check your understanding

1.What makes a subnet "public"?

2.How can servers in a private subnet download software updates?

3.Why allow traffic from a security group instead of an IP range?

4.What does a load balancer's health check do?

← Object storage