Skip to content

hackertalk/math-renderer

Repository files navigation

Math Renderer for AWS Lambda

Query parameters:

  • inline: inline latex string
  • block: block latex string
  • color: color of the rendered image
  • alternativeColor: alternative color of the rendered image (dark mode)

Deploying the function to AWS Lambda

esbuild produces one JavaScript file with all dependencies. This is the only file that you need to add to the .zip archive.

Steps:

  • Create a new Lambda function on aws console, function name: math-renderer
  • Run yarn install & yarn build, upload dist/index.zip file to lambda function
  • Set the handler to index.handler
  • Set the runtime to Node.js 18.x
  • Select configuration tab, create a function URL

Done!

Use AWS Codebuild to build and deploy

You can use AWS Codebuild to build and deploy the function.

  • Create a new Codebuild project, project name: math-renderer
  • Set the source to https://github.com/hackertalk/math-renderer
  • Set the buildspec to buildspec.yml
  • Set the environment to Managed image, operating system: Ubuntu, runtime: Standard, image: aws/codebuild/standard:6.0, environment type: Linux
  • Select New service role, role name: codebuild-math-renderer-service-role

After creating the project, open IAM console, find the role codebuild-math-renderer-service-role, attach the policy AWSLambdaFullAccess to it. Or you can create a new policy and attach it to the role.

Required permissions: actions: lambda:UpdateFunctionCode, lambda:UpdateFunctionConfiguration Resource: arn:aws:lambda:my-region:my-aws-account-id:function:math-renderer

Use AWS Lambda@Edge and CloudFront to render math

  • Change runtime handler to index.edgeHandler on aws console
  • Create a new CloudFront distribution for the Lambda@Edge function
  • Set the origin to function url
  • Create a new cache policy, cache all query strings (best practice) and select the cache policy for the distribution

Testing the function

References

  1. Nodejs + Serverless 实现 LaTeX 公式渲染服务
  2. typescript package
  3. Lambda@Edge example functions