Include custom fonts in AWS Lambda

喜欢而已 提交于 2020-05-16 05:53:45

问题


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?


回答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.




回答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




回答3:


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.




回答4:


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)



来源:https://stackoverflow.com/questions/46486261/include-custom-fonts-in-aws-lambda

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!