AWS Builder Center
One Font File Instead of Four: Bundling Japanese Type in a Lambda Container Without a Compiler in Sight

One Font File Instead of Four: Bundling Japanese Type in a Lambda Container Without a Compiler in Sight

Shipping Japanese text from a Lambda container usually means picking a base image and a font strategy. Here is why starting from AWS's own Lambda base image, and a single variable font file, kept this one dependency-free and under 340 MB.

Cloud Engineer
I built a small tool that overlays Niconico-style scrolling comments onto PowerPoint slides. The rendering happens inside a container Lambda: Pillow draws each animated GIF frame, and most of the text in it is Japanese. Getting Japanese glyphs to render correctly inside a Lambda container turned into two small decisions that, looking back, mattered more than anything else in the Dockerfile.
FROM public.ecr.aws/lambda/python:3.12

RUN microdnf install -y tar gzip || true

COPY requirements.txt ${LAMBDA_TASK_ROOT}/
RUN pip install --no-cache-dir -r ${LAMBDA_TASK_ROOT}/requirements.txt

RUN mkdir -p ${LAMBDA_TASK_ROOT}/fonts && \
python -c "import urllib.request; urllib.request.urlretrieve('https://raw.githubusercontent.com/google/fonts/main/ofl/notosansjp/NotoSansJP%5Bwght%5D.ttf', '${LAMBDA_TASK_ROOT}/fonts/NotoSansJP-VF.ttf')"

COPY app.py ${LAMBDA_TASK_ROOT}/
CMD ["app.handler"]
Four lines, no  apt-get , no compiler. That's the whole image.

Decision one: start from AWS's own base image, not a generic one

On a different Lambda container I built around the same time, one that needed Node.js for an MCP server, I started from  python:3.11-slim  and installed everything myself, including a Lambda Runtime Interface Client compiled from source, which dragged in  gcc ,  g++ , and a handful of build-time-only packages that never run at invocation. That is the standard path when you need a container to behave like a Lambda function but the image doesn't start out as one.
 public.ecr.aws/lambda/python:3.12  skips that problem by not existing on the other side of it. It's Amazon Linux 2023 with the Runtime Interface Client already built in, so  CMD ["app.handler"]  just works without me writing an entrypoint or compiling anything. The only native dependency I needed,  tar / gzip  for unpacking a font download, is a one-line  microdnf install . No compiler ever enters the image. If your function is plain Python with dependencies that ship prebuilt wheels, which Pillow and python-pptx both do, the official base image is less work in every direction, not just a smaller image.

Decision two: one font file, not four

Japanese type usually means bundling more than a Latin font would need, and my comments render in more than one weight depending on how loud a comment is supposed to look. The obvious approach is to bundle a Regular file and a Bold file, doubling the font payload for a feature that's really just "make some text heavier."
Instead the image bundles exactly one file,  NotoSansJP[wght].ttf , Google Fonts' variable version of Noto Sans JP, and picks the weight at render time:
font = ImageFont.truetype(FONT_PATH, font_size)
font.set_variation_by_axes([700]) # Bold, from the same file
A variable font stores its entire weight range, light through black, as interpolation data inside one file, and Pillow (via FreeType) can select any point on that axis at runtime instead of you shipping a separate static file per weight. I only ever need two points, 400 and 700, but the mechanism doesn't care how many I'd wanted. Adding a third weight later costs zero additional bytes in the image, which is exactly backwards from the static-font approach.


What it comes to

The image lands at about 337 MB, compressed, in ECR. For comparison, two of my other container Lambdas that are pure Python with ordinary dependencies and no bundled fonts sit around 198 MB, so the font and the extra libraries (Pillow's own binary dependencies, python-pptx) account for roughly the other third. None of it is compiler residue, because there was never a compiler in the image to leave residue behind.
I did not set out to write a container-optimization post here. I set out to get Japanese text rendering correctly in an animated GIF, and the two decisions that made that easy, matching the base image to what Lambda already needs and reaching for a variable font instead of a font family, are the kind of thing that look obvious in hindsight and invisible in advance. The Dockerfile that resulted has no build stage to optimize, because there was nothing in it to build.
Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article