
Rebuilt from code
Course 6 · Your platform becomes two templates and one command, then you defend it
Today
| When | What | Where |
|---|---|---|
| 0:00 | Infrastructure as code, CloudFormation, change sets, drift, Terraform | Lecture |
| 0:20 | CI/CD and deployment strategies | Lecture |
| 0:35 | FinOps, DR, Well-Architected | Lecture |
| 0:45 | M5: templates, idea in | Project |
| 1:00 | M5: wipe, rebuild in one command, prove it | Project |
| 1:30 | M5: a change set, a drift | Project |
| 2:00 | Freeze, then defenses | Defense |

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 | |
|---|---|---|
Parameters | Inputs given at deploy time | Login, NamePrefix |
Resources | The only required one: logical ID, Type, Properties | AssetsBucket, an AWS::S3::Bucket |
Outputs | Values shown once it's built | BucketArn, 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.
🏭 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 built | Inside AWS, nothing to manage | A state file you store and lock yourself |
| Fails halfway | Rolls back by itself | Stops there, you fix and re-apply |
| Drift | Detection when you ask | Every plan shows it |
| Reuse | Nested stacks, modules, Quick Starts | A huge public library of modules |
| Licence | Free, part of AWS | BSL since 2023, OpenTofu is the open-source fork |

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

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
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 | |
|---|---|---|---|
| How | Replace servers a few at a time | A full second fleet, then switch all traffic | 10% to the new version, watch, then 100% |
| On your platform | ECS swaps 1 container at a time | Listener flips from a blue to a green target group | Listener splits 90/10, then 0/100 |
| Going back | Roll the old version again (slow) | Point back at blue (seconds) | New share back to 0 (seconds) |
| Extra cost | None | Double the servers for a while | A little |
| Watch out | Old and new run together on one database | Blue and green share one database | Without good alarms you miss the 10% failing |
There's only one database, and that's what makes every strategy hard.

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 serversAnm5.2xlargepicked "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 × 2 | 730 h each | ~$15 |
RDS db.t3.micro, single-AZ | 730 h + 20 GB | ~$16 |
| Application Load Balancer | 730 h + ~1 LCU | ~$22 |
| NAT gateway | 730 h + a few GB | ~$33 |
| Public IPv4 × 3 | NAT EIP + 2 ALB addresses | ~$11 |
| Total, with disks, S3 and alarms | us-east-1, low traffic | ~$100 |
NAT, ALB and public IPs are two thirds of the bill. The two API servers, a sixth.
The day a whole region goes down
| Backup region runs | RPO | RTO | Cost | |
|---|---|---|---|---|
| Backup and restore | Nothing, only backups | Hours | Hours to a day | $ |
| Pilot light | Data copied, database on, app off | Minutes | Tens of minutes to hours | $$ |
| Warm standby | A small complete copy | Seconds to minutes | Minutes | $$$ |
| Multi-site | The full stack, both places | Near zero | Near zero | $$$$ |
With templates, "restore" means deploying them in another region and loading the latest backup.
How your platform scores
| Pillar | What you did |
|---|---|
| Operational excellence | Two templates, one-command rebuild, change sets, an alarm that pages you |
| Security | Private subnets, chained SGs, Secrets Manager, execution role, deployer role |
| Reliability | 2 AZs, 2+ containers behind the ALB, the kill drill (gaps: one NAT, single-AZ RDS) |
| Performance | CloudFront, your own autoscaler, S3 gateway endpoint |
| Cost | Small containers, db.t3.micro, Project tags, monthly estimate |
| Sustainability | Scale 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.

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.
