Guide · Serverless & AWS

AWS Lambda cost optimization: Node.js, Rust and arm64

Lambda bills are mostly memory × duration. The cheapest wins are architectural: run on arm64 (Graviton), right-size memory, ship smaller bundles, and rewrite only the hottest functions in Rust.

How Lambda actually bills you

You pay per request plus GB-seconds: the memory you configure multiplied by how long each invocation runs, billed per millisecond. So there are three levers: price per GB-second (architecture), memory (configuration), and duration (your code).

Five wins, in order of effort

  • Switch to arm64 (Graviton). It's cheaper per GB-second than x86 and often faster. For pure JavaScript it's a one-line change.
  • Right-size memory. More memory also means more CPU, so sometimes more memory is cheaper. Measure with the open-source AWS Lambda Power Tuning tool.
  • Ship smaller bundles. Bundle and tree-shake with esbuild and cut dependencies. Smaller packages mean faster cold starts.
  • Do setup once. Create SDK clients and database connections outside the handler so warm invocations reuse them.
  • Don't pay to wait. Batch work from SQS, and move long waits to Step Functions or async events instead of sleeping inside a function.
Node.js · reuse clients across invocations
import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";

// created once per container, reused by every warm invocation
const db = new DynamoDBClient({});

export const handler = async (event) => {
  const res = await db.send(new GetItemCommand({
    TableName: process.env.TABLE,
    Key: { id: { S: event.id } },
  }));
  return res.Item ?? null;
};

Rust on arm64 for the hot paths

Most functions are I/O-bound glue, and Node.js is perfect for them. But a few functions usually dominate the bill: CPU-heavy parsing, transformations, image or data processing, high-traffic APIs. Those are where Rust shines: a small native binary, fast cold starts, low memory, and predictable performance.

Hybrid Node.js and Rust Lambda architecture on arm64API Gateway routes requests to Node.js Lambdas for glue code and to a Rust Lambda on arm64 for the CPU-heavy hot path, with SQS batching background work.All functions on arm64 (Graviton)API GatewayHTTPNode.js λarm64 · glueRust λhot pathSQSbatchingStorageDynamoDB · S3
Keep Node.js where it's productive; move only the expensive functions to Rust.
Rust · src/main.rs
use lambda_runtime::{run, service_fn, Error, LambdaEvent};
use serde_json::{json, Value};

async fn handler(event: LambdaEvent<Value>) -> Result<Value, Error> {
    let name = event.payload["name"].as_str().unwrap_or("world");
    Ok(json!({ "message": format!("Hello, {name}!") }))
}

#[tokio::main]
async fn main() -> Result<(), Error> {
    run(service_fn(handler)).await
}
Shell · build and deploy for arm64
# cargo-lambda cross-compiles for Lambda's OS-only runtime (provided.al2023)
cargo lambda build --release --arm64
cargo lambda deploy my-function

Because Rust uses far less memory, you can often lower the memory setting at the same time. That's a second saving on top of the shorter duration.

Measure before and after

Optimizations can backfire. Provisioned concurrency, for example, removes cold starts but bills continuously. Track cost per 1,000 invocations and p95/p99 duration per function, change one thing at a time, and keep what the numbers support.

FAQ

Is Lambda on arm64 cheaper than x86?

Yes. AWS prices arm64 (Graviton) Lambda duration lower than x86, and many workloads also run faster on it, so the saving compounds. Measure your own functions, but for most Node.js and Rust code it's the easiest win available.

Does Node.js code run on arm64 Lambda without changes?

Pure JavaScript does: you switch the architecture setting and redeploy. Packages with native binaries (image processing, some database drivers) need arm64 builds, so test those.

When is Rust worth it on Lambda?

For CPU-heavy functions, high-invocation hot paths and cold-start-sensitive APIs. Rust compiles to a small native binary with fast cold starts and low memory use, which often lets you lower the memory setting too. For I/O-bound glue code, Node.js is usually fine.

Can optimizing cold starts increase my bill?

Yes. Provisioned concurrency removes cold starts but you pay for it continuously. Calculate the cost before enabling it; often a smaller bundle or a Rust rewrite of one function solves the problem more cheaply.