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.
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.
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.
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
)
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
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:
fonts
directory.
fonts
directory add the TTF or your font.
/var/task/fonts
.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>
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.