skip to content

Function Cost & Trade-offs

A function's compute is often the smallest line on its bill. Interviewers ask what else a function workload pays for, how retries run up cost, and when the numbers say move off.

on this pageshow

questions

4

Why can a function-backed API's gateway, log ingestion and NAT gateway charges each exceed the function's own compute charge?

level: middleimportance: must knowfreq 62%

answer

  1. compute is one meter among many
  2. a per-request front door
  3. bytes logged on every invocation
  4. private network traffic through NAT
  5. capacity billed while idle

basics

~20 s

The function's compute meter is only one line. The gateway charges per request, logging per gigabyte ingested and stored, and a NAT gateway per hour and per gigabyte processed, so short, cheap invocations can leave those lines larger than compute.

solid answer

~40 s

A function platform bills invocations and duration times memory, so a fast handler is cheap per call. The services around it meter different things: the API gateway charges per request, the log service per gigabyte ingested and per gigabyte-month kept, and a NAT gateway, needed when a function in a private network calls out, per hour and per gigabyte processed. None of those shrink when the handler gets faster. Add pre-provisioned capacity left on, billed whether used or not, and compute easily becomes the smallest line. I read the bill by service, divide each line by invocations, then cut the biggest: log level and retention, private endpoints instead of NAT, a lighter gateway tier or caching, and pre-provisioned capacity sized to measured use.

go deeper

for a junior

Remember that a function workload is billed by several services: the gateway per request, logs per gigabyte, NAT per hour and per gigabyte, and the function itself per invocation and duration.

for a middle

Explain what each non-compute line meters and why a faster handler does not move it, then name the first lever for each: log level and retention, private endpoints, gateway tier or caching.

for a senior

Show how you would read a real bill by service, compute a per-request figure for each line, and find the one function or setting that dominates before changing anything.

for a principal

Treat logging, network paths and pre-provisioned capacity as defaults a platform team sets for every function, so the non-compute lines are designed in rather than discovered on the bill.

## Compute is one meter among several Most **function platforms** bill the function itself for invocations and for execution time scaled by the memory configured (a container-instance platform bills instance time instead, with the same effect). That meter rewards short, efficient handlers, and that is exactly why the rest of the bill stands out. A handler that finishes in a few dozen milliseconds costs very little per call, but every call also passes through a front door, writes log lines, may cross a **NAT gateway** and may land on capacity that was paid for whether or not it ran. Each of those services keeps its own meter, and none of them gets cheaper because the handler got faster. A startup whose function-backed API shows the gateway, log ingestion and a NAT gateway each above function compute is therefore typical, not unlucky. The fix starts with reading the bill **by service**, not only by function. ## The front door: per-request gateway charges - An **API gateway** usually charges per request received, plus data sent back out, and its optional features (response caching, a custom authoriser that is itself another function invocation) add charges of their own. - Because the gateway meters requests and a fast handler's compute per request is tiny, a chatty API with small, quick handlers can easily pay more at the gateway than for compute. - Levers: a lighter HTTP gateway tier where the provider offers one, a built-in HTTPS endpoint on the function where the platform has one (giving up gateway features such as throttling and authorisers), batching chatty client calls, and a CDN cache in front of cacheable reads. At steady high volume, a load balancer priced by the hour and by capacity used can undercut per-request pricing. ## Logs: ingestion and retention - Log services typically charge per gigabyte **ingested** and then per gigabyte-month **stored**; ingestion is usually the larger of the two. - Almost every invocation writes something. On many platforms the platform itself writes start, end and report lines per invocation, and the handler adds its own. Debug-level logging left on in production, whole request and response payloads, and stack traces repeated on every retry multiply the bytes per invocation. - On some providers a new log group keeps everything indefinitely until someone sets a retention period, so storage only ever grows. - Levers: a log level per environment, sampling of success-path logs, emitting a metric instead of a log line for counts, retention set in the deployment template at creation, archiving to object storage, and a cheaper log class or an ingestion-time exclusion filter where the provider offers one. ## Egress and the NAT gateway When a function is attached to a **private network** (usually to reach a database), its outbound traffic to the internet, including calls to the provider's own public service endpoints and to third-party APIs, leaves through a NAT gateway. NAT gateways commonly charge per hour for each gateway (often one per zone for resilience) and per gigabyte processed, on top of ordinary internet egress. A function that pulls large objects from object storage through NAT pays processing on every byte. The levers, in the order they usually pay off: 1. Take functions that need nothing private out of the private network. 2. Add **private endpoints** for the provider services the function calls, so that traffic skips NAT; some endpoint types carry their own hourly charge, so check the arithmetic per service. 3. Cache third-party responses and compress payloads, because NAT and egress both meter bytes. ## Idle pre-provisioned capacity **Pre-provisioned capacity** or a **minimum instance count** keeps instances initialised so requests avoid cold starts. It is billed for as long as it is configured, whether requests arrive or not (some platforms bill idle instances at a reduced rate). Set for a launch and never removed, or switched on for every function after one latency complaint, it becomes a standing always-on charge on a model chosen for not having one. Schedule it to follow known traffic and review how much of it is actually used. Smaller lines appear too: the tracing service per trace recorded, a secrets store per fetch when a handler reads a secret on every invocation, queues and topics per request, and a hosted workflow engine per step. ## Reading the bill line by line | Bill line | What it meters | First lever | |---|---|---| | API gateway | requests received, features used | lighter tier, caching, fewer chatty calls | | Log ingestion | bytes written per invocation | log level, sampling, metrics instead of lines | | Log storage | bytes kept times retention | a retention period set at creation | | NAT gateway | gateway hours plus bytes processed | private endpoints, leave the private network | | Pre-provisioned capacity | configured capacity over time | schedule it, size it to measured use | A practical first pass: 1. Group the month's bill by service and put the function compute line next to every other line the workload touches. 2. Divide each line by the invocation count to get a per-request figure for the gateway, logs and NAT. 3. Find the biggest log writers by bytes per invocation, not by invocation count. 4. Find which functions send traffic through NAT and what they are fetching. 5. List every pre-provisioned or minimum-instance setting with its utilisation.

  • Why does making the handler faster leave the gateway and NAT lines unchanged?
    Those services do not meter execution time. The gateway charges per request received and the NAT gateway per hour and per gigabyte processed, so a handler that finishes sooner still makes the same number of requests and moves the same bytes. Only fewer requests, smaller payloads or a different network path move those lines.
  • Is taking every function out of the private network a safe way to remove the NAT charge?
    Only for functions that reach nothing private. A function that needs the database still needs a path into the private network; for it, private endpoints to the provider services it calls remove most NAT traffic while keeping the database reachable. Check each endpoint's own hourly charge against the NAT bytes it saves.
  • What is usually the quickest win on the log line?
    Set a retention period on every log group and drop debug-level and full-payload logging in production. Measure bytes per invocation first: one noisy function often accounts for most of the ingestion, and fixing it beats trimming every function a little.

A short taxi ride on a toll road: the meter for the ride is small, but the gate toll per trip, the airport access fee and a car kept waiting at the kerb are charged separately, and driving faster lowers only the meter.

  • Function cost is invocations times duration, so a faster handler fixes the whole bill.
  • Logs cost nothing because the platform writes them automatically.
  • A NAT gateway charges only for the hours it runs, not for traffic.
  • Pre-provisioned capacity costs money only while it serves requests.
  • The gateway charge grows with the function's execution time.
open as a page

How do you attribute a function workload's bill to individual functions and individual requests when the gateway, logs and NAT are shared?

level: middleimportance: should knowfreq 38%

basics

~20 s

Function compute usually splits per function in billing data; shared lines need allocation keys: gateway by requests, logs by bytes, NAT by bytes sent. Per-request cost comes from each invocation's billed duration and memory where the platform bills per invocation.

open as a page

A function that writes its result into the same storage bucket that triggers it ran up a large bill overnight; what happened, and what stops it recurring?

level: seniorimportance: should knowfreq 58%

basics

~20 s

Each result written to the bucket fired the trigger again, so the function looped and scaled out until a limit or a person stopped it. Write output elsewhere, cap concurrency and alarm on invocation rate; billing alerts lag by hours.

open as a page

A steady, always-busy API has run on functions for years and its bill keeps growing; what evidence would make you move it to containers, and would you move all of it?

level: principalimportance: should knowfreq 45%

basics

~20 s

Move when the evidence shows a steady concurrency floor, pre-provisioned capacity covering it, and full-bill cost per request above a realistic container estimate including operations work. Usually move only the steady hot path and keep event-shaped work on functions.

open as a page