Coder Social home page Coder Social logo

node-canvas-lambda's Introduction

node-canvas-lambda

A node-canvas (and chart.js) layer for AWS Lambda

Build

Build new layers using the Dockerfile and copy them to the build folder. Be sure to have Docker installed then run the following command:

make build

The default version of Nodejs is 16. To build for a different node version set the NODE_VERSION:

make build NODE_VERSION=14

Publish

Upload the layers to AWS into your default region. This will build the layers if they have not already been built.

make publish

Test

A lambda image (e.g. public.ecr.aws/lambda/nodejs:16-arm64 ) can be used to test the layers by loading the layers and running a simple lambda handler that uses canvas.

The following command will unzip the layers and mount the layer folders into to the /opt/ folder of the lambda container and then run the test.js handler:

make test

If the test worked correctly the output should be a data URI. e.g.: "data:image/png;base64,iVBORw0KGgoAAAA.... You can copy the URI and paste it into our browser's url bar to see the image.

Architecture

The default architecture for these layers are ARM. If you're function uses amd64 architecute you shouldn't have any issues. If you're using x86-64 you'll need to checkout the x86-64 branch and run make build.

Debug

The build image can be used to debug any issues with the layers.

Use make debug to start an interactive bash session in the container where you can use ldd or lddtree to examine the canvas.node binary:

ldd nodejs/node_modules/canvas/build/Release/canvas.node

node-canvas-lambda's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

node-canvas-lambda's Issues

Error running docker command

I'm getting an error when running the docker command from the README.md

 docker run -d --rm --mount type=bind,source=$(pwd),target=/out canvas-layers /out/layers.zip /root/layers 
d24efe42e9ca297d8c85d4c44a0e6a9c7ddec3627e93a53d4f7b5f0552b11b42
docker: Error response from daemon: OCI runtime create failed: container_linux.go:349: starting container process caused "exec: \"/out/layers.zip\": stat /out/layers.zip: no such file or directory": unknown.

Fixed one error, gave another error

Hello there,

I've added this layer to my canvas, but now I'm getting this error:

internal/modules/cjs/loader.js:717
return process.dlopen(module, path.toNamespacedPath(filename));
Error: Error loading shared library libresolv.so.2: No such file or directory (needed by /home/container/node_modules/canvas/build/Release/librsvg-2.so.2)
at Object.Module._extensions..node (internal/modules/cjs/loader.js:717:18)
at Module.load (internal/modules/cjs/loader.js:598:32)
at tryModuleLoad (internal/modules/cjs/loader.js:537:12)
at Function.Module._load (internal/modules/cjs/loader.js:529:3)
at Module.require (internal/modules/cjs/loader.js:636:17)
at require (internal/modules/cjs/helpers.js:20:18)
at Object.<anonymous> (/home/container/node_modules/canvas/lib/bindings.js:3:18)
at Module._compile (internal/modules/cjs/loader.js:688:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10)
at Module.load (internal/modules/cjs/loader.js:598:32)

How to fix it?

I'm using Ubuntu 16.04 with the latest versions of all the things. Installed canvas with the npm install canvas command and added the files.

Kind regards,

Stijn

Incorrect Amazon Linux in Dockerfile

In the Dockerfile, "FROM amazonlinux:latest" now refers to Amazon Linux 2023, and the Node.JS v16 installation fails on this version. Changing that line to "FROM amazonlinux:2" corrects the issue.

Fontconfig error: Cannot load default config file

Hello! This is getting me a little closer to getting node-canvas running, however, I am running into Fontconfig error: Cannot load default config file now. Anyone else faced this or know if a fix?

use serverless.yml to deploy the layer won't work

Hi there,

I installed the layer to my lambda function succeed via the AWS console.
While by using the (AWS) serverless yml file to deploy the lambda, it will still get the error.

Do you have any idea?

Thanks

{
    "errorType": "Runtime.ImportModuleError",
    "errorMessage": "Error: Cannot find module 'canvas'",
    "stack": [
        "Runtime.ImportModuleError: Error: Cannot find module 'canvas'",
        "    at _loadUserApp (/var/runtime/UserFunction.js:100:13)",
        "    at Object.module.exports.load (/var/runtime/UserFunction.js:140:17)",
        "    at Object.<anonymous> (/var/runtime/index.js:45:30)",
        "    at Module._compile (internal/modules/cjs/loader.js:778:30)",
        "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)",
        "    at Module.load (internal/modules/cjs/loader.js:653:32)",
        "    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)",
        "    at Function.Module._load (internal/modules/cjs/loader.js:585:3)",
        "    at Function.Module.runMain (internal/modules/cjs/loader.js:831:12)",
        "    at startup (internal/bootstrap/node.js:283:19)",
        "    at bootstrapNodeJSCore (internal/bootstrap/node.js:623:3)"
    ]
}

Installation sls template sample:

layers:
  canvasLib:
    path: ../layers/canvas # required, path to layer contents on disk
    name: canvasLibLayer # optional, Deployed Lambda layer name
    description: Layer for canvas # optional, Description to publish to AWS
    compatibleRuntimes:
      - nodejs10.x
  nodeCanvas:
    path: ../layers/nodeCanvas # required, path to layer contents on disk
    name: nodeCanvasLayer # optional, Deployed Lambda layer name
    description: Layer for node-canvas # optional, Description to publish to AWS
    compatibleRuntimes:
      - nodejs10.x
functions:
    layers:
      - { Ref: CanvasLibLambdaLayer }
      - { Ref: NodeCanvasLambdaLayer }

e.CanvasRenderingContext2dInit is not a function

I am getting this error after following the steps in this repo.

e.CanvasRenderingContext2dInit is not a function

The way I construct my lambda is by bundling with Parcel from the function's entry point, letting it figure out what to bundle, then zipping it and uploading.
My bundle is definitely including a copy of canvas from my local node_modules folder and is very likely the cause of this error.
That said, I am new to lambdas and I find it difficult to infer from the readme how people workaround the issue of excluding the local 'canvas' module from the zip they upload to AWS.
If I managed to finally exclude my local copy of canvas from the zip it, is there a change that const Canvas = require('canvas') won't pick the canvas module from the layer?

Missing fonts in lambda layer

Hi,
I am using the prebuilt layer that was provided.
After exporting the canvas on the lambda all text is shown with squares, locally it works fine.
Seems like the lambda OS is missing fonts. did anyone encounter this ?

code example:

ctx.font = '30px Impact'; ctx.rotate(.1); ctx.fillText("Awesome!", 50, 100); canvas.toDataURL();

output:

Latest canvas (2.8.0) fails with "canvas.node: cannot open shared object file: No such file or directory"

Thanks for providing this @jwerre it is super useful, but is there any chance you know how to get it working for the latest canvas / chartjs-node-canvas versions.

After building the layers with the latest canvas (2.8.0) and deploying to AWS I'm getting the following error from Lambda:

 {
  "errorType": "Error",
  "errorMessage": "/opt/nodejs/node_modules/canvas/build/Release/canvas.node: cannot open shared object file: No such file or directory",
  "trace": [
    "Error: /opt/nodejs/node_modules/canvas/build/Release/canvas.node: cannot open shared object file: No such file or directory",
    "    at Object.Module._extensions..node (internal/modules/cjs/loader.js:1057:18)",
    "    at Module.load (internal/modules/cjs/loader.js:863:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)",
    "    at Module.require (internal/modules/cjs/loader.js:887:19)",
    "    at require (internal/modules/cjs/helpers.js:74:18)",
    "    at Object.<anonymous> (/opt/nodejs/node_modules/canvas/lib/bindings.js:3:18)",
    "    at Module._compile (internal/modules/cjs/loader.js:999:30)",
    "    at Object.Module._extensions..js (internal/modules/cjs/loader.js:1027:10)",
    "    at Module.load (internal/modules/cjs/loader.js:863:32)",
    "    at Function.Module._load (internal/modules/cjs/loader.js:708:14)"
  ]
}

I built the layers using the build.sh and when testing I encountered the above error. My simplest test function is:

const { createCanvas } = require('canvas')

FYI: I managed to get a lambda function using the prebuilt layers and the old (pre 4.0.0) chartjs-node-canvas API (e.g. using CanvasRenderService):

const { CanvasRenderService } = require('chartjs-node-canvas');

exports.handler = async (event) => {
    const chartJSNodeCanvas = new CanvasRenderService(800, 600 ); 

    const response = {
        statusCode: 200,
        body: '<img src="' + await chartJSNodeCanvas.renderToDataURL({}) + '" />',
    };
    return response;
};

process "/bin/sh -c yum -y update && yum -y groupinstall did not complete successfully

While running make build command getting the below error

6 | # set up container
7 | >>> RUN yum -y update
8 | >>> && yum -y groupinstall "Development Tools"
9 | >>> && curl --silent --location https://rpm.nodesource.com/setup_${NODE_VERSION}.x | bash -
10 | >>> && yum install -y
11 | >>> nodejs
12 | >>> python37
13 | >>> which
14 | >>> binutils
15 | >>> sed
16 | >>> gcc-c++
17 | >>> cairo-devel
18 | >>> libjpeg-turbo-devel
19 | >>> pango-devel
20 | >>> giflib-devel
21 |

ERROR: failed to solve: process "/bin/sh -c yum -y update && yum -y groupinstall "Development Tools" && curl --silent --location https://rpm.nodesource.com/setup_${NODE_VERSION}.x | bash - && yum install -y \tnodejs \tpython37 \twhich \tbinutils \tsed \tgcc-c++ \tcairo-devel \tlibjpeg-turbo-devel \tpango-devel \tgiflib-devel" did not complete successfully: exit code: 1
make: *** [build] Error 1

Node 16 x86 error at /canvas.node

Hi, I've downloaded the latest x86 zip files from https://github.com/jwerre/node-canvas-lambda/releases/tag/v2.0.0
I then created two lambda layers, one for each zip file:

  • node16_canvas_layer_x86_64.zip
  • node16_canvas_lib64_layer_x86_64.zip

Then I attached both layers to my lambda function, and the function was built for x86 Node 16.
However after much research and attempts, I still get the error:

{
    "errorType": "Error",
    "errorMessage": "/opt/nodejs/node_modules/canvas/build/Release/canvas.node: cannot open shared object file: No such file or directory",
    "code": "ERR_DLOPEN_FAILED",
    "stack": [
        "Error: /opt/nodejs/node_modules/canvas/build/Release/canvas.node: cannot open shared object file: No such file or directory",
        "    at Object.Module._extensions..node (node:internal/modules/cjs/loader:1189:18)",
        "    at Module.load (node:internal/modules/cjs/loader:981:32)",
        "    at Function.Module._load (node:internal/modules/cjs/loader:822:12)",
        "    at Module.require (node:internal/modules/cjs/loader:1005:19)",
        "    at require (node:internal/modules/cjs/helpers:102:18)",
        "    at Object.<anonymous> (/opt/nodejs/node_modules/canvas/lib/bindings.js:3:18)",
        "    at Module._compile (node:internal/modules/cjs/loader:1105:14)",
        "    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1159:10)",
        "    at Module.load (node:internal/modules/cjs/loader:981:32)",
        "    at Function.Module._load (node:internal/modules/cjs/loader:822:12)"
    ]
}

The lambda function uses the following import:
const { createCanvas, loadImage } = require('canvas');

Any guidance would be much appreciated.

Fonts Don't Renender

Hi @jwerre,

Have you had much success with fonts on Lambda? It seems like the fonts just render as boxes. When I had this problem with node-canvas on docker, I just did a npm install canvas --rebuild-from-source and this fixed the issue. (Unfortunately, when I build node-canvas inside amazonlinux on Docker I have to include a lot more shared libraries than the 3 that this layer requires).

Pass Node 8 version arguments to Dockerfile.

If anyone needs a Dockerfile for Node 8 there isn't one but it would be easy enough to create one. Just hop into the Dockerfile and change line 9 to:

RUN curl --silent --location https://rpm.nodesource.com/setup_8.x | bash -

Then follow the steps at the bottom of the README.

If someone wants to update the Dockerfile to accept node versions arguments that would be great!

Missing fontconfig file

I have issue in AWS lambda. I use a layer to load canvas but I can't figure out how to load my own fonts to the lambda, maybe someone has already come across this
Fontconfig error: Cannot load default config file

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.