Sub topic 1
What serverless means and how Lambda is billed
Prerequisites
This chapter assumes you can read basic Python, understand JSON and HTTP APIs, have an AWS account with access to the console or AWS CLI, and know what an IAM role is. You do not need to have written a Lambda function yet.
What serverless changes
"Serverless" does not mean there are no servers. It means you do not provision, patch or scale them. AWS runs the servers, and you supply code and configuration.
With a virtual machine, you choose an instance size, keep it running, and pay for every hour whether or not it does work. With Lambda, you upload a function. AWS runs it only when an event arrives, and you pay only for those runs.
Three properties define the model:
- No server management. AWS handles the operating system, runtime patching and capacity.
- Event-driven execution. Something triggers the function, such as an HTTP request, a queue message or a file upload.
- Automatic scaling. AWS starts more copies of your function as events arrive and stops them when traffic drops. Scaling to zero means you pay nothing when idle.
The trade-off is control. You accept limits on run time (a maximum of 15 minutes per invocation), you cannot keep state in the function between unrelated requests, and you design around events instead of long-running processes.
How Lambda is billed
Lambda charges on two main dimensions:
- Requests. A flat price per million invocations.
- Duration. The time your code runs, rounded up to the nearest millisecond, multiplied by the memory you configured. The unit is the GB-second.
Check the AWS pricing page for current rates, because they vary by region and CPU architecture (Arm-based Graviton functions cost less per GB-second than x86). A free tier also applies each month.
Duration cost depends on memory, not on memory actually used. A function configured with 1024 MB that runs for 200 ms uses 1 GB × 0.2 s = 0.2 GB-seconds.
def lambda_cost(memory_mb, duration_ms, invocations,
price_per_gb_second, price_per_million_requests):
gb_seconds = (memory_mb / 1024) * (duration_ms / 1000) * invocations
duration_cost = gb_seconds * price_per_gb_second
request_cost = (invocations / 1_000_000) * price_per_million_requests
return duration_cost + request_cost
# Example with placeholder prices. Replace them with current regional prices.
print(lambda_cost(
memory_mb=512,
duration_ms=120,
invocations=10_000_000,
price_per_gb_second=0.0000166667,
price_per_million_requests=0.20,
))With these placeholder prices, 10 million invocations at 512 MB and 120 ms use 600,000 GB-seconds. That is about 10.00 for duration plus 2.00 for requests, roughly 12.00 in total.
Common errors in cost thinking
- Assuming serverless is always cheaper. A function that runs steadily at high volume can cost more than a container or instance doing the same work. Compare at your expected traffic.
- Ignoring waiting time. You are billed while your code waits on a slow database or HTTP call. A function that idles for 3 seconds waiting on a response costs the same as one computing for 3 seconds.
- Forgetting related charges. Logging to CloudWatch, data transfer, API Gateway requests and optional features such as provisioned concurrency are billed separately.
- Over-sizing memory by habit. Test a few memory values and compare cost per invocation, not just speed.
When Lambda fits
Lambda suits spiky or unpredictable traffic, event processing, scheduled jobs and glue between AWS services. It fits poorly for work that runs longer than 15 minutes, needs very low and consistent latency without tuning, or keeps persistent connections open.
Exercise
A function is configured with 2048 MB and runs for 500 ms per invocation. It is invoked 1,000,000 times. How many GB-seconds of duration are billed, and what would you change first to try to lower cost?
Show answer
2048 MB is 2 GB, and 500 ms is 0.5 s, so each invocation uses 1 GB-second. Across 1,000,000 invocations that is 1,000,000 GB-seconds. To lower cost, test different memory settings and measure duration. If the function is not CPU-bound, a lower memory setting may cut the cost. If it is CPU-bound, a higher setting may shorten the duration enough to cost less.
Exercise
Why can a function that mostly waits on a slow external API be expensive, even though it uses little CPU?
Show answer
Lambda bills for the whole time the handler runs, multiplied by configured memory. Waiting time counts as billed duration, so slow downstream calls raise cost without doing any computation.