Deployment, MLOps, and Security › Containers and Cloud › Day 334
Hands-on lab — Day 334: Cloud Options and Free Tiers
- ← Back to the Day 334 lesson
- Open the hands-on files on GitHub — clone or download them from the public labs repository
- Local path in your clone:
labs/sections/deployment-mlops-and-security/day-334-cloud-options-and-free-tiers/
Commands
Setup
pip install -r requirements/requirements.txt Run
python3 examples/cost_demo.py Test
bash tests/run_tests.sh File tree
examples/cost_demo.py examples/hosting_cost.py expected-output/examples-run.txt expected-output/FIELDS.md expected-output/measured-values.txt expected-output/run-output.txt expected-output/starter-run.txt expected-output/test-output.txt expected-output/test-run.txt metadata.yml README.md requirements/requirements.txt security.md starter/hosting_cost.py tests/run_tests.sh tests/test_hosting_cost.py troubleshooting.md
Lab README
Day 334 Lab: Cloud Options and Free Tiers
Lesson
- Lesson title: Cloud Options and Free Tiers
- Day number: 334 of 365
- Lesson article: https://ai-roadmap-365.github.io/day-334-cloud-options-and-free-tiers
- Lab files: everything you need is in this directory — follow “How to run” below.
- Browse the course locally: from the repository root, this lab also appears in the course website at
/labs/day-334-cloud-options-and-free-tierswhen the site is running.
Purpose
Price four ways of hosting the same AI endpoint, honestly, including the parts the pricing page leaves out.
A free tier is a real offer with an edge, and the edge is rarely where the headline suggests. The tier in this lab advertises two million free requests — a hundred times the workload's traffic — and ends at 1.25x, because the egress allowance is a single gigabyte.
You will also find where the cheapest option at launch stops being the cheapest, which is lower than most people expect.
Learning objectives
- Distinguish always-on billing from per-request billing, and say what each costs at zero traffic.
- Price compute, egress and storage separately, deducting free allowances from each.
- Compute headroom per dimension and identify which allowance binds first.
- Find the traffic multiple at which two options change places.
- State what a cost model does not tell you.
Prerequisites
- Day 333, "Kubernetes Concepts".
- Comfortable with Python dataclasses and enums.
Supported operating systems
macOS, Linux and Windows (via WSL or PowerShell). Nothing is platform-specific.
Hardware requirements
Any machine running Python 3.10 or newer. The suite completes in hundredths of a second.
Required software
- Python 3.10 or newer.
pytest7 or newer.
No cloud account is required and nothing is spent. The rate cards are literals in the demo. If you do create an account to check the numbers, read security.md first — the security event of this day is the account, not the code.
Free and open-source options
Every dependency is free and open source: Python is PSF-licensed, pytest is MIT-licensed. For real deployments the genuinely free-tier-friendly options worth knowing are Cloudflare Workers, Fly.io, Railway, Render and Hugging Face Spaces; for self-hosting, k3s on a single small VM is often cheaper than any of them past the crossover.
Installation
python3 -m venv .venv
.venv/bin/pip install -r requirements/requirements.txt
Removing .venv fully reverses the installation.
File structure
starter/hosting_cost.py your work: fourteen stubbed tasks
examples/hosting_cost.py reference implementation
examples/cost_demo.py four options, three traffic levels
tests/test_hosting_cost.py grouped by what the model decides
tests/run_tests.sh suite entry point
expected-output/ real captured output and measured values
requirements/ pinned dependency
How to run
python3 examples/cost_demo.py # price four options at launch, 50x and 1000x
bash tests/run_tests.sh # run the suite
To work on the exercise, edit starter/hosting_cost.py, then copy it over the reference:
cp starter/hosting_cost.py examples/hosting_cost.py
bash tests/run_tests.sh
Keep a copy of the reference first if you want to compare.
What the commands do
python3 examples/cost_demo.py prices a serverless free tier, a paid serverless plan, a small always-on VM and a managed container service against the same workload at three traffic levels, reports which allowance binds each free tier first, and bisects for the traffic multiple at which the ranking changes.
bash tests/run_tests.sh runs pytest over twenty-four tests grouped by what the model decides: workload arithmetic, always-on billing, per-request billing, egress, headroom, selection, crossover and the free ceiling.
Expected output
--- where the free tier ends, and why ---
serverless-free-tier free until 1.30x (26,000/mo) binds on egress-gb at 1.25x
the headline allowance is rarely the one that runs out first:
egress-gb 1.25x current load
requests 100.00x current load
--- where the ranking changes ---
serverless-free-tier stops beating small-vm at 45.0x (899,599 requests/month)
The advertised constraint and the binding constraint differ by a factor of eighty.
Validation steps
bash tests/run_tests.shreports24 passed.- An always-on option must cost the same at zero traffic as at full traffic. If it scales with usage, the whole comparison collapses.
- A per-request option must cost exactly zero at zero traffic — that is what scale-to-zero means.
binding_constraintmust returnegress-gb, notrequests. Returning the advertised allowance meansheadroomis not taking a minimum.- Free allowances must clamp at zero. An option under its free tier must not earn you money.
Tests
Twenty-four tests in eight groups:
- workload — compute seconds, egress, and scaling that grows storage more slowly than traffic.
- always-on — bills by the hour regardless of traffic, including at zero.
- per-request — costs nothing at zero; free seconds and free requests deducted from their own quantities; excess billed.
- egress — free allowance honoured, only the excess billed, and egress dominating compute at scale.
- headroom — a multiple per dimension, infinity for unused ones, the smallest one binding.
- selection — cheapest chosen; choosing from nothing raises.
- crossover — the launch winner losing at scale;
Nonewhen prices match or one option always wins. - free ceiling —
Nonewhen never free, and located when it exists.
Cleanup
rm -rf __pycache__ .pytest_cache
rm -rf .venv
Both are safe to remove and fully reversible.
Troubleshooting
See troubleshooting.md.
Security notes
See security.md. In short: no account is used here, and the risk arrives when you create one — MFA before anything else, a budget alert before you deploy, and no long-lived key on a laptop.
Extension exercises
- Add capacity. Give each option a requests-per-second ceiling and refuse to price a workload it cannot serve. This removes the model's most misleading output — a single vCPU quoted for twenty million requests a month.
- Price your own workload. Take a service you actually run, measure its request rate and response size, and put your provider's current rate card in. The interesting result is your own binding constraint.
- Add committed-use discounts and spot pricing. Reserved instances change always-on economics substantially, and spot changes them again at the cost of being interrupted. Find where each moves the crossover.
Navigation
- Lesson
- Previous: Day 333 — Kubernetes Concepts
- Next: Day 335 — CI/CD with GitHub Actions
Expected output
FIELDS.md
# Reading the cost table
## A row
serverless-free-tier $ 0.00 compute $ 0.00 egress $ 0.00 storage $ 0.00 FREE
| Column | Meaning |
| --- | --- |
| total | The monthly bill for this workload. |
| compute | Hours for an always-on option; billed seconds and requests for a per-request one. |
| egress | Per GB leaving the provider, after any free allowance. |
| storage | Small, steady, and the line people forget entirely. |
| `FREE` | The total rounds to zero. Not the same as "inside the free tier" — see below. |
## The two billing models
| Model | Costs at zero traffic | Costs at high traffic |
| --- | --- | --- |
| `ALWAYS_ON` | The full hourly rate. A VM bills at 3am. | The same. Compute is flat. |
| `PER_REQUEST` | Nothing. This is scale-to-zero. | Grows with usage, without limit. |
That difference is the whole decision, and it is why the ranking changes with
traffic rather than being a fixed preference.
## Where the free tier ends, and why
serverless-free-tier free until 1.30x (26,000/mo) binds on egress-gb at 1.25x
the headline allowance is rarely the one that runs out first:
egress-gb 1.25x current load
storage-gb 2.50x current load
compute-seconds 22.50x current load
requests 100.00x current load
**This is the table to read.** The tier advertises two million free requests,
which at this workload is a hundred times current traffic. It ends at 1.25x,
because the egress allowance is a single gigabyte.
The advertised number and the binding number differ by a factor of eighty. No
pricing page prints the second one, and it is the only one that matters.
The small gap between "binds at 1.25x" and "free until 1.30x" is rounding: a
fraction of a gigabyte of egress costs less than a cent and shows as $0.00. The
binding constraint is the honest figure.
## Where the ranking changes
serverless-free-tier stops beating small-vm at 45.0x (899,599 requests/month)
at that point: serverless $8.66 vs vm $8.67
A crossover is the traffic multiple at which the cheaper option stops being
cheaper. It is computed by bisection, and `None` is a real answer meaning the
ordering never changes in the range — which tells you the choice is not
sensitive to growth.
Note how low the crossover is. Serverless is free at launch and a VM costs
$7.79; nine hundred thousand requests a month is a small service, and past it
the VM is cheaper for good.
## What this model does not do
It prices a workload. It does not check that an option can **serve** it.
At the 1000x row the model quotes `small-vm` at $90.77 for twenty million
requests a month on one vCPU, which that machine could not actually deliver.
Cost is one input to a hosting decision; capacity, reliability and blast radius
are others, and the extension exercises add the first of them.
examples-run.txt
--- launch: 20,000 requests/month ---
serverless-free-tier $ 0.00 compute $ 0.00 egress $ 0.00 storage $ 0.00 FREE
serverless-paid $ 0.34 compute $ 0.20 egress $ 0.10 storage $ 0.05
small-vm $ 7.79 compute $ 7.59 egress $ 0.00 storage $ 0.20
managed-container $ 25.02 compute $ 24.82 egress $ 0.00 storage $ 0.20
-> cheapest: serverless-free-tier
--- 50x: 1,000,000 requests/month ---
serverless-free-tier $ 10.14 compute $ 5.28 egress $ 4.68 storage $ 0.18
serverless-paid $ 15.07 compute $ 10.00 egress $ 4.80 storage $ 0.27
small-vm $ 8.77 compute $ 7.59 egress $ 0.00 storage $ 1.18
managed-container $ 29.60 compute $ 24.82 egress $ 3.60 storage $ 1.18
-> cheapest: small-vm
--- 1000x: 20,000,000 requests/month ---
serverless-free-tier $ 295.88 compute $ 194.88 egress $ 95.88 storage $ 5.12
serverless-paid $ 300.64 compute $ 200.00 egress $ 96.00 storage $ 4.64
small-vm $ 90.77 compute $ 7.59 egress $ 63.00 storage $20.18
managed-container $ 139.80 compute $ 24.82 egress $ 94.80 storage $20.18
-> cheapest: small-vm
--- where the free tier ends, and why ---
serverless-free-tier free until 1.30x (26,000/mo) binds on egress-gb at 1.25x
serverless-paid not free at launch binds on compute-seconds at 0.00x
small-vm not free at launch binds on compute-seconds at 0.00x
managed-container not free at launch binds on compute-seconds at 0.00x
the headline allowance is rarely the one that runs out first:
egress-gb 1.25x current load
storage-gb 2.50x current load
compute-seconds 22.50x current load
requests 100.00x current load
--- where the ranking changes ---
serverless-free-tier stops beating small-vm at 45.0x (899,599 requests/month)
at that point: serverless $8.66 vs vm $8.67
measured-values.txt
Measured on macOS 26.5.1 (Apple Silicon), Python 3.14.0, pytest 9.1.1.
python3 examples/cost_demo.py -> 31 lines, exit 0
bash tests/run_tests.sh -> 24 passed in 0.01s
the untouched starter -> 24 failed in 0.07s
One AI endpoint -- 20,000 requests a month, 400ms each, 40 KB out -- priced
four ways at three traffic levels.
launch (20k/mo) serverless-free-tier $0.00 small-vm $7.79
50x (1M/mo) serverless-free-tier $10.14 small-vm $8.77
1000x (20M/mo) serverless-free-tier $295.88 small-vm $90.77
The cheapest option at launch is the most expensive one at scale, and the
crossover is not far away:
serverless stops beating the VM at 45.0x -- 899,599 requests/month
at that point serverless $8.66 vs vm $8.67
Nine hundred thousand requests a month is a small service. The option that is
free today is the wrong one before the service is meaningfully popular.
The number this lab exists for is the free-tier headroom table:
egress-gb 1.25x current load
storage-gb 2.50x current load
compute-seconds 22.50x current load
requests 100.00x current load
The tier advertises two million free requests -- a hundred times this traffic.
It ends at 1.25x, on egress, because the egress allowance is one gigabyte. The
advertised constraint and the binding constraint differ by a factor of eighty,
and only the second one decides when the bill starts.
That is the general shape of a free tier: generous on the dimension named in
the headline, tight on one that is not.
Two honest notes.
The demo reports "free until 1.30x" alongside "binds on egress-gb at 1.25x".
The gap is rounding -- a fraction of a gigabyte of egress costs under a cent and
displays as $0.00. The binding constraint is the number to trust.
The model prices a workload; it does NOT check that an option can serve one. At
the 1000x row it quotes a single 1 vCPU VM at $90.77 for twenty million
requests a month, which that machine could not deliver. This is stated in the
module docstring and in security.md rather than hidden, because a cost model
that silently answers the wrong question is worse than no cost model.
run-output.txt
--- launch: 20,000 requests/month ---
serverless-free-tier $ 0.00 compute $ 0.00 egress $ 0.00 storage $ 0.00 FREE
serverless-paid $ 0.34 compute $ 0.20 egress $ 0.10 storage $ 0.05
small-vm $ 7.79 compute $ 7.59 egress $ 0.00 storage $ 0.20
managed-container $ 25.02 compute $ 24.82 egress $ 0.00 storage $ 0.20
-> cheapest: serverless-free-tier
--- 50x: 1,000,000 requests/month ---
serverless-free-tier $ 10.14 compute $ 5.28 egress $ 4.68 storage $ 0.18
serverless-paid $ 15.07 compute $ 10.00 egress $ 4.80 storage $ 0.27
small-vm $ 8.77 compute $ 7.59 egress $ 0.00 storage $ 1.18
managed-container $ 29.60 compute $ 24.82 egress $ 3.60 storage $ 1.18
-> cheapest: small-vm
--- 1000x: 20,000,000 requests/month ---
serverless-free-tier $ 295.88 compute $ 194.88 egress $ 95.88 storage $ 5.12
serverless-paid $ 300.64 compute $ 200.00 egress $ 96.00 storage $ 4.64
small-vm $ 90.77 compute $ 7.59 egress $ 63.00 storage $20.18
managed-container $ 139.80 compute $ 24.82 egress $ 94.80 storage $20.18
-> cheapest: small-vm
--- where the free tier ends, and why ---
serverless-free-tier free until 1.30x (26,000/mo) binds on egress-gb at 1.25x
serverless-paid not free at launch binds on compute-seconds at 0.00x
small-vm not free at launch binds on compute-seconds at 0.00x
managed-container not free at launch binds on compute-seconds at 0.00x
the headline allowance is rarely the one that runs out first:
egress-gb 1.25x current load
storage-gb 2.50x current load
compute-seconds 22.50x current load
requests 100.00x current load
--- where the ranking changes ---
serverless-free-tier stops beating small-vm at 45.0x (899,599 requests/month)
at that point: serverless $8.66 vs vm $8.67
starter-run.txt
An untouched starter fails the suite. Captured from a real run:
FAILED tests/test_hosting_cost.py::test_headroom_reports_a_multiple_per_dimension
FAILED tests/test_hosting_cost.py::test_an_unused_dimension_has_infinite_headroom
FAILED tests/test_hosting_cost.py::test_the_binding_constraint_is_the_smallest_headroom
FAILED tests/test_hosting_cost.py::test_a_dimension_with_no_allowance_binds_immediately
FAILED tests/test_hosting_cost.py::test_the_cheapest_option_is_chosen - NotIm...
FAILED tests/test_hosting_cost.py::test_choosing_from_nothing_is_an_error - N...
FAILED tests/test_hosting_cost.py::test_the_cheap_option_at_launch_can_lose_at_scale
FAILED tests/test_hosting_cost.py::test_two_identically_priced_options_have_no_crossover
FAILED tests/test_hosting_cost.py::test_an_option_that_always_wins_has_no_crossover
FAILED tests/test_hosting_cost.py::test_the_free_ceiling_is_none_when_the_option_costs_money_at_launch
FAILED tests/test_hosting_cost.py::test_the_free_ceiling_finds_where_a_free_tier_runs_out
24 failed in 0.07s
Implement each TASK in starter/, copy it over examples/, and the same
suite goes green.
test-output.txt
........................ [100%]
24 passed in 0.01s
test-run.txt
........................ [100%]
24 passed in 0.01s
Source files
examples/cost_demo.py (3714 bytes)
#!/usr/bin/env python3
"""Four ways to host the same AI endpoint, priced at three traffic levels."""
from __future__ import annotations
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
from hosting_cost import (
Billing,
Option,
Workload,
binding_constraint,
cheapest,
crossover,
free_tier_ceiling,
scale_workload,
)
# A small AI endpoint: 20k requests a month, 400ms each, 50 KB out.
BASE = Workload(
requests=20_000,
seconds_per_request=0.4,
gb_out_per_request=0.00004,
storage_gb=2.0,
)
OPTIONS = [
Option(
"serverless-free-tier", Billing.PER_REQUEST,
per_gb_second_usd=0.000024, per_million_requests_usd=0.40,
free_compute_seconds=180_000, free_requests=2_000_000, free_egress_gb=1.0,
egress_per_gb_usd=0.12, storage_per_gb_usd=0.026, free_storage_gb=5.0,
),
Option(
"serverless-paid", Billing.PER_REQUEST,
per_gb_second_usd=0.000024, per_million_requests_usd=0.40,
egress_per_gb_usd=0.12, storage_per_gb_usd=0.023,
),
Option(
"small-vm", Billing.ALWAYS_ON,
hourly_usd=0.0104, # a 1 vCPU / 1 GB instance
free_egress_gb=100.0, egress_per_gb_usd=0.09,
storage_per_gb_usd=0.10,
),
Option(
"managed-container", Billing.ALWAYS_ON,
hourly_usd=0.034,
free_egress_gb=10.0, egress_per_gb_usd=0.12,
storage_per_gb_usd=0.10,
),
]
def headroom_of(option: Option) -> dict[str, float]:
from hosting_cost import headroom
return headroom(option, BASE)
def table(label: str, w: Workload) -> None:
print(f"--- {label}: {w.requests:,} requests/month ---")
for o in OPTIONS:
b = o.breakdown(w)
free = " FREE" if o.is_free(w) else ""
print(f" {o.name:<22} ${b['total']:>9,.2f} "
f"compute ${b['compute']:>8,.2f} egress ${b['egress']:>7,.2f}"
f" storage ${b['storage']:>5,.2f}{free}")
print(f" -> cheapest: {cheapest(OPTIONS, w).name}")
def main() -> int:
table("launch", BASE)
table("50x", scale_workload(BASE, 50))
table("1000x", scale_workload(BASE, 1000))
print("--- where the free tier ends, and why ---")
for o in OPTIONS:
ceiling = free_tier_ceiling(o, BASE)
which, at = binding_constraint(o, BASE)
limit = "never in range" if at == float("inf") else f"{at:,.2f}x"
if ceiling is None:
state = "free throughout the range" if o.is_free(BASE) else "not free at launch"
print(f" {o.name:<22} {state:<26} binds on {which} at {limit}")
else:
w = scale_workload(BASE, ceiling)
print(f" {o.name:<22} free until {ceiling:>6,.2f}x ({w.requests:,}/mo) "
f"binds on {which} at {limit}")
print(" the headline allowance is rarely the one that runs out first:")
fh = {k: v for k, v in headroom_of(OPTIONS[0]).items()}
for k, v in sorted(fh.items(), key=lambda kv: kv[1]):
print(f" {k:<16} {'unused' if v == float('inf') else f'{v:,.2f}x current load'}")
print("--- where the ranking changes ---")
free_tier = OPTIONS[0]
vm = OPTIONS[2]
x = crossover(free_tier, vm, BASE)
if x:
w = scale_workload(BASE, x)
print(f" serverless-free-tier stops beating small-vm at {x:,.1f}x "
f"({w.requests:,} requests/month)")
print(f" at that point: serverless ${free_tier.monthly_usd(w):,.2f} "
f"vs vm ${vm.monthly_usd(w):,.2f}")
else:
print(" the ranking never changes in the range tested")
return 0
if __name__ == "__main__":
raise SystemExit(main())
examples/hosting_cost.py (7398 bytes)
"""Price a hosting option honestly, including the parts the pricing page omits.
Offline and standard-library only. No cloud account, no API calls, no spending.
A free tier is a real offer with an edge, and the edge is rarely where the
headline suggests. Four things decide what you actually pay, and only the first
appears on the front page:
compute per hour, or per request-second, or free below a threshold
egress per GB leaving the provider. Free tiers rarely include much
idle an always-on VM bills at 3am; scale-to-zero does not
storage small, steady, and the one people forget entirely
The interesting question is not "which is cheapest". It is "at what traffic
does the ranking change", because the answer is usually far lower than people
expect and the cheapest option at launch is frequently the most expensive one
six months later.
Nothing here judges reliability or developer experience, and -- importantly --
nothing here checks CAPACITY. The model will happily price a single 1 vCPU VM
serving twenty million requests a month, which it could not actually do. It
answers "what would this cost", not "would this work", and the second question
has to be answered separately.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
HOURS_PER_MONTH = 730
class Billing(str, Enum):
ALWAYS_ON = "always-on" # billed by the hour whether used or not
PER_REQUEST = "per-request" # billed by compute actually consumed
@dataclass(frozen=True)
class Workload:
"""What the service is asked to do in a month."""
requests: int
seconds_per_request: float
gb_out_per_request: float
storage_gb: float = 1.0
@property
def compute_seconds(self) -> float:
return self.requests * self.seconds_per_request
@property
def egress_gb(self) -> float:
return self.requests * self.gb_out_per_request
@dataclass(frozen=True)
class Option:
"""A hosting option, priced the way providers actually price them."""
name: str
billing: Billing
hourly_usd: float = 0.0 # ALWAYS_ON only
per_gb_second_usd: float = 0.0 # PER_REQUEST only
per_million_requests_usd: float = 0.0
free_compute_seconds: float = 0.0
free_requests: float = 0.0
free_egress_gb: float = 0.0
egress_per_gb_usd: float = 0.0
storage_per_gb_usd: float = 0.0
free_storage_gb: float = 0.0
def compute_usd(self, w: Workload) -> float:
if self.billing is Billing.ALWAYS_ON:
# The defining property: you pay for the hours, not the requests.
return self.hourly_usd * HOURS_PER_MONTH
billable = max(0.0, w.compute_seconds - self.free_compute_seconds)
billable_requests = max(0.0, w.requests - self.free_requests)
per_req = self.per_million_requests_usd * (billable_requests / 1_000_000)
return billable * self.per_gb_second_usd + per_req
def egress_usd(self, w: Workload) -> float:
return max(0.0, w.egress_gb - self.free_egress_gb) * self.egress_per_gb_usd
def storage_usd(self, w: Workload) -> float:
return max(0.0, w.storage_gb - self.free_storage_gb) * self.storage_per_gb_usd
def monthly_usd(self, w: Workload) -> float:
return round(self.compute_usd(w) + self.egress_usd(w) + self.storage_usd(w), 2)
def breakdown(self, w: Workload) -> dict[str, float]:
return {
"compute": round(self.compute_usd(w), 2),
"egress": round(self.egress_usd(w), 2),
"storage": round(self.storage_usd(w), 2),
"total": self.monthly_usd(w),
}
def is_free(self, w: Workload) -> bool:
return self.monthly_usd(w) == 0.0
def headroom(option: Option, w: Workload) -> dict[str, float]:
"""How many times the current workload each free allowance would absorb.
A dimension with no free allowance has no headroom at all, and one that is
unused reports infinity. The smallest finite number is the one that decides
when the free tier ends.
"""
out: dict[str, float] = {}
pairs = (
("compute-seconds", option.free_compute_seconds, w.compute_seconds),
("requests", option.free_requests, float(w.requests)),
("egress-gb", option.free_egress_gb, w.egress_gb),
("storage-gb", option.free_storage_gb, w.storage_gb),
)
for name, allowance, used in pairs:
if used <= 0:
out[name] = float("inf")
else:
out[name] = allowance / used
return out
def binding_constraint(option: Option, w: Workload) -> tuple[str, float]:
"""Which free allowance runs out first, and at what multiple of this load.
This is the number that matters and the one no pricing page prints. A free
tier advertising two million requests can end at 1.25x current traffic
because the egress allowance is a single gigabyte.
"""
h = headroom(option, w)
name = min(h, key=lambda k: (h[k], k))
return name, round(h[name], 2) if h[name] != float("inf") else float("inf")
def cheapest(options: list[Option], w: Workload) -> Option:
"""Cheapest option for this workload, ties broken by name for stability."""
if not options:
raise ValueError("no options to choose from")
return min(options, key=lambda o: (o.monthly_usd(w), o.name))
def scale_workload(w: Workload, factor: float) -> Workload:
"""The same shape of traffic, more of it. Storage grows more slowly."""
return Workload(
requests=int(w.requests * factor),
seconds_per_request=w.seconds_per_request,
gb_out_per_request=w.gb_out_per_request,
storage_gb=round(w.storage_gb * (1 + (factor - 1) * 0.1), 3),
)
def crossover(a: Option, b: Option, base: Workload, *, max_factor: float = 10_000.0) -> float | None:
"""The traffic multiple at which the cheaper option stops being cheaper.
Returned as a multiple of the base workload. None if the ranking never
changes within the search range -- which is itself a useful answer, because
it means the choice is not sensitive to growth.
"""
if a.monthly_usd(base) == b.monthly_usd(base):
return None
a_first = a.monthly_usd(base) < b.monthly_usd(base)
lo, hi = 1.0, max_factor
at_hi = scale_workload(base, hi)
if (a.monthly_usd(at_hi) < b.monthly_usd(at_hi)) == a_first:
return None # same ordering at the far end
for _ in range(60): # bisect; 60 halvings is ample
mid = (lo + hi) / 2
w = scale_workload(base, mid)
if (a.monthly_usd(w) < b.monthly_usd(w)) == a_first:
lo = mid
else:
hi = mid
return round(hi, 2)
def free_tier_ceiling(option: Option, base: Workload, *, max_factor: float = 10_000.0) -> float | None:
"""How much traffic the free tier absorbs, as a multiple of the base.
None if the option is not free even at the base workload, or if it stays
free all the way to the search limit.
"""
if not option.is_free(base):
return None
if option.is_free(scale_workload(base, max_factor)):
return None
lo, hi = 1.0, max_factor
for _ in range(60):
mid = (lo + hi) / 2
if option.is_free(scale_workload(base, mid)):
lo = mid
else:
hi = mid
return round(lo, 2)
metadata.yml (928 bytes)
lesson_id: D334
day: 334
kind: python
languages:
- python
setup_commands:
- pip install -r requirements/requirements.txt
run_commands:
- python3 examples/cost_demo.py
test_commands:
- bash tests/run_tests.sh
cleanup_commands:
- rm -rf __pycache__ .pytest_cache
requires_network: false
requires_api_key: false
estimated_minutes: 55
last_executed: '2026-08-31'
executed_on: 'macOS 26.5.1 (Apple Silicon), Python 3.14.0, pytest 9.1.1 — python3 examples/cost_demo.py
reproduced the documented figures exactly (serverless $0.00 against a VM at $7.79 at launch, the
ranking reversing at 45x, and the free tier binding on egress at 1.25x while its advertised request
allowance would have lasted 100x); bash tests/run_tests.sh -> 24 passed in 0.01s. The untouched
starter fails all 24. requires_network is false and NO CLOUD ACCOUNT is used: the lab prices
published rate cards arithmetically and spends nothing.'
requirements/requirements.txt (14 bytes)
pytest>=7.0.0
starter/hosting_cost.py (5795 bytes)
"""STARTER -- implement each TASK below, then run the tests.
Every function that raises NotImplementedError is yours to write. The
imports, constants and data structures are given; the logic is not.
Run `bash tests/run_tests.sh` to see which tasks are still outstanding.
Price a hosting option honestly, including the parts the pricing page omits.
Offline and standard-library only. No cloud account, no API calls, no spending.
A free tier is a real offer with an edge, and the edge is rarely where the
headline suggests. Four things decide what you actually pay, and only the first
appears on the front page:
compute per hour, or per request-second, or free below a threshold
egress per GB leaving the provider. Free tiers rarely include much
idle an always-on VM bills at 3am; scale-to-zero does not
storage small, steady, and the one people forget entirely
The interesting question is not "which is cheapest". It is "at what traffic
does the ranking change", because the answer is usually far lower than people
expect and the cheapest option at launch is frequently the most expensive one
six months later.
Nothing here judges reliability or developer experience, and -- importantly --
nothing here checks CAPACITY. The model will happily price a single 1 vCPU VM
serving twenty million requests a month, which it could not actually do. It
answers "what would this cost", not "would this work", and the second question
has to be answered separately.
"""
from __future__ import annotations
from dataclasses import dataclass
from enum import Enum
HOURS_PER_MONTH = 730
class Billing(str, Enum):
ALWAYS_ON = 'always-on'
PER_REQUEST = 'per-request'
@dataclass(frozen=True)
class Workload:
"""What the service is asked to do in a month."""
requests: int
seconds_per_request: float
gb_out_per_request: float
storage_gb: float = 1.0
@property
def compute_seconds(self) -> float:
raise NotImplementedError('TASK 1: implement compute_seconds.')
@property
def egress_gb(self) -> float:
raise NotImplementedError('TASK 2: implement egress_gb.')
@dataclass(frozen=True)
class Option:
"""A hosting option, priced the way providers actually price them."""
name: str
billing: Billing
hourly_usd: float = 0.0
per_gb_second_usd: float = 0.0
per_million_requests_usd: float = 0.0
free_compute_seconds: float = 0.0
free_requests: float = 0.0
free_egress_gb: float = 0.0
egress_per_gb_usd: float = 0.0
storage_per_gb_usd: float = 0.0
free_storage_gb: float = 0.0
def compute_usd(self, w: Workload) -> float:
raise NotImplementedError('TASK 3: implement compute_usd.')
def egress_usd(self, w: Workload) -> float:
raise NotImplementedError('TASK 4: implement egress_usd.')
def storage_usd(self, w: Workload) -> float:
raise NotImplementedError('TASK 5: implement storage_usd.')
def monthly_usd(self, w: Workload) -> float:
raise NotImplementedError('TASK 6: implement monthly_usd.')
def breakdown(self, w: Workload) -> dict[str, float]:
raise NotImplementedError('TASK 7: implement breakdown.')
def is_free(self, w: Workload) -> bool:
raise NotImplementedError('TASK 8: implement is_free.')
def headroom(option: Option, w: Workload) -> dict[str, float]:
"""How many times the current workload each free allowance would absorb.
A dimension with no free allowance has no headroom at all, and one that is
unused reports infinity. The smallest finite number is the one that decides
when the free tier ends.
"""
raise NotImplementedError('TASK 9: implement headroom. How many times the current workload each free allowance would absorb.')
def binding_constraint(option: Option, w: Workload) -> tuple[str, float]:
"""Which free allowance runs out first, and at what multiple of this load.
This is the number that matters and the one no pricing page prints. A free
tier advertising two million requests can end at 1.25x current traffic
because the egress allowance is a single gigabyte.
"""
raise NotImplementedError('TASK 10: implement binding_constraint. Which free allowance runs out first, and at what multiple of this load.')
def cheapest(options: list[Option], w: Workload) -> Option:
"""Cheapest option for this workload, ties broken by name for stability."""
raise NotImplementedError('TASK 11: implement cheapest. Cheapest option for this workload, ties broken by name for stability.')
def scale_workload(w: Workload, factor: float) -> Workload:
"""The same shape of traffic, more of it. Storage grows more slowly."""
raise NotImplementedError('TASK 12: implement scale_workload. The same shape of traffic, more of it. Storage grows more slowly.')
def crossover(a: Option, b: Option, base: Workload, *, max_factor: float=10000.0) -> float | None:
"""The traffic multiple at which the cheaper option stops being cheaper.
Returned as a multiple of the base workload. None if the ranking never
changes within the search range -- which is itself a useful answer, because
it means the choice is not sensitive to growth.
"""
raise NotImplementedError('TASK 13: implement crossover. The traffic multiple at which the cheaper option stops being cheaper.')
def free_tier_ceiling(option: Option, base: Workload, *, max_factor: float=10000.0) -> float | None:
"""How much traffic the free tier absorbs, as a multiple of the base.
None if the option is not free even at the base workload, or if it stays
free all the way to the search limit.
"""
raise NotImplementedError('TASK 14: implement free_tier_ceiling. How much traffic the free tier absorbs, as a multiple of the base.')
tests/run_tests.sh (209 bytes)
#!/usr/bin/env bash
# Run the day 334 hosting-cost test suite.
# Offline, standard library plus pytest. No cloud account, no spending.
set -euo pipefail
cd "$(dirname "$0")/.."
python3 -m pytest tests -q "$@"
tests/test_hosting_cost.py (7271 bytes)
"""Grouped by what the cost model decides.
Run with: bash tests/run_tests.sh
"""
from __future__ import annotations
import math
import os
import sys
import pytest
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(os.path.dirname(HERE), "examples"))
from hosting_cost import ( # noqa: E402
HOURS_PER_MONTH,
Billing,
Option,
Workload,
binding_constraint,
cheapest,
crossover,
free_tier_ceiling,
headroom,
scale_workload,
)
BASE = Workload(requests=20_000, seconds_per_request=0.4,
gb_out_per_request=0.00004, storage_gb=2.0)
def vm(hourly=0.01, **kw):
return Option("vm", Billing.ALWAYS_ON, hourly_usd=hourly, **kw)
def serverless(**kw):
return Option("srv", Billing.PER_REQUEST, per_gb_second_usd=0.000024, **kw)
# --------------------------------------------------------------- workload
def test_compute_seconds_is_requests_times_duration():
assert BASE.compute_seconds == 8_000.0
def test_egress_is_requests_times_bytes_out():
assert BASE.egress_gb == pytest.approx(0.8)
def test_scaling_multiplies_traffic_but_not_storage_proportionally():
w = scale_workload(BASE, 10)
assert w.requests == 200_000
assert w.storage_gb < BASE.storage_gb * 10 # storage grows more slowly
def test_scaling_by_one_changes_nothing_material():
w = scale_workload(BASE, 1.0)
assert w.requests == BASE.requests and w.storage_gb == BASE.storage_gb
# ------------------------------------------------------------- always-on
def test_an_always_on_option_bills_by_the_hour_regardless_of_traffic():
# The defining property: it costs the same at 3am with no users.
o = vm(hourly=0.01)
quiet = Workload(requests=1, seconds_per_request=0.1, gb_out_per_request=0.0)
assert o.compute_usd(BASE) == o.compute_usd(quiet)
assert o.compute_usd(BASE) == pytest.approx(0.01 * HOURS_PER_MONTH)
def test_an_always_on_option_costs_the_same_at_zero_traffic():
o = vm(hourly=0.02)
idle = Workload(requests=0, seconds_per_request=0.0, gb_out_per_request=0.0)
assert o.compute_usd(idle) > 0
# ----------------------------------------------------------- per-request
def test_a_per_request_option_costs_nothing_at_zero_traffic():
o = serverless(per_million_requests_usd=0.40)
idle = Workload(requests=0, seconds_per_request=0.0, gb_out_per_request=0.0)
assert o.compute_usd(idle) == 0.0
def test_free_compute_seconds_are_deducted_before_billing():
o = serverless(free_compute_seconds=8_000.0)
assert o.compute_usd(BASE) == 0.0
def test_free_requests_are_deducted_before_billing():
# Isolate the per-request charge: give both options the same free seconds,
# so the only difference between them is the request allowance.
billed = serverless(per_million_requests_usd=1.0, free_compute_seconds=8_000)
covered = serverless(per_million_requests_usd=1.0, free_compute_seconds=8_000,
free_requests=20_000)
assert billed.compute_usd(BASE) == pytest.approx(0.02)
assert covered.compute_usd(BASE) == 0.0
def test_usage_beyond_the_free_allowance_is_billed():
o = serverless(free_compute_seconds=4_000.0)
assert o.compute_usd(BASE) == pytest.approx(4_000.0 * 0.000024)
# --------------------------------------------------------------- egress
def test_egress_below_the_free_allowance_is_free():
assert vm(free_egress_gb=1.0, egress_per_gb_usd=0.09).egress_usd(BASE) == 0.0
def test_only_the_excess_egress_is_billed():
o = vm(free_egress_gb=0.5, egress_per_gb_usd=0.10)
assert o.egress_usd(BASE) == pytest.approx(0.03) # 0.3 GB over
def test_egress_is_frequently_the_dominant_cost_at_scale():
o = vm(hourly=0.0104, free_egress_gb=100.0, egress_per_gb_usd=0.09)
big = scale_workload(BASE, 1000)
assert o.egress_usd(big) > o.compute_usd(big)
# -------------------------------------------------------------- headroom
def test_headroom_reports_a_multiple_per_dimension():
o = serverless(free_compute_seconds=80_000, free_requests=2_000_000,
free_egress_gb=1.0, free_storage_gb=5.0)
h = headroom(o, BASE)
assert h["compute-seconds"] == pytest.approx(10.0)
assert h["requests"] == pytest.approx(100.0)
def test_an_unused_dimension_has_infinite_headroom():
w = Workload(requests=10, seconds_per_request=0.1, gb_out_per_request=0.0)
assert math.isinf(headroom(serverless(free_egress_gb=5.0), w)["egress-gb"])
def test_the_binding_constraint_is_the_smallest_headroom():
# THE finding: the advertised allowance is rarely the one that runs out.
o = serverless(free_compute_seconds=180_000, free_requests=2_000_000,
free_egress_gb=1.0, free_storage_gb=5.0)
which, at = binding_constraint(o, BASE)
assert which == "egress-gb"
assert at == pytest.approx(1.25, abs=0.01)
assert headroom(o, BASE)["requests"] == pytest.approx(100.0)
def test_a_dimension_with_no_allowance_binds_immediately():
which, at = binding_constraint(vm(), BASE)
assert at == 0.0
# ------------------------------------------------------------- selection
def test_the_cheapest_option_is_chosen():
a, b = vm(hourly=1.0), Option("b", Billing.ALWAYS_ON, hourly_usd=0.001)
assert cheapest([a, b], BASE).name == "b"
def test_choosing_from_nothing_is_an_error():
with pytest.raises(ValueError):
cheapest([], BASE)
# ------------------------------------------------------------ crossover
def test_the_cheap_option_at_launch_can_lose_at_scale():
# Serverless wins when idle and loses when busy. Finding WHERE is the point.
srv = serverless(per_million_requests_usd=0.40, free_compute_seconds=180_000,
free_requests=2_000_000, free_egress_gb=1.0,
egress_per_gb_usd=0.12, storage_per_gb_usd=0.026, free_storage_gb=5.0)
box = vm(hourly=0.0104, free_egress_gb=100.0, egress_per_gb_usd=0.09,
storage_per_gb_usd=0.10)
assert srv.monthly_usd(BASE) < box.monthly_usd(BASE)
x = crossover(srv, box, BASE)
assert x is not None and 1.0 < x < 1000.0
at = scale_workload(BASE, x * 1.5)
assert box.monthly_usd(at) < srv.monthly_usd(at)
def test_two_identically_priced_options_have_no_crossover():
a = vm(hourly=0.01)
b = Option("b2", Billing.ALWAYS_ON, hourly_usd=0.01)
assert crossover(a, b, BASE) is None
def test_an_option_that_always_wins_has_no_crossover():
cheap = vm(hourly=0.001)
dear = Option("dear", Billing.ALWAYS_ON, hourly_usd=10.0)
assert crossover(cheap, dear, BASE) is None
# --------------------------------------------------------- free ceiling
def test_the_free_ceiling_is_none_when_the_option_costs_money_at_launch():
assert free_tier_ceiling(vm(hourly=0.01), BASE) is None
def test_the_free_ceiling_finds_where_a_free_tier_runs_out():
o = serverless(free_compute_seconds=180_000, free_requests=2_000_000,
free_egress_gb=1.0, egress_per_gb_usd=0.12,
free_storage_gb=5.0, storage_per_gb_usd=0.026)
assert o.is_free(BASE)
ceiling = free_tier_ceiling(o, BASE)
assert ceiling is not None and ceiling > 1.0
assert not o.is_free(scale_workload(BASE, ceiling * 2))
Troubleshooting
Troubleshooting — Day 334
NotImplementedError on every test
Expected. The starter stubs fourteen functions — see expected-output/starter-run.txt.
Start with Workload.compute_seconds and Workload.egress_gb; every price depends on them.
test_an_always_on_option_bills_by_the_hour_regardless_of_traffic fails
You are scaling an always-on option by usage. That is the property that distinguishes it: a VM costs hourly × 730 whether it served ten requests or ten million, including at 3am when it served none. If your always-on cost changes with traffic, the entire comparison in this lab collapses.
test_a_per_request_option_costs_nothing_at_zero_traffic fails
The mirror image. A per-request option must bill nothing when there are no requests — that is what scale-to-zero means and it is the only reason serverless wins at launch.
test_free_requests_are_deducted_before_billing fails
There are two independent allowances on a per-request option: free compute-seconds and free requests. Deduct each from its own quantity. The test isolates the request charge by giving both options the same free seconds, so a failure here means the request allowance is being ignored or applied to the wrong quantity.
Free allowances go negative
Clamp with max(0.0, used - allowance). Without it, an option under its free tier earns you money, which is a pleasing bug and not a correct one.
test_the_binding_constraint_is_the_smallest_headroom fails
headroom returns a multiple per dimension — allowance divided by current usage. binding_constraint takes the smallest of those, because the free tier ends when the first allowance runs out, not when the advertised one does.
Watch the zero-usage case: a dimension with no usage has infinite headroom, not a division error.
crossover returns None when you expect a number
Three legitimate reasons, and only the third is a bug:
- The two options cost the same at the base workload.
- One option is cheaper at every scale in the range — the ranking never changes, which is a genuinely useful answer.
- Your bisection is not bracketing correctly. Check that you compare the ordering at the far end before searching.
The free ceiling is slightly above the binding constraint
Not a bug. The demo reports a free tier binding on egress at 1.25x and staying free until about 1.30x, because a small fraction of a gigabyte of egress rounds to $0.00 at cent precision. The binding constraint is the honest number; the ceiling includes the rounding.
The numbers do not match my provider
They will not. The rate cards in the demo are representative, not current, and providers change them. The lab is about the shape — always-on against per-request, and which allowance binds first — not about any particular price. Substitute your provider's numbers and re-run.
Security notes
Security notes — Day 334
What this lab does and does not touch
- No cloud account, no credentials, no API calls, no spending. The rate cards are literals in the demo. Nothing is provisioned.
- No network.
- No writes outside this directory. Only
__pycache__/,.pytest_cache/and, if you create it,.venv/. - No
sudo, no global installs.
A free tier is a credential you are about to create
The security event of this day is not in the code. It is what happens twenty minutes after the lesson, when somebody signs up for a provider to try the numbers.
Three things go wrong at that moment, reliably:
- A root account with no MFA. The account you create in a hurry to test something becomes the account that owns production. Enable MFA before anything else, and create a separate non-root user for day-to-day work.
- A long-lived access key on a laptop.
~/.aws/credentialsand its equivalents outlive the experiment by years. Prefer short-lived credentials, and delete keys created "just to try it". - A key committed to a repository. This is how most cloud-account compromises begin, and automated scanners find a pushed key within minutes. Day 330's point about layers applies here too — removing it in a later commit does not remove it from history.
Free tiers are the target, not a safe corner
An account with a free tier and a card attached is worth compromising precisely because the attacker's spending is not capped by the free allowance — it is capped by your card. Crypto-mining on stolen cloud credentials is a routine, industrialised business, and it favours new accounts with weak hygiene.
Set a budget alert before you deploy anything. Every major provider offers them free. Day 327 built the per-request cap for model spend; this is the same control at the account level, and it is the difference between a surprising bill and a ruinous one.
Egress pricing has a security dimension
The cost model here prices egress because it dominates at scale. It is also the mechanism by which a compromised endpoint becomes expensive: an attacker who can make your service return large responses is spending your egress budget, and rate limits usually count requests rather than bytes.
If a response size can be influenced by input, cap the bytes as well as the calls.
What this model deliberately does not do
It prices a workload. It does not check that the option can serve it — the model will happily quote a single 1 vCPU VM for twenty million requests a month, which it could not do. Cost is one input to a hosting decision; capacity, reliability and blast radius are others, and a cheap option that falls over is not cheap.
Reversing everything this lab did
rm -rf __pycache__ .pytest_cache .venv
Nothing else was created, and nothing outside this directory was modified.