
Course 3 Β· Networking
A private network with a single front door
π― By the end of today
- You can design a VPC (your own private network inside AWS) with an IP plan that won't clash with other networks.
- You can tell a public subnet from a private one by reading its route table.
- You can chain security groups so each layer only accepts traffic from the layer in front of it.
- You can say when to use peering, Transit Gateway, a VPN or Direct Connect, and what a NAT gateway costs you.
- Your app and your database run in private subnets, behind an Application Load Balancer, and you've proven an intruder can't reach them (project M2).
ποΈ The plan for today
| Time | What | Where |
|---|---|---|
| 0:00 (10 min) | Recap: what's wrong with your M1 deploy | Lecture |
| 0:10 (35 min) | VPC, CIDR, public vs private subnets, route tables, internet gateway, NAT | Lecture |
| 0:45 (25 min) | Security groups vs NACLs, the chain, endpoints, connecting networks | Lecture |
| 1:10 (15 min) | Load balancers, and what Floci emulates vs what real AWS routes | Lecture |
| 1:25 (10 min) | Break | |
| 1:35 (25 min) | Project M2, steps 1 and 2: your VPC, subnets and doors | Project |
| 2:00 (60 min) | Steps 3 to 6: the security group chain, the new ALB, app and database move in | Project |
| 3:00 (25 min) | Step 7: the intruder test, then the break drill inside your group | Project |
| 3:25 (20 min) | Steps 8 and 9: tear down the naive deploy, check M2, commit | Project |
| 3:45 | Buffer. Not done? Course 4's project block opens with 15 minutes of catch-up |
π§± The VPC: your own private network
In last session, your database had a public address, so anyone on the internet could try to connect, and only a password stopped them.
A private network solve this problem.
A VPC (Virtual Private Cloud) is a private network in one Region, and it spans all of that Region's Availability Zones (AZs, the separate data centers inside a Region).
Nothing gets in unless you open a way in, like a gated neighborhood where you draw the streets and build the gates.
The default VPC you used in last session has public subnets everywhere, so things work on the first try.
That's also why your database ended up public, and a real app needs the opposite.
When you create a VPC you give it a CIDR block (Classless Inter-Domain Routing, a way to write an IP range as an address plus a size).
Your project uses 10.0.0.0/16.
/16means the first 16 bits are fixed (10.0) and the last 16 are yours, so 2\^16 = 65,536 addresses, from10.0.0.0to10.0.255.255.- You then cut it into subnets. A
/24like10.0.1.0/24holds 256 addresses, and you can fit 256 of those in the/16.
π Want more? Reserved addresses and size limits
- AWS keeps 5 addresses in every subnet: the network address (
.0), the VPC router (.1), the DNS resolver (.2), one reserved for later (.3) and the last one (.255in a/24). So a/24gives you 251 usable IPs. - A VPC can be anywhere from
/16(largest) to/28(16 addresses, 11 usable).
Puff's wizard creates 10.0.0.0/20 and 10.0.16.0/20 as public subnets, and 10.0.128.0/20 and 10.0.144.0/20 as private ones.
Each /20 holds 4,096 addresses, far more than Puff needs, and private IPs cost nothing.
The range matters even on a private network, because one day you'll connect it to something else (a second VPC, an office, a VPN).
A packet for 10.0.3.7 must have one home only, so AWS refuses to connect overlapping VPCs.
Fixing that later means rebuilding the network and moving every server in it.
π£οΈ Public vs private subnets
This decides whether a server can be reached from the internet, so it's the idea to get right today.
A subnet is a slice of the VPC's range that lives in exactly one AZ, like a street in the neighborhood.
You create them in pairs across AZs, so losing one data center doesn't take your tenant down.
There's no "public" checkbox on a subnet, its route table decides.
- A subnet is public when its route table (the list of rules that says where traffic goes next) sends
0.0.0.0/0, meaning "everything outside the VPC", to an Internet Gateway. - It's private when it doesn't.
Every route table also has a local route for the VPC's own range, so subnets in the same VPC can always reach each other (firewalls then decide if they're allowed to).
| Destination | Public route table | Private route table |
|---|---|---|
10.0.0.0/16 | local | local |
0.0.0.0/0 | igw-... (Internet Gateway) | nat-... (NAT gateway) |
S3 prefix list pl-... | vpce-... (endpoint) | vpce-... (endpoint) |
To find out whether a subnet is public, don't trust its name, open its route table and look at where 0.0.0.0/0 goes.
To be reachable from the internet, an instance also needs a public IP.
Your app will have neither a public IP nor a public subnet.
π Want more? Which route wins
When several routes match a destination, the most specific one wins. A packet for 10.0.3.7 matches both 10.0.0.0/16 and 0.0.0.0/0, and the /16 is more specific, so it stays local. That's also why an S3 endpoint's prefix list beats the 0.0.0.0/0 route for S3 traffic.
πͺ Internet Gateway and NAT gateway: the two doors
Your private API still needs to download Docker, the python:3.12-slim image and OS updates.
You need a way out that isn't a way in.
The Internet Gateway (IGW) connects your VPC to the internet.
Traffic goes both ways and can start from either side, there's one per VPC and it's free.
The NAT gateway (Network Address Translation) is a one-way exit.
It sits in a public subnet with an Elastic IP (a fixed public address you keep).
- Private subnets send
0.0.0.0/0to it. - It swaps the sender's address for its own public IP, and it only lets replies come back.
- Nothing on the internet can ever start a conversation with your private API through it.
The map uses plain names, the zones are AZs, the one-way exit is the NAT gateway, the load balancer is alb, the app is app and the database is db.
Your network spans two AZs, with one public and one private subnet in each.
The public subnets hold the ALB and the NAT gateway, the private subnets hold the app and the database.
The public route table sends everything leaving the VPC to the Internet Gateway, the private one sends it to the NAT gateway.
A visitor comes in through the Internet Gateway, then the ALB, then the API, and the API talks to the database.
A download goes the other way, from the API to the NAT gateway, then the Internet Gateway, then Docker Hub, and the reply comes back the same way.
A stranger gets stopped, because the API has no public IP and its subnet has no way in from the internet.
A NAT gateway charges per hour and per GB.
In us-east-1 that's about $0.045 per hour (roughly \$32 a month doing nothing) plus $0.045 per GB processed.
Your platform still takes one: on real AWS, without it your private containers couldn't pull images or call outside APIs.
You could put the API in a public subnet "with a strict security group" instead, but then one wrong rule exposes it, while a private subnet has no way in whatever the rules say.
So we pay for one NAT.
π Want more? One NAT or one per AZ, and how bills grow
- With a single NAT, if its AZ goes down, the private instances in the other AZ lose outbound access. Your project can live with that. A system that depends on outbound calls would pay for one NAT per AZ.
- The per-GB fee comes on top of normal data transfer. A team that runs one NAT per AZ in three AZs, in dev, staging and prod, pays for nine gateways before a single byte moves.
- An app that pulls large images or backs up to S3 through the NAT can turn the per-GB line into the biggest network item on the invoice.
π‘οΈ Security groups vs network ACLs
Routes decide where traffic can go, firewalls decide who's allowed through, and you'll touch them every day you work on a cloud.
A security group is a firewall on one resource (an instance, a load balancer, a database), like a bouncer with a guest list.
It only has "allow" rules, and it remembers who it let in, so replies go out automatically, that memory is what "stateful" means.
A network ACL (Access Control List) is a firewall on a whole subnet.
It reads numbered rules from the lowest up, first match wins, and it can allow or deny.
It has no memory ("stateless"), so replies need their own rule.
| Security group | Network ACL | |
|---|---|---|
| Applies to | One server's network card | A whole subnet |
| Memory | Remembers, replies get out automatically | No memory, replies need their own rule |
| Rules | Allow only | Allow and deny |
| How it reads rules | All at once, any match lets you in | In number order, first match wins |
| Default | New group: nothing in, everything out | Default NACL: everything allowed both ways |
| When you use it | Every day | Rarely, as an extra layer or to block one IP range |
Security groups do 95% of the work.
Leave NACLs at their default unless you have a precise reason (like blocking an abusive IP range), because stateless rules are easy to get wrong and painful to debug.
π Want more? Why stateless rules bite
If a NACL allows inbound 443, you also need an outbound rule for the ephemeral ports (1024 to 65535) the replies go back to. Forget it and the request arrives, the server answers, and the answer gets dropped at the subnet edge. From the outside it looks like a timeout, which is the hardest kind of bug to read.
Your chain:
| Security group | Inbound rule | Source |
|---|---|---|
alb-sg | TCP 80 | 0.0.0.0/0 |
app-sg | TCP 8080 | alb-sg |
db-sg | TCP 5432 | app-sg |
Read it from the bottom: the database only talks to the API, the API only to the load balancer, and only the load balancer talks to the world.
A stranger can only reach port 80 on the ALB.
If your first connection test fails later today, one link of this chain is missing, and finding it is the best way to learn this table.
π³οΈ VPC endpoints: a private path to AWS
Reaching S3 from a private server through the NAT costs money per GB, for a service sitting in the same Region.
A VPC endpoint is a private path from your VPC straight to an AWS service.
A gateway endpoint exists for S3 and DynamoDB only, it's a line in the route table (destination: the service's prefix list, target: vpce-...), it's free, and S3 traffic stops touching the NAT.
A private instance that copies 500 GB of backups to S3 each month through the NAT pays about \$22 in NAT processing for nothing.
Add the S3 gateway endpoint to the private route table and that line drops to zero, with no change to the application.
On real AWS you'd add the free S3 gateway endpoint to your VPC.
π Want more? Interface endpoints
For most other AWS services there's the interface endpoint (also called PrivateLink). It puts a network card with a private IP in your subnet. It's billed per hour per AZ and per GB, so you add one when the traffic, or a rule that says "never leave the private network", justifies it.
π Connecting networks
One VPC isn't enough forever, a second app arrives, or a company keeps its payroll database in its own building.
Four tools link networks, from small to big.
- VPC peering: a private connection between two VPCs. Free to set up, routes on both sides, and it doesn't chain (A peers with B, B with C, A still can't reach C).
- Transit Gateway: a central hub every VPC and VPN connects to once, like a roundabout.
- Site-to-Site VPN (Virtual Private Network): an encrypted tunnel over the internet to an office router. Up in an hour.
- Direct Connect: a private physical cable from a data center into AWS. Takes weeks to set up.
| Situation | Use | Why |
|---|---|---|
| Two or three VPCs need to talk | VPC peering | Simple and free, the "doesn't chain" limit doesn't hurt yet |
| Ten VPCs across accounts, plus an office | Transit Gateway | One connection per network instead of a web of links, one place for routing |
| Office needs access to AWS this week | Site-to-Site VPN | Fast to set up, encrypted, cheap |
| A data center sends terabytes to AWS every day, or needs steady speed | Direct Connect (with VPN as backup) | Predictable speed and cheaper data transfer at volume |
None of these works with overlapping ranges, so the IP plan is what keeps all four options open.
π Want more? The four tools in detail
- Peering works across accounts and Regions, you pay only for data, and it refuses overlapping ranges.
- Ten VPCs that all need to reach each other with peering means 45 separate peering connections. A Transit Gateway replaces that web with ten attachments, and its route tables decide who talks to whom. It's billed per attachment per hour and per GB.
- A VPN is cheap, but its speed and latency depend on the internet. Direct Connect gives stable latency and cheaper data at volume for a fixed monthly fee, and big companies often keep a VPN as its backup.
βοΈ Load balancers: the only public door
Once the API is private, something public has to receive visitors and pass them to it.
A load balancer receives the traffic, spreads it across your instances and skips the unhealthy ones, like a receptionist sending visitors to free desks.
Later, it lets you add containers behind one stable address.
It has three parts:
- Listeners: the port it accepts, for example HTTP 80.
- Target groups: the instances it forwards to, with a health check.
- Rules: they connect the two.
π Want more? ALB or NLB
| Application Load Balancer (ALB) | Network Load Balancer (NLB) | |
|---|---|---|
| What it understands | Web traffic (HTTP and HTTPS), layer 7 | Raw connections (TCP, UDP, TLS), layer 4 |
| How it routes | By path, host name, header, query string | By port only |
| Address | A DNS name, the IPs behind it change | A DNS name with one fixed IP per AZ |
| Extras | Handles HTTPS certificates, redirects, login | Very fast, keeps the visitor's real IP |
| Pick it for | Web apps and APIs (your app) | Non-web protocols, fixed IPs a partner can allowlist, huge traffic |
AWS has two main kinds, the ALB (Application Load Balancer) for web traffic and the NLB (Network Load Balancer) for raw connections.
For anything that speaks HTTP, start with an ALB.
Use an NLB when you need a static IP or a protocol the ALB doesn't speak.
An ALB must sit in public subnets in at least two AZs to receive internet traffic, and its targets can sit in private subnets.
π§ͺ Project M2: a private network with one front door
You build version 2 of your platform next to the naive one, in its own VPC, then move your app and database in and delete the old deploy. Teams migrate exactly like that in real life: build the new house, move in, then demolish the old one.
Before you start: M1 done, Floci running, source env.sh and export GROUP=gXX.
Keep copying every command that works into infra/deploy.sh.
Get the new checks first (they now include M2 and the intruder test):
# Update the check scripts from the course repo
curl -fsSL https://raw.githubusercontent.com/ricoleabricot/puff/main/starter/checks/check.sh -o checks/check.sh
curl -fsSL https://raw.githubusercontent.com/ricoleabricot/puff/main/starter/checks/intruder.sh -o checks/intruder.sh
chmod +x checks/*.sh# Update the check scripts from the course repo
curl.exe -fsSL https://raw.githubusercontent.com/ricoleabricot/puff/main/starter/checks/check.ps1 -o checks/check.ps1
curl.exe -fsSL https://raw.githubusercontent.com/ricoleabricot/puff/main/starter/checks/intruder.ps1 -o checks/intruder.ps1Step 1: the VPC and its four subnets. Two public (for the load balancer), two private (for the app and the database), in two Availability Zones.
# Your private network, 65,536 addresses, tagged with your group name
VPC2=$(aws ec2 create-vpc --cidr-block 10.0.0.0/16 \
--tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=$GROUP-vpc}]" \
--query Vpc.VpcId --output text)
# Two public subnets, one per AZ
PUB_A=$(aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.1.0/24 \
--availability-zone us-east-1a --query Subnet.SubnetId --output text)
PUB_B=$(aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.2.0/24 \
--availability-zone us-east-1b --query Subnet.SubnetId --output text)
# Two private subnets, one per AZ
PRIV_A=$(aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.11.0/24 \
--availability-zone us-east-1a --query Subnet.SubnetId --output text)
PRIV_B=$(aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.12.0/24 \
--availability-zone us-east-1b --query Subnet.SubnetId --output text)# Your private network, 65,536 addresses, tagged with your group name
$VPC2 = aws ec2 create-vpc --cidr-block 10.0.0.0/16 `
--tag-specifications "ResourceType=vpc,Tags=[{Key=Name,Value=$env:GROUP-vpc}]" `
--query Vpc.VpcId --output text
# Two public subnets, one per AZ
$PUB_A = aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.1.0/24 `
--availability-zone us-east-1a --query Subnet.SubnetId --output text
$PUB_B = aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.2.0/24 `
--availability-zone us-east-1b --query Subnet.SubnetId --output text
# Two private subnets, one per AZ
$PRIV_A = aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.11.0/24 `
--availability-zone us-east-1a --query Subnet.SubnetId --output text
$PRIV_B = aws ec2 create-subnet --vpc-id $VPC2 --cidr-block 10.0.12.0/24 `
--availability-zone us-east-1b --query Subnet.SubnetId --output textStep 2: the two doors. What makes a subnet public or private is only its route table.
# The internet gateway: the two-way door, attached to the VPC
IGW=$(aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text)
aws ec2 attach-internet-gateway --internet-gateway-id $IGW --vpc-id $VPC2
# Public route table: "everything else goes to the internet gateway"
PUB_RT=$(aws ec2 create-route-table --vpc-id $VPC2 --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PUB_RT --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
# Attach it to the two public subnets
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_A
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_B
# The NAT gateway: the one-way exit, it lives in a public subnet with a fixed public IP
EIP=$(aws ec2 allocate-address --domain vpc --query AllocationId --output text)
NAT=$(aws ec2 create-nat-gateway --subnet-id $PUB_A --allocation-id $EIP \
--query NatGateway.NatGatewayId --output text)
# Private route table: "everything else goes out through the NAT", nothing comes in
PRIV_RT=$(aws ec2 create-route-table --vpc-id $VPC2 --query RouteTable.RouteTableId --output text)
aws ec2 create-route --route-table-id $PRIV_RT --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT
# Attach it to the two private subnets
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $PRIV_A
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $PRIV_B# The internet gateway: the two-way door, attached to the VPC
$IGW = aws ec2 create-internet-gateway --query InternetGateway.InternetGatewayId --output text
aws ec2 attach-internet-gateway --internet-gateway-id $IGW --vpc-id $VPC2
# Public route table: "everything else goes to the internet gateway"
$PUB_RT = aws ec2 create-route-table --vpc-id $VPC2 --query RouteTable.RouteTableId --output text
aws ec2 create-route --route-table-id $PUB_RT --destination-cidr-block 0.0.0.0/0 --gateway-id $IGW
# Attach it to the two public subnets
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_A
aws ec2 associate-route-table --route-table-id $PUB_RT --subnet-id $PUB_B
# The NAT gateway: the one-way exit, it lives in a public subnet with a fixed public IP
$EIP = aws ec2 allocate-address --domain vpc --query AllocationId --output text
$NAT = aws ec2 create-nat-gateway --subnet-id $PUB_A --allocation-id $EIP `
--query NatGateway.NatGatewayId --output text
# Private route table: "everything else goes out through the NAT", nothing comes in
$PRIV_RT = aws ec2 create-route-table --vpc-id $VPC2 --query RouteTable.RouteTableId --output text
aws ec2 create-route --route-table-id $PRIV_RT --destination-cidr-block 0.0.0.0/0 --nat-gateway-id $NAT
# Attach it to the two private subnets
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $PRIV_A
aws ec2 associate-route-table --route-table-id $PRIV_RT --subnet-id $PRIV_BStep 3: the security group chain. Each layer only accepts the layer in front of it.
# Three firewalls, one per layer
ALB_SG2=$(aws ec2 create-security-group --group-name $GROUP-alb-sg2 \
--description "ALB, public" --vpc-id $VPC2 --query GroupId --output text)
APP_SG2=$(aws ec2 create-security-group --group-name $GROUP-app-sg2 \
--description "app, only from the ALB" --vpc-id $VPC2 --query GroupId --output text)
DB_SG2=$(aws ec2 create-security-group --group-name $GROUP-db-sg2 \
--description "db, only from the app" --vpc-id $VPC2 --query GroupId --output text)
# The ALB accepts the whole internet on port 80
aws ec2 authorize-security-group-ingress --group-id $ALB_SG2 --protocol tcp --port 80 --cidr 0.0.0.0/0
# The app accepts port 80 from the load balancer's network only (see the note below)
aws ec2 authorize-security-group-ingress --group-id $APP_SG2 --protocol tcp --port 80 --cidr 172.17.0.0/16
# The database accepts 5432 from the app's security group only
aws ec2 authorize-security-group-ingress --group-id $DB_SG2 --protocol tcp --port 5432 --source-group $APP_SG2# Three firewalls, one per layer
$ALB_SG2 = aws ec2 create-security-group --group-name "$env:GROUP-alb-sg2" `
--description "ALB, public" --vpc-id $VPC2 --query GroupId --output text
$APP_SG2 = aws ec2 create-security-group --group-name "$env:GROUP-app-sg2" `
--description "app, only from the ALB" --vpc-id $VPC2 --query GroupId --output text
$DB_SG2 = aws ec2 create-security-group --group-name "$env:GROUP-db-sg2" `
--description "db, only from the app" --vpc-id $VPC2 --query GroupId --output text
# The ALB accepts the whole internet on port 80
aws ec2 authorize-security-group-ingress --group-id $ALB_SG2 --protocol tcp --port 80 --cidr 0.0.0.0/0
# The app accepts port 80 from the load balancer's network only (see the note below)
aws ec2 authorize-security-group-ingress --group-id $APP_SG2 --protocol tcp --port 80 --cidr 172.17.0.0/16
# The database accepts 5432 from the app's security group only
aws ec2 authorize-security-group-ingress --group-id $DB_SG2 --protocol tcp --port 5432 --source-group $APP_SG2On real AWS, the app's rule would say --source-group $ALB_SG2: "anything wearing the ALB's badge".
On Floci the load balancer isn't a container, it lives inside Floci itself, on your machine's Docker network 172.17.0.0/16, so you allow that range instead. Step 7 shows it still keeps an intruder out.
Step 4: the new front door, in the public subnets.
# The ALB, in the two public subnets
ALB2=$(aws elbv2 create-load-balancer --name $GROUP-alb2 \
--subnets $PUB_A $PUB_B --security-groups $ALB_SG2 \
--query 'LoadBalancers[0].LoadBalancerArn' --output text)
# Its target group, in the new VPC
TG2=$(aws elbv2 create-target-group --name $GROUP-tg2 \
--protocol HTTP --port 80 --target-type ip --vpc-id $VPC2 --health-check-path / \
--query 'TargetGroups[0].TargetGroupArn' --output text)
# Its listener
aws elbv2 create-listener --load-balancer-arn $ALB2 --protocol HTTP --port 80 \
--default-actions Type=forward,TargetGroupArn=$TG2# The ALB, in the two public subnets
$ALB2 = aws elbv2 create-load-balancer --name "$env:GROUP-alb2" `
--subnets $PUB_A $PUB_B --security-groups $ALB_SG2 `
--query 'LoadBalancers[0].LoadBalancerArn' --output text
# Its target group, in the new VPC
$TG2 = aws elbv2 create-target-group --name "$env:GROUP-tg2" `
--protocol HTTP --port 80 --target-type ip --vpc-id $VPC2 --health-check-path / `
--query 'TargetGroups[0].TargetGroupArn' --output text
# Its listener
aws elbv2 create-listener --load-balancer-arn $ALB2 --protocol HTTP --port 80 `
--default-actions "Type=forward,TargetGroupArn=$TG2"Step 5: your app moves in, private. Same task definition as M1, new service in the private subnets, no public IP.
# The v2 service: private subnets, app firewall, no public IP
aws ecs create-service --cluster $GROUP --service-name $GROUP-app2 \
--task-definition $GROUP-app --desired-count 1 --launch-type FARGATE \
--network-configuration "awsvpcConfiguration={subnets=[$PRIV_A,$PRIV_B],securityGroups=[$APP_SG2],assignPublicIp=DISABLED}" \
--load-balancers "targetGroupArn=$TG2,containerName=app,containerPort=80"
# Your new URL (wait ~30 s)
echo "http://$(aws elbv2 describe-load-balancers --names $GROUP-alb2 \
--query 'LoadBalancers[0].DNSName' --output text)/"# The v2 service: private subnets, app firewall, no public IP
aws ecs create-service --cluster $env:GROUP --service-name "$env:GROUP-app2" `
--task-definition "$env:GROUP-app" --desired-count 1 --launch-type FARGATE `
--network-configuration "awsvpcConfiguration={subnets=[$PRIV_A,$PRIV_B],securityGroups=[$APP_SG2],assignPublicIp=DISABLED}" `
--load-balancers "targetGroupArn=$TG2,containerName=app,containerPort=80"
# Your new URL (wait ~30 s)
$DNS2 = aws elbv2 describe-load-balancers --names "$env:GROUP-alb2" `
--query 'LoadBalancers[0].DNSName' --output text
"http://$DNS2/"Step 6: the database moves in, private. A database can't change VPC, you create a new one.
# Tell RDS which subnets it may use: the private ones
aws rds create-db-subnet-group --db-subnet-group-name $GROUP-db-subnets \
--db-subnet-group-description "private subnets" --subnet-ids $PRIV_A $PRIV_B
# The v2 database: private subnets, database firewall, not public
aws rds create-db-instance --db-instance-identifier $GROUP-db2 \
--engine postgres --db-instance-class db.t3.micro --allocated-storage 20 \
--master-username app --master-user-password 'ChangeMe-123' \
--db-subnet-group-name $GROUP-db-subnets --vpc-security-group-ids $DB_SG2 \
--no-publicly-accessible# Tell RDS which subnets it may use: the private ones
aws rds create-db-subnet-group --db-subnet-group-name "$env:GROUP-db-subnets" `
--db-subnet-group-description "private subnets" --subnet-ids $PRIV_A $PRIV_B
# The v2 database: private subnets, database firewall, not public
aws rds create-db-instance --db-instance-identifier "$env:GROUP-db2" `
--engine postgres --db-instance-class db.t3.micro --allocated-storage 20 `
--master-username app --master-user-password 'ChangeMe-123' `
--db-subnet-group-name "$env:GROUP-db-subnets" --vpc-security-group-ids $DB_SG2 `
--no-publicly-accessibleThe password is still in clear. Course 4 fixes it.
Step 7: prove it, the intruder test. A throwaway container inside your private subnets, in a security group nobody trusts, tries to open your app directly.
# Expect: app BLOCKED
./checks/intruder.sh# Expect: app BLOCKED
.\checks\intruder.ps1The database line says REACHED, and that's a Floci limit worth understanding: your database sits behind Floci's own proxy, which doesn't apply security groups. On real AWS, $DB_SG2 would block the intruder too. Your rule is right, the emulator just can't prove it.
Then the break drill, inside your group: one member breaks something while the others look away, the others find it and fix it with the CLI only. Pick from:
# Break A: the listener answers 503 to everyone
aws elbv2 modify-listener --listener-arn $(aws elbv2 describe-listeners --load-balancer-arn $ALB2 --query 'Listeners[0].ListenerArn' --output text) \
--default-actions '[{"Type":"fixed-response","FixedResponseConfig":{"StatusCode":"503","ContentType":"text/plain","MessageBody":"down"}}]'
# Break B: the app stops trusting the load balancer, then restarts
aws ec2 revoke-security-group-ingress --group-id $APP_SG2 --protocol tcp --port 80 --cidr 172.17.0.0/16
aws ecs update-service --cluster $GROUP --service $GROUP-app2 --force-new-deployment
# Break C: zero containers
aws ecs update-service --cluster $GROUP --service $GROUP-app2 --desired-count 0# Break A: the listener answers 503 to everyone (the action is written to a file)
'[{"Type": "fixed-response", "FixedResponseConfig": {"StatusCode": "503", "ContentType": "text/plain", "MessageBody": "down"}}]' |
Set-Content break-a.json
$LISTENER = aws elbv2 describe-listeners --load-balancer-arn $ALB2 --query 'Listeners[0].ListenerArn' --output text
aws elbv2 modify-listener --listener-arn $LISTENER --default-actions file://break-a.json
# Break B: the app stops trusting the load balancer, then restarts
aws ec2 revoke-security-group-ingress --group-id $APP_SG2 --protocol tcp --port 80 --cidr 172.17.0.0/16
aws ecs update-service --cluster $env:GROUP --service "$env:GROUP-app2" --force-new-deployment
# Break C: zero containers
aws ecs update-service --cluster $env:GROUP --service "$env:GROUP-app2" --desired-count 0Break B needs the restart because security groups, on Floci like on AWS, let connections that are already open finish: only new connections get blocked.
Step 8: tear down the naive deploy. Once v2 answers.
# Stop and delete the M1 service
aws ecs update-service --cluster $GROUP --service $GROUP-app --desired-count 0
aws ecs delete-service --cluster $GROUP --service $GROUP-app
# Delete the M1 load balancer and its target group
aws elbv2 delete-load-balancer --load-balancer-arn $(aws elbv2 describe-load-balancers \
--names $GROUP-alb --query 'LoadBalancers[0].LoadBalancerArn' --output text)
aws elbv2 delete-target-group --target-group-arn $(aws elbv2 describe-target-groups \
--names $GROUP-tg --query 'TargetGroups[0].TargetGroupArn' --output text)
# Delete the public database
aws rds delete-db-instance --db-instance-identifier $GROUP-db --skip-final-snapshot# Stop and delete the M1 service
aws ecs update-service --cluster $env:GROUP --service "$env:GROUP-app" --desired-count 0
aws ecs delete-service --cluster $env:GROUP --service "$env:GROUP-app"
# Delete the M1 load balancer and its target group
$OLD_ALB = aws elbv2 describe-load-balancers --names "$env:GROUP-alb" `
--query 'LoadBalancers[0].LoadBalancerArn' --output text
aws elbv2 delete-load-balancer --load-balancer-arn $OLD_ALB
$OLD_TG = aws elbv2 describe-target-groups --names "$env:GROUP-tg" `
--query 'TargetGroups[0].TargetGroupArn' --output text
aws elbv2 delete-target-group --target-group-arn $OLD_TG
# Delete the public database
aws rds delete-db-instance --db-instance-identifier "$env:GROUP-db" --skip-final-snapshotStep 9: check, commit, push.
# Run the milestone check, keep its output in NOTES.md
./checks/check.sh M2 | tee -a NOTES.md
# Share it with your group
git add -A
git commit -m "M2"
git push# Run the milestone check, keep its output in NOTES.md
$r = .\checks\check.ps1 M2
$r; $r | Add-Content NOTES.md
# Share it with your group
git add -A
git commit -m "M2"
git pushπ§― If it doesn't work
- The new ALB never answers: check the app's rule allows
172.17.0.0/16on port 80, and if you changed the rule, restart the service with--force-new-deployment. InvalidSubnetorInvalidVpcID: a variable is empty (new terminal). Rerun the lines that set it, or read the IDs back withaws ec2 describe-subnets --filters Name=vpc-id,Values=$VPC2.intruder.shsays no VPC: your VPC isn't taggedName=$GROUP-vpc, add the tag withaws ec2 create-tags.intruder.shsays the app is REACHED: your app's rule is wider than it should be (0.0.0.0/0left over?), tighten it and restart the service.- Deleting the old ALB fails: delete its listener first, or check you used
$GROUP-alb, not$GROUP-alb2.
πΈ On real AWS
The same commands build a real VPC, and there the routes matter: a container in a private subnet without the NAT route can't pull its image, and you'd see the task stuck in PENDING.
The console's "VPC and more" wizard does steps 1 and 2 in one screen and draws the result as a map: subnets, route tables, gateways and their links. Try it once on a real account, it's the best picture of what you just typed.
Real NAT gateways bill by the hour, about 33 dollars a month each before traffic, which is why teams share them or use VPC endpoints.
π² Today's quiz
π€ Think about it
- Your private app can reach the internet but not your database, which is in the same VPC. Name the three places you'd look, in order, and what you'd expect to find in each.
- A classmate says "we could skip the NAT gateway and give the API a public IP, with a security group that only allows the ALB". Is that less safe than what you built? What happens the day someone edits that rule by mistake?
- A second team builds an analytics VPC that needs to read your database, and an office needs access to both. Which tools would you pick, and what choice from today could make this impossible?
- Your NAT gateway line on the bill doubled this month. Name two kinds of traffic that could cause it, and how you'd take each one off the NAT path.
- Why does the ALB need subnets in two AZs, while your single app container lives in one? What's the weak point of what you built today, and which course fixes it?
π‘ Hints
- Route table (is the destination
local?), then the security group on each end, then the NACL. Same VPC means routing is already fine. - Think about what stops traffic when a rule is wrong: in a private subnet there's no route in at all, so a bad rule changes nothing.
- Peering or Transit Gateway for VPC to VPC, VPN or Direct Connect for the office. Overlapping CIDRs kill every option.
- S3 traffic (gateway endpoint), traffic to other AWS services (interface endpoints), image pulls (cache or pull less often).
- The load balancer is designed to survive one AZ failing, your one instance isn't. Course 5 adds an Auto Scaling group across both AZs.