'Include custom fonts in AWS Lambda

Does Anyone know something like ebextensions[2] in EC2 for AWS Lambda?

The goal is to install custom fonts in the AWS Lambda execution environment.

There are many ways to provide libraries and tools with fonts but the easiest way would be to include them via OS.

Also asked in response on AWS forum: https://forums.aws.amazon.com/thread.jspa?messageID=807139&#807139

[2]How I install specific fonts on my AWS EC2 instance?



Solution 1:[1]

The official AWS response on the forum post is still correct.

Currently, it is not possible to customize Lambda environment. If you want additional packages you can build on Amazon Linux and put them into the zip file you upload.

That's the extent to which you can "install" anything in the Lambda environment.

the easiest way would be to include them via OS.

Arguably so, but that's simply not how Lambda works.

Solution 2:[2]

Here's what I just got to work for custom fonts on AWS Lambda with pandoc/xelatex.

I created a fonts directory in my project and placed all of my fonts there. Also in that directory I created a fonts.conf file that looks like this:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/var/task/fonts/</dir>
  <cachedir>/tmp/fonts-cache/</cachedir>
  <config></config>
</fontconfig>

And then in my (node.js based) handler function before shelling out to call pandoc I set an ENV var to tell fontconfig where to find the fonts.

process.env.FONTCONFIG_PATH='/var/task/fonts'

After doing that I can refer to a font, like Bitter, in my template by name (just Bitter) and then pandoc/xelatex/fontconfig/whatever knows which version of the font to use (like Bitter-Bold.otf vs Bitter-Italic.otf) based on the styling that any bit of text is supposed to have.

I figured this out based on the tips in this project for getting RSVG to work with custom fonts on Lambda: https://github.com/claudiajs/rsvg-convert-aws-lambda-binary/blob/master/README.md#using-custom-fonts

Solution 3:[3]

A lot of the answer on this subject of using fonts on Lambda's were a bit incomplete.

My scenario required using a custom font in conjunction with Imagemagick. I checked out out this branch with Imagemagick and Freetype support and worked through the README. The key for my use case is the lambda or lambda layer used in the function needed freetype support to access my fonts. I'm using a TTF.

After deploying the lambda layer, in my Lambda function's directory I did the following:

  • At the root of my lambda create a fonts directory.
    • In the fonts directory add the TTF or your font.
      • I'm using serverless framework so this directory once deployed will be located at /var/task/fonts.
    • Also in the fonts directory include the following fonts.conf.
<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
  <dir>/var/task/fonts/</dir>
  <cachedir>/tmp/fonts-cache/</cachedir>
  <config></config>
</fontconfig>
  • Finally, in your serverless.yml add the following directory so that your fonts and fonts.conf will be included in the lambda.
package:
    include:
        - fonts/**

Because freetype is now accessible in the lambda layer any fonts in the fonts directory will be accessible. Could have also dynamically downloaded fonts needed, but I decided to choose include in my lambda function.

Solution 4:[4]

Package your assets along with the code or have it fetch them from s3. This is the way we generate PDFs with custom fonts with Lambda.

Things like the Serverless Framework will do this for you automatically (uploading code + deps assets).

When you deploy it will create a zip file with your code, dependencies and anything else you have in the folder. Then it will automatically upload it to S3 and deploy it with the help of CloudFormation.

Solution 5:[5]

I followed the instructions on this gist and it worked a charm (although for me in the "Configuring fontconfig" and "Installing and caching fonts" sections the /tmp/…/fontconfig seemed to really mean /var/task/<MyLambda>/headless-chrome/fontconfig)

Solution 6:[6]

Lambda extracts the layer contents into the /opt directory when setting up the execution environment for the function. So, fonts.conf should have /opt/.fonts under and ttf fonts should be placed there.

In addition Lambda needs to access the fontconfig library like libfontconfig.so, libexpat.so and libfreetype.so. The files can be found in phantom-lambda-fontconfig-pack.

It seems that it works in Node.JS lambda. In case of using Python Lambda, it still does not work.

At last, I created a Lambda container image based on the dockerfile in rchauhan9/image-scraper-lambda-container and then added the following code after "RUN apk add chromium..."

ENV NOTO_TC="https://github.com/googlefonts/noto-cjk/raw/main/Sans/Variable/OTF/NotoSansCJKhk-VF.otf"
ENV NOTO_SC="https://github.com/googlefonts/noto-cjk/raw/main/Sans/Variable/OTF/NotoSansCJKsc-VF.otf"
ENV NOTO_JP="https://github.com/googlefonts/noto-cjk/raw/main/Sans/Variable/OTF/NotoSansCJKjp-VF.otf"
ENV NOTO_KR="https://github.com/googlefonts/noto-cjk/raw/main/Sans/Variable/OTF/NotoSansCJKkr-VF.otf"
RUN apk --no-cache add \
      fontconfig \
      wget \
      && mkdir -p /usr/share/fonts \
      && wget -q "${NOTO_TC}" -P /usr/share/fonts \
      && wget -q "${NOTO_SC}" -P /usr/share/fonts \
      && wget -q "${NOTO_JP}" -P /usr/share/fonts \
      && wget -q "${NOTO_KR}" -P /usr/share/fonts \
      && fc-cache -fv

ENV LANG="C.UTF-8"

It works with CJK fonts. Reference: Section "Building a Custom Image for Python" in New for AWS Lambda – Container Image Support

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Michael - sqlbot
Solution 2 Jeremy Green
Solution 3 Zelf
Solution 4 Eduardo Romero
Solution 5 Andrew
Solution 6 Cristik