Worked example

PySpark and Delta Lake on AWS Lambda

Spark runs on AWS Lambda when it is packed in a container image. This page follows one real pipeline from raw Parquet in S3 to Delta tables in the Glue Data Catalog, read by Athena. It shows the Terraform, the run timings, the cold start, what 91 runs cost, and the limits that tell you when to move a step to Fargate.

01 The pieces

What runs where

  • Raw data in S3. Parquet files under seed/y=/m=/d=/. In this example there are 3 files, 8.5 KB in total: a small sample of New York taxi trips.
  • Transforms in Spark SQL (PySpark works the same way). Each transform is a Terraform module in the pipeline’s main.tf.
  • One Lambda function per pipeline, from a container image: Python 3.12, Apache Spark 4.0.2 and Delta Lake 4.0.0, arm64, 3008 MB of memory, a 900-second timeout and 10 GB of /tmp. It runs the transforms of a pipeline in order in one Spark session.
  • A Step Functions state machine starts the Lambda function on a schedule, or when new files arrive in S3, or when an upstream pipeline succeeds.
  • Delta Lake tables in S3, registered in the Glue Data Catalog with their real columns, so Athena reads them with no crawler.
  • Run history (runs, node runs, row counts, timings) as Delta tables in the same catalog.

There is no cluster and nothing runs between runs. Clavesa writes all of it as Terraform in your repository and deploys it into your own AWS account.

02 The pipeline

Two SQL transforms, as Terraform

This is the pipeline’s main.tf, shortened: the module source, bucket and catalog lines are left out. trips reads the raw Parquet files from S3, from the partition 2026/06/02 on; revenue_by_payment reads trips.

taxi/main.tf
module "trips" {
  name     = "trips"
  language = "sql"
  sql      = "SELECT pickup_ts, payment_type, fare_amount, trip_distance FROM raw_trips"
  source_inputs = { raw_trips = {
    bucket = "smoke2-clavesa", prefix = "seed/", format = "parquet",
    partitions = ["y", "m", "d"], start_from = "2026/06/02"
  } }
  output_definitions = { default = { stats = true } }
}

module "revenue_by_payment" {
  name     = "revenue_by_payment"
  language = "sql"
  sql      = "SELECT payment_type, SUM(fare_amount) AS revenue, COUNT(*) AS trip_count FROM trips GROUP BY payment_type"
  inputs   = { trips = module.trips.outputs["default"] }
}

The schedule is one variable, trigger_schedule = "rate(1 day)". The same SQL runs on your laptop with clavesa pipeline run taxi in the same container image, and produces the same Delta tables there.

zsh
$ AWS_PROFILE=personal clavesa deploy
# builds and pushes the runner image, applies the workspace and the pipeline

03 Timings

The cold start and the warm runs

Real output, Helsinki time, from 10 consecutive runs. The first run of a series starts a new Lambda container and a new Spark session (SPARK 30.1 s). In the later runs Spark is ready in under a second when the warm session is reused, and in about 10 s when it is not.

clavesa pipeline timings taxi
STARTED              STATUS     TOTAL   SPARK  READS  TRANSFORM  WRITES  STATS  OTHER
2026-11-16 23:24:10  SUCCEEDED  1m 23s  30.1s  5.5s   0.2s       25.4s   13.4s  9.1s
2026-11-16 23:25:36  SUCCEEDED  20.3s   0.0s   0.2s   0.0s       14.5s   0.0s   5.5s
2026-11-16 23:26:24  SUCCEEDED  44.0s   10.7s  4.0s   0.1s       10.3s   9.7s   9.3s
2026-11-16 23:27:13  SUCCEEDED  18.3s   0.1s   0.5s   0.0s       8.9s    0.0s   8.8s
2026-11-16 23:28:24  SUCCEEDED  39.7s   10.7s  3.4s   0.1s       12.4s   7.3s   5.9s
2026-11-16 23:29:06  SUCCEEDED  11.6s   0.0s   0.4s   0.0s       6.7s    0.0s   4.5s
2026-11-16 23:29:50  SUCCEEDED  7.6s    0.1s   0.4s   0.0s       4.1s    0.0s   3.0s
2026-11-16 23:30:21  SUCCEEDED  27.7s   9.9s   2.9s   0.1s       7.3s    0.0s   7.6s
2026-11-16 23:31:28  SUCCEEDED  21.2s   0.1s   0.5s   0.0s       7.8s    7.7s   5.2s
2026-11-16 23:31:50  SUCCEEDED  15.9s   0.0s   0.2s   0.0s       10.6s   0.0s   5.1s
  • The transform itself is not the cost. With 8.5 KB of input, TRANSFORM is under 0.3 s. Most of the time is writing the Delta commit (WRITES), table statistics (STATS) and Spark start-up.
  • So these numbers are a floor. They are what a Spark run on Lambda costs before the data adds anything. A larger input adds read and transform time on top.
  • The node rows of the newest run: trips 24.1 s, 201 rows out; revenue_by_payment 17.8 s, 4 rows out.

04 The tables

Delta Lake in Glue, read by Athena

The output is a Delta table in S3 whose Glue entry has the real columns and spark.sql.sources.provider = delta. Athena reads it directly:

Athena, workgroup clavesa-smoke2
SELECT payment_type, ROUND(revenue, 2) AS revenue, trip_count
FROM clavesa_smoke2__taxi.revenue_by_payment
ORDER BY revenue DESC

payment_type  revenue  trip_count
credit_card   2723.87  57
dispute       2445.55  50
cash          2149.11  54
no_charge     1435.89  40

-- 197 bytes scanned, 1.7 s

The run history is in the same catalog, in clavesa_smoke2_system__pipelines(runs, node_runs), so the same SQL answers “how long did each step take last week”.

05 Cost

What 91 runs cost

The 91 runs are the last 30 days of this pipeline (measured 2026-11-17). They were started by the release test, not by the daily schedule, which was paused. Prices are eu-north-1 list prices, before the free tier.

  • Lambda: $0.115. CloudWatch says 91 invocations, 2,939 seconds in total (average 32.3 s, longest 139 s). At 3008 MB that is 8,634 GB-seconds, at $0.0000133334 per GB-second for arm64.
  • Clavesa’s own estimate: $0.125 for the same runs (clavesa workspace summary), from the node timings. It agrees with the bill within 10%.
  • Step Functions: under $0.01. One state transition per run, at $0.025 per 1,000.
  • Athena, Glue and S3: under $0.01 at this size. Athena bills a 10 MB minimum per query, $0.00005.
  • Per run: about $0.0013 on average by the bill; clavesa estimates $0.0021 for the newest run (45 s).
  • A daily schedule: 30 runs at the average would be about $0.04 a month of Lambda. The Lambda free tier (400,000 GB-seconds a month) covers that many times over.
clavesa workspace summary
smoke2 (cloud warehouse, eu-north-1): OK
  note: taxi: schedule paused since 9.11 09:21
Last run:   taxi succeeded at 2026-11-17 00:55:09 (45s)
Runs, 14 d: 91 succeeded, 0 failed
Freshness:  no freshness_sla declared
Cost, 30 d: $0.1247, $713.72 per billion records

The cost per billion records is high here because each run has very little data: the fixed start-up and commit time is spread over about 1,900 records per run. More data adds read and transform time to this floor. Another pipeline built with clavesa is the web analytics example, which turns this site’s own CloudFront logs into sessions.

06 Limits

When Lambda is the wrong place

  • 15 minutes. A Lambda run stops at 900 seconds. A step that can take longer must not run on Lambda.
  • 10 GB of memory and about 6 vCPU at most, and 10 GB of /tmp. Spark runs in local mode inside one function, so a large shuffle or join does not fit.
  • Move one step to Fargate with one attribute, compute = "fargate", on that transform. The SQL does not change. Fargate takes one to two minutes to start a task, so keep the small steps on Lambda.
  • EMR Serverless is planned, not built. Clavesa accepts compute = "emr-serverless" in validation and refuses it at deploy.
  • Delta Lake, not Iceberg. Clavesa writes Delta tables. If you need Iceberg, this is not the tool.
  • If you want a library, not a platform: for small files, plain Python with delta-rs or DuckDB on Lambda starts faster than Spark. Spark is worth its start-up when the same code must also run on Fargate and on your laptop.

Why one Spark engine everywhere: ADR-012. More examples: the cookbook, and one Databricks step on AWS compute.

Try it on your laptop first

clavesa workspace init demo and clavesa ui run the same Spark image locally with no AWS account. Deploy when the tables look right.