# Understanding the Cost Model — Serverless & Edge

Source: https://www.skillbyai.com/en/serverless-edge/se-cost-model

> Estimate a function's monthly cost from requests, duration and memory, and spot the costs that hide around it.

## Requests, GB-seconds and everything around them

FaaS pricing usually has two parts: a fee per **request** and a fee for **compute time**, measured in GB-seconds (configured memory in GB multiplied by billed duration in seconds). Lambda bills duration in 1 ms increments. Edge platforms often bill per request plus **CPU time** rather than wall-clock time, so waiting on a network fetch costs little there. The big numbers frequently sit around the function: API gateway requests, data transfer out, NAT gateways, log ingestion and storage, and the managed services you call. Pay-per-use is cheap at low or spiky load and can cost more than a reserved server at constant high load, so estimate both. On Lambda, more memory also means proportionally more CPU, which can shorten duration enough that a bigger setting costs the same or less. Measure instead of guessing, for example with the open-source AWS Lambda Power Tuning tool.

## A cost estimate you can adapt

Replace the placeholder rates with current prices from your provider. The arithmetic is the part to remember.

```python
# Rough monthly cost model for a pay-per-use function.
# The two rates below are ILLUSTRATIVE placeholders - read your provider's pricing page.
PRICE_PER_MILLION_REQUESTS = 0.20
PRICE_PER_GB_SECOND = 0.0000166667

requests = 30_000_000          # per month
avg_duration_s = 0.120         # 120 ms billed per call
memory_gb = 512 / 1024         # 512 MB configured

gb_seconds = requests * avg_duration_s * memory_gb
compute = gb_seconds * PRICE_PER_GB_SECOND
request_fee = requests / 1_000_000 * PRICE_PER_MILLION_REQUESTS

print(f"GB-seconds : {gb_seconds:,.0f}")
print(f"compute    : ${compute:,.2f}")
print(f"requests   : ${request_fee:,.2f}")
print(f"total      : ${compute + request_fee:,.2f}")
```

Output:

```
GB-seconds : 1,800,000
compute    : $30.00
requests   : $6.00
total      : $36.00
```

## The electricity meter

Your home electricity bill counts units: power used multiplied by hours. A function's bill counts memory multiplied by seconds. A heater left running is your function waiting on a slow API, and the fixed meter charge is the per-request fee.

## Set a budget alarm on day one

A recursive trigger or a runaway retry loop can multiply invocations within hours. Configure a billing budget with alerts on every cloud account, including your personal student account, before deploying anything.

**Quiz:** A function configured with 1024 MB runs for 200 ms per request. How many GB-seconds does one request use?

- [x] 0.2
- [ ] 204.8
- [ ] 1024
- [ ] 0.02

*Answer:* 0.2. 1024 MB is 1 GB, and 200 ms is 0.2 s, so 1 x 0.2 = 0.2 GB-seconds.
