Include custom fonts in AWS Lambda

11,991

Solution 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

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

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

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.

Share:
11,991
will
Author by

will

Updated on June 26, 2022

Comments