Tools,

PaddleOCR on AWS Lambda, with one memory fix

98.4%

of scan words in a 3,008 MB function

Get the Dockerfile

Full-size pages run out of memory

By default the text finder sees the whole page, here 1920 x about 3,000 px.

Peak memory, 2 threads
Text finder Function Peak
Full size 3,008 MB Killed on page 1
Full size 10,240 MB 4,525 MB, page 1
Capped at 960 px 3,008 MB 1,432 MB, 10 pages

The fix is two settings, already in app.py: text_det_limit_type="max" and text_det_limit_side_len=960.

Capped, the text finder sees each page at about a third of its size. The recognizer still reads the lines from the full page. Page 1 read 201 of 202 words both ways.

More words than Tesseract, much slower

True words found on 10 pages, of 1,955
Run Words Share
PaddleOCR 3.7 1,924 98.4%
Our Actor's code 1,880 96.2%
Tesseract, same test 1,879 96.1%

Tesseract, in the same Lambda-like container, read all 10 pages in 7.3 s at 1,769 MB. More in our Tesseract Lambda guide.

Memory and threads set the speed

Lambda gives CPU in step with memory. Page 1, capped, as the first call:

Function Threads Page 1
1,024 MB 2 45.0 s
1,536 MB 2 28.1 s
3,008 MB 2 13.5 s
3,008 MB 3 31.0 s
3,008 MB 4 or 10 Segmentation fault
7,076 MB 4 9.2 s

The Dockerfile bakes the models in

Save both files in one folder.

Dockerfile app.py

Show the Dockerfile (12 lines)
# PaddleOCR 3.7 on AWS Lambda, arm64 (Graviton), CPU only (brasspage.com/paddleocr-lambda/, Nov 26, 2026)
FROM public.ecr.aws/lambda/python:3.13
ENV PIP_NO_CACHE_DIR=1 PADDLE_PDX_DISABLE_MODEL_SOURCE_CHECK=True PADDLE_PDX_CACHE_HOME=/opt/paddlex
# PyPI has no Linux arm64 wheel of paddlepaddle 3.3.1. Paddle's own index has one.
RUN pip install paddlepaddle==3.3.1 -i https://www.paddlepaddle.org.cn/packages/stable/cpu/ \
 && pip install paddleocr==3.7.0
# PaddleX needs the full OpenCV wheel, and that needs libGL.so.1. The Lambda image has none.
RUN dnf install -y mesa-libGL && dnf clean all
COPY app.py ${LAMBDA_TASK_ROOT}/
# The models go into the image now, so a cold start downloads nothing.
RUN python -c "import app"
CMD ["app.handler"]
Show app.py (43 lines)
"""PaddleOCR 3.7 on AWS Lambda, one page or a small PDF per call (brasspage.com/paddleocr-lambda/, Nov 26, 2026)."""

import base64
import json
import os
import tempfile

from paddleocr import PaddleOCR

SIZE = os.environ.get("OCR_SIZE", "medium")  # PP-OCRv6 medium, small or tiny
opts = {"cpu_threads": int(os.environ.get("OCR_THREADS", "2"))}  # PaddleOCR's default is 10
if SIZE != "medium":
    opts["text_detection_model_name"] = f"PP-OCRv6_{SIZE}_det"
    opts["text_recognition_model_name"] = f"PP-OCRv6_{SIZE}_rec"
# The text finder sees the page at most 960 px on its long side. 0 = PaddleOCR's default, the page at full
# size (up to 4000 px), which needed 4.5 GB for one 1920 px page and was killed at 3,008 MB.
DET_MAX = int(os.environ.get("OCR_DET_MAX", "960"))
if DET_MAX:
    opts["text_det_limit_type"] = "max"
    opts["text_det_limit_side_len"] = DET_MAX
# Loaded once per container (the cold start), then reused by every warm call.
ocr = PaddleOCR(
    use_doc_orientation_classify=False,
    use_doc_unwarping=False,
    use_textline_orientation=False,
    **opts,
)


def handler(event, context):
    body = event.get("body") or ""
    data = base64.b64decode(body) if event.get("isBase64Encoded") else body.encode()
    suffix = ".pdf" if data[:4] == b"%PDF" else ".png"
    with tempfile.NamedTemporaryFile(suffix=suffix, dir="/tmp") as f:
        f.write(data)
        f.flush()
        pages = [r["rec_texts"] for r in ocr.predict(f.name)]
    text = "\n\n".join("\n".join(lines) for lines in pages)
    return {
        "statusCode": 200,
        "headers": {"content-type": "application/json"},
        "body": json.dumps({"pages": len(pages), "text": text}),
    }

Run it in the Lambda emulator

The base image has the Lambda emulator inside:

docker build --platform linux/arm64 --provenance=false -t paddle-lambda .
docker run --rm -p 9000:8080 --memory 3008m --memory-swap 3008m --cpus 1.7 paddle-lambda

Then send one page and get its text:

python3 -c 'import base64, json, sys; print(json.dumps({"body": base64.b64encode(open(sys.argv[1], "rb").read()).decode(), "isBase64Encoded": True}))' page.jpg \
  | curl -s --data-binary @- http://localhost:9000/2015-03-31/functions/function/invocations \
  | jq -r '.body | fromjson | .text' > page.txt

Image: 642 MB compressed. Clean build: about 70 s.

Three install errors, fixed in the Dockerfile

Error Fix
ImportError: libGL.so.1: cannot open shared object file dnf install -y mesa-libGL
Headless OpenCV: DependencyError: `OCR` requires additional dependencies. Keep opencv-contrib-python. PaddleX checks for that package name, not for cv2.
PyPI has no Linux arm64 wheel of paddlepaddle 3.3.1 Paddle's own index, as in the Dockerfile.

Three traps when it runs

Trap Fix
Models not in the image: a 114.8 s cold start in the emulator. A real Lambda stops Init at 10 s and retries it at the first call. Bake them in at build time: RUN python -c "import app"
4 or 10 threads on 2 cores: Segmentation fault on call 1. Keep the threads at the core count. app.py uses 2.
The 10-page PDF, 8.65 MB as base64, is over the documented 6 MB. The emulator cut it, so the JSON broke: Runtime.UnmarshalError. One page per call, or the PDF in S3 with an S3 handler, as on Tesseract on Lambda.

No image to build: our Actor

Our Apify Actor, PDF to Markdown OCR, reads scanned PDFs in the cloud with one HTTP call.

Tested in the Lambda emulator, not on AWS

No AWS account: the Lambda image ran in Docker on an Apple M5 Max, with Lambda-like limits.

Not tested: a real Lambda, amd64, the full-size default on all 10 pages, the tiny and small models.

How we ran it
  • Base image: public.ecr.aws/lambda/python:3.13 for arm64, created Nov 24, 2026.
  • Amazon Linux 2023, Python 3.13.18.
  • paddlepaddle 3.3.1 from Paddle's index, paddleocr 3.7.0.
  • paddlex 3.7.2, opencv-contrib-python 4.10.0.84.
  • Models: PP-OCRv6_medium_det and PP-OCRv6_medium_rec, the default, in /opt/paddlex.
  • Limits: a read-only root, a fresh folder as /tmp, a non-root user, memory with no swap, at most 2 cores (4 at 7,076 MB and up).
  • Docker had 7.7 GiB, so the 10,240 MB limit was above it. The 4,525 MB peak fits.
  • Our model gives 4 cores at 10,240 MB. The AWS rule gives 5.8 vCPU.
  • CPU: memory over 1,769 MB, one vCPU in the AWS docs. A model, not a Lambda measurement.
  • Calls: Function URL events, one page JPEG per call, in page order, in one container. Call 1 is the cold start.
  • With the models in the image, the emulator's INIT (import and model load) took 0.87 s to 1.64 s. The image was already on the local disk.
  • A 2-page PDF in one call read 371 of 381 words.
  • mesa-libGL brings 26 packages, among them llvm-libs at 110 MB.
  • The models sat in a read-only folder at run time, and that was fine. Paddle wrote its cache under /tmp.
  • The image holds 1,873 MiB of files. AWS allows 10 GB for a Lambda image.
  • Without baked-in models, PaddleX tried Hugging Face, aistudio and ModelScope, then paddle-model-ecology.bj.bcebos.com, which worked.
  • The emulator reads only the first 6 MiB and 100 bytes of a request and gave no error of its own. AWS documents 6 MB, but not the error a real Lambda returns.
  • Scan: the 10 proofread en.wikisource pages of our n8n guide. Score: true words found in order.
  • Other jobs used the Mac during the runs.
  • Scripts and results stay in our private repo, in research/bench/paddleocr-lambda-2026-11-26/.

Questions people ask

Can PaddleOCR run on AWS Lambda?

Yes, as a container image. In the Lambda base image for Python 3.13, on arm64, it read 98.4% of our scan words in a 3,008 MB function. We tested it in the Lambda emulator, not on AWS.

How much memory does PaddleOCR need on Lambda?

With the text finder at full size, one scan page took 4,525 MB. With the text finder capped, 10 pages peaked at 1,432 MB.

How do I fix libGL.so.1 for PaddleOCR on Lambda?

Install it in the image with dnf install -y mesa-libGL. The headless OpenCV wheel does not help: PaddleX checks for the opencv-contrib-python package by name.

Can I run a PaddleOCR server on Lambda?

app.py takes the event a Function URL sends, one page per call, and returns the text as JSON. Send the page as image/jpeg, so that Lambda base64-encodes it. We sent that event in the emulator. We did not test a real Function URL.

How long is a PaddleOCR cold start on Lambda?

With the models in the image, the first call took 13.5 s in the emulator, about a second of it Init. Without them, the load took 114.8 s, past the 10 s Init limit of a real Lambda. We did not measure the image pull.