
Giving Your Lambda More Memory Can Make It Cheaper. Here's the Math.
Most Lambda functions run on the 128 MB default and are slower than they need to be, often at no saving. Here's why more memory can cost the same or less, and a short Python script that finds the right setting for your function.
When you create a Lambda function, the memory setting starts at 128 MB. Most people never touch it, because more memory sounds like more money.
That instinct is often wrong. For a lot of functions, raising memory makes them several times faster for the same price, and occasionally for less.
Memory is also your CPU setting
Lambda doesn't let you choose CPU directly. It gives you CPU in proportion to the memory you pick. At 1,769 MB you get the equivalent of one full vCPU. At 128 MB you get a small slice of one.
So if your function does real work (parsing JSON, resizing images, running a model, compressing files), 128 MB isn't just low on memory. It's starved for CPU.
Why it can cost the same
Lambda bills in GB-seconds: memory multiplied by how long the function runs.
cost = memory (GB) x duration (seconds) x price per GB-secondDouble the memory and the price per second doubles. But if the extra CPU also cuts the run time in half, the two cancel out. You pay the same and your users wait half as long.
Here's the typical shape for a CPU-bound function. These are illustrative numbers to show the pattern, not a benchmark:
Memory | Duration | Cost per 1M calls |
|---|---|---|
128 MB | 2,400 ms | $5.00 |
256 MB | 1,200 ms | $5.00 |
512 MB | 600 ms | $5.00 |
1024 MB | 310 ms | $5.17 |
1769 MB | 190 ms | $5.47 |
3008 MB | 185 ms | $9.06 |
Going from 128 MB to 1024 MB makes the function almost 8 times faster for about 3% more. Past 1,769 MB, a single-threaded function stops getting faster and you're paying for nothing.
The opposite case matters too. If your function spends most of its time waiting on a database or an API, more CPU won't help and the low setting is fine. That's why you measure instead of guessing.
A script that finds your number
This tests one function at several memory sizes, reads the billed duration from each run, and prints the cost per million calls. It puts the original setting back when it's done.
import base64, re, boto3
FUNCTION = "my-test-function"PAYLOAD = b"{}" # an event your function acceptsSIZES = [128, 256, 512, 1024, 1769, 3008]PRICE = 0.0000166667 # per GB-second, x86 in us-east-1
lam = boto3.client("lambda")original = lam.get_function_configuration(FunctionName=FUNCTION)["MemorySize"]
def set_memory(mb): lam.update_function_configuration(FunctionName=FUNCTION, MemorySize=mb) lam.get_waiter("function_updated").wait(FunctionName=FUNCTION)
for mb in SIZES: set_memory(mb) lam.invoke(FunctionName=FUNCTION, Payload=PAYLOAD) # warm up runs = [] for _ in range(5): r = lam.invoke(FunctionName=FUNCTION, Payload=PAYLOAD, LogType="Tail") log = base64.b64decode(r["LogResult"]).decode() runs.append(int(re.search(r"Billed Duration: (\d+)", log).group(1))) ms = sum(runs) / len(runs) cost = (mb / 1024) * (ms / 1000) * PRICE * 1_000_000 print(f"{mb:>5} MB {ms:>7.0f} ms ${cost:.2f} per 1M calls")
set_memory(original)A few notes before you run it:
- Point it at a test copy of your function. It really invokes the function about 36 times with your payload, so make sure that's safe.
- Check the price for your Region and architecture on the Lambda pricing page. Arm (Graviton) functions are about 20% cheaper per GB-second.
- The $0.20 per million request charge is the same at every memory size, so it's left out.
Pick the setting
Look for the row where duration stops dropping sharply. That's usually your answer. If two rows cost about the same, take the faster one.
If you want this on autopilot, the open source AWS Lambda Power Tuning tool runs the same idea through Step Functions and draws a chart for you. AWS Compute Optimizer will also suggest memory sizes for functions that have enough history.
Five minutes of testing, one number changed, and your function gets faster for roughly the same bill. That's a good trade.
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article