Rebuilt from code

Course 6 · Your platform becomes two templates and one command, then you defend it

Today

When What Where
0:00Infrastructure as code, CloudFormation, change sets, drift, TerraformLecture
0:20CI/CD and deployment strategiesLecture
0:35FinOps, DR, Well-ArchitectedLecture
0:45M5: templates, idea inProject
1:00M5: wipe, rebuild in one command, prove itProject
1:30M5: a change set, a driftProject
2:00Freeze, then defensesDefense

Nobody knows what your platform looks like anymore

Port 22 opened "for five minutes", a database resized from the console, and the VPC only lives in your memory of course 3.

Infrastructure as code

The recipe, not the cake

Lose the cake, you bake another. Lose the recipe, you're guessing.

Four things get better

  • You see driftThe tool tells you when reality differs from the code, before something breaks
  • Changes get reviewedA firewall change becomes a pull request a classmate reads, with a history of who changed what
  • Copies are identicalTest and the real thing come from the same template, so "it worked in test" means something
  • Recovery gets simpleLose a region, redeploy the files somewhere else. No rebuilding from screenshots at 3:00

Write the steps, or write the end result

Imperative

A bash script: create-vpc, then create-subnet, then create-nat-gateway. Crashes halfway? You find where by hand

Declarative

"There is a VPC 10.0.0.0/16 with four subnets." The tool works out the steps itself

CloudFormation and Terraform are both declarative.

You run the bash script twice. How many VPCs?

And if you apply the same template twice?

A template has three sections

What it holds Example
ParametersInputs given at deploy timeLogin, NamePrefix
ResourcesThe only required one: logical ID, Type, PropertiesAssetsBucket, an AWS::S3::Bucket
OutputsValues shown once it's builtBucketArn, VpcId

!Ref gives the ID, !GetAtt one attribute. Every !Ref is also a "build that first".

Three stack features you'll use for real

  • Change setsA preview of what gets added, modified or removed, before anything happens
  • Automatic rollbackA deploy fails halfway, everything goes back. No half a platform
  • Drift detectionCompares each resource with the template and flags anything changed by hand

Replacement means a new, empty database

Read that column in every change set. And once a resource belongs to a stack, change it through the stack only.

Game time

Read the change set

Open it on the course page

🏭 In a data center

The old way

A weekly change board, a CMDB with every server, and drift the day someone fixes a server by hand

With a stack

A change set asks the board's question to a machine. The stack and its tags are the inventory

Drift detection tells you when someone went around it.

CloudFormation or Terraform

CloudFormation

AWS runs it for you, YAML, speaks AWS only. Zero setup

Terraform

You run it, HCL, speaks almost every cloud and online tool. plan, then apply

terraform plan is the same idea as a change set.

Side by side

CloudFormation Terraform
Remembers what it builtInside AWS, nothing to manageA state file you store and lock yourself
Fails halfwayRolls back by itselfStops there, you fix and re-apply
DriftDetection when you askEvery plan shows it
ReuseNested stacks, modules, Quick StartsA huge public library of modules
LicenceFree, part of AWSBSL since 2023, OpenTofu is the open-source fork

Your platform only ever touches AWS. Which one would you pick?

Terraform

At Alan: AWS, monitoring, DNS, SaaS tools and several accounts, one tool, one workflow.

What's in your two templates

VPC

10.0.0.0/16, 4 subnets over 2 AZs

Gateways

Internet gateway, one NAT gateway

Routing

Route tables, S3 gateway endpoint

Security groups

ALB, app, DB, chained

app.yaml adds the secret, the DB, logs, roles, the ALB and ECS. A new tenant is one deploy with another NamePrefix.

Hands-on

Project M5: your platform as code, rebuilt from zero

✅ After ./wipe.sh, ./infra/rebuild.sh brings it all back, check.sh M2 to M5 all PASS

Steps on the course page

From a commit to production

  • BuildTurns code into one artifact: a Docker image tagged with the commit SHA
  • TestRuns that exact image: unit tests, then the container against a real PostgreSQL
  • DeployStaging first, then production, often with a human approval in between

Build once, promote the same image everywhere

Rebuild for production, and production runs something you never tested.

Three ways to swap in the new version

Rolling Blue/green Canary
HowReplace servers a few at a timeA full second fleet, then switch all traffic10% to the new version, watch, then 100%
On your platformECS swaps 1 container at a timeListener flips from a blue to a green target groupListener splits 90/10, then 0/100
Going backRoll the old version again (slow)Point back at blue (seconds)New share back to 0 (seconds)
Extra costNoneDouble the servers for a whileA little
Watch outOld and new run together on one databaseBlue and green share one databaseWithout good alarms you miss the 10% failing

There's only one database, and that's what makes every strategy hard.

Game time

Blue/green and canary

Open it on the course page

Game time

Ship a buggy release

Open it on the course page

The next release renames the text column to body. Why does that break rolling and blue/green?

FinOps

Whoever picks the server size is spending money

When your group defends its platform, "what does it cost?" will be one of my first questions.

The usual cost surprises

  • NAT gateway dataPer hour and per GB, that's why S3 traffic takes the free gateway endpoint
  • Idle resourcesA NAT, a load balancer or a database costs the same with zero visitors
  • Oversized serversAn m5.2xlarge picked "to be safe", at 4% CPU for a year
  • Data going outTo the internet, across AZs and regions. Coming in is free
  • Public IPv4Every address, the NAT's and the ALB's, billed per hour

What your platform costs per month

Assumption Per month
EC2 t3.micro × 2730 h each~$15
RDS db.t3.micro, single-AZ730 h + 20 GB~$16
Application Load Balancer730 h + ~1 LCU~$22
NAT gateway730 h + a few GB~$33
Public IPv4 × 3NAT EIP + 2 ALB addresses~$11
Total, with disks, S3 and alarmsus-east-1, low traffic~$100

NAT, ALB and public IPs are two thirds of the bill. The two API servers, a sixth.

Game time

Your platform's monthly bill

Open it on the course page

The day a whole region goes down

Backup region runs RPO RTO Cost
Backup and restoreNothing, only backupsHoursHours to a day$
Pilot lightData copied, database on, app offMinutesTens of minutes to hours$$
Warm standbyA small complete copySeconds to minutesMinutes$$$
Multi-siteThe full stack, both placesNear zeroNear zero$$$$

With templates, "restore" means deploying them in another region and loading the latest backup.

Game time

Pick a disaster plan

Open it on the course page

How your platform scores

Pillar What you did
Operational excellenceTwo templates, one-command rebuild, change sets, an alarm that pages you
SecurityPrivate subnets, chained SGs, Secrets Manager, execution role, deployer role
Reliability2 AZs, 2+ containers behind the ALB, the kill drill (gaps: one NAT, single-AZ RDS)
PerformanceCloudFront, your own autoscaler, S3 gateway endpoint
CostSmall containers, db.t3.micro, Project tags, monthly estimate
SustainabilityScale in when idle, a wipe leaves nothing running

It doubles as the checklist for your project justification.

Defenses start

At 2:00, everything freezes

check.sh M5 in NOTES.md, pushed. Then nobody touches the platform again.

Pitch it like a small cloud provider

How it scales

How it stays available

How it recovers

What it costs

How it's secured

8 minutes per group, rolling from 2:10. The Project page has the format and the grading.

Game time

Today's quiz

Open it on the course page

A classmate fixes a bug in the console. Tomorrow a teammate deploys to the same stack. What happens?

Everything from today is on the course page.