skip to content

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%

answer

  1. compute splits by itself
  2. shared lines need an allocation key
  3. bytes, not equal shares
  4. billed duration per invocation
  5. cost per business transaction

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.

solid answer

~40 s

I attribute in layers. Compute comes per function from the provider's resource-level billing data, and logs often do too where they are grouped per function. Shared lines get an allocation key that each function actually drives: gateway charges by requests routed to it, log ingestion by bytes, the NAT gateway by bytes sent from flow logs, never an even split. For per-request cost on a per-invocation platform I take its per-invocation report of billed duration and memory, price it with our own rate, add the request's share of gateway and log bytes, and join on the request id with route and tenant in the same structured log line. That gives unit cost per route, tenant or order, and a deploy that raises it shows up as a step even when traffic is flat.

code

json · 11 lines
json
{
  "requestId": "req-7f3c",
  "function": "orders-api",
  "route": "POST /orders",
  "tenant": "tenant-a",
  "outcome": "success",
  "attempt": 1,
  "billedDurationMs": 180,
  "memoryMb": 512,
  "logBytes": 2048
}

go deeper

for a junior

Remember that compute can be split per function easily, but the gateway, logs and NAT are shared and need to be divided by something each function actually uses.

for a middle

Explain an allocation key for each shared line and how billed duration and memory from each invocation give a per-request cost joined on the request id.

for a senior

Show how you would use unit cost per route or tenant to catch a deploy that raised cost per request, and why retries and failures are tracked as their own share.

for a principal

Decide how precise attribution must be: stable, explainable keys that reach the team able to change the cost beat exact figures nobody can act on.

## Why per-function cost is harder than it looks A function workload is many small units sharing a few large services. The **function compute** line is the easy part: resource-level billing data can usually break it out per function. The lines that surprise people, the API gateway, log ingestion, the NAT gateway, tracing and queues, are often shared by many functions, and a team that attributes only compute ends up owning the cheap part of the bill and nobody owns the expensive part. Attribution here has two goals: **per function**, so each team sees what its functions cost, and **per request**, so the cost of a route, a tenant or a business transaction is known. ## Layer one: what the bill already splits - **Function compute** per function, from the provider's resource-level billing export. - **Log storage and ingestion** per function, where the platform groups logs per function by default. - Anything carrying an owner or service label; how labels flow into billing data is a general attribution subject, and the point here is that the shared lines are exactly the ones a label on a function cannot reach. ## Layer two: allocating the shared lines A shared line needs an **allocation key**: a measured quantity that each function contributes to, so the line can be split in proportion. | Shared line | Allocation key | Where the key comes from | |---|---|---| | API gateway | requests routed to each function | gateway access logs or per-route metrics | | Log ingestion | bytes ingested per function | log service usage per log group or stream | | NAT gateway | bytes each function sends through it | network flow logs | | Tracing service | traces recorded per function | tracing service usage | | Pre-provisioned capacity | capacity configured per function | function configuration | An even split is the usual mistake: one chatty function or one heavy downloader can own most of a shared line, and an even split hides it from the team that could fix it. ## Layer three: cost per request Per-request cost is built bottom-up from invocation data rather than by dividing the month's total by request count: 1. Read the platform's **per-invocation report**, which on platforms that bill each invocation records billed duration and configured memory for it. 2. Multiply by the compute rate from your own bill to get each invocation's compute cost. 3. Add the per-request share of the gateway, the log bytes that invocation wrote, and any per-request charges for queues, secrets fetches or downstream calls. 4. Join on the **request id**, and carry the route, tenant and feature in the same structured log line, so the cost can be grouped by any of them. 5. Aggregate into **unit cost**: cost per order, per active tenant, per thousand requests on a route. On a **container-instance platform** that sends many requests to one instance and bills instance time, there is no per-request billed duration to read. Divide each instance's billed time across the requests it served, weighted by each request's duration, and the same unit-cost reporting follows. Mixing the two methods across platforms is fine as long as each workload uses one consistently. ## Turning it into a signal - Track cost per request per route over time; a deploy that makes a route log twice as much or run longer shows up as a step in its unit cost even when total traffic is flat. - Attribute **retried and failed invocations** separately: they are cost with no business output, and their share is a direct measure of how much reliability problems are costing. - Report per tenant where tenants differ widely in usage, because pricing and limits for customers depend on it. ## Pitfalls - Allocation keys that drift: a new function added without its logs or routes mapped falls into an unallocated bucket. - Free allowances and commitment discounts applied unevenly, which makes some functions look cheaper than their usage. - Using a monthly average duration to price routes whose durations vary widely. - Applying per-invocation arithmetic to a platform that bills instance time, which counts the same instance-second once per concurrent request. - Double counting a shared line once as allocated cost and again as overhead. Good attribution does not need to be exact to the cent. It needs to be stable, explainable, and fine enough that the team that can change a cost sees it.

  • Why attribute retried and failed invocations separately?
    They cost the same per invocation as successful ones but produce no business result. Reporting them as their own share shows how much reliability problems cost and gives the owning team a figure to bring down, instead of hiding it inside an average cost per request.
  • How would you attribute cost per tenant in one multi-tenant function?
    Carry the tenant id in every structured log line and trace, compute each invocation's cost from billed duration, memory and log bytes, and sum by tenant. Shared fixed lines, such as pre-provisioned capacity, are split by each tenant's share of invocations or duration.

  • An owner label on each function attributes the whole workload's bill.
  • Gateway and log charges can simply be split evenly across functions.
  • Per-request cost is the monthly bill divided by the request count.
  • A monthly average duration is precise enough to price individual routes.
  • Failed invocations are free, so they need no attribution.