Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ Icon?
ehthumbs.db
Thumbs.db

layers.zip
root
build
lib
nodejs
20 changes: 7 additions & 13 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM amazonlinux:latest
ARG NODE_VERSION=12
FROM lambci/lambda:build-nodejs${NODE_VERSION}.x

ARG LIBS=/usr/lib64
ARG OUT=/root/layers
Expand All @@ -7,8 +8,7 @@ ARG NODE_VERSION=12
# set up container
RUN yum -y update \
&& yum -y groupinstall "Development Tools" \
&& curl --silent --location https://rpm.nodesource.com/setup_${NODE_VERSION}.x | bash - \
&& yum install -y nodejs gcc-c++ cairo-devel libjpeg-turbo-devel pango-devel giflib-devel
&& yum install -y gcc-c++ cairo-devel libjpeg-turbo-devel pango-devel giflib-devel
# && yum install -y nodejs cairo cairo-devel cairomm-devel libjpeg-turbo-devel pango pango-devel pangomm pangomm-devel giflib-devel

RUN node --version
Expand All @@ -19,23 +19,17 @@ WORKDIR $OUT/nodejs
RUN npm install canvas@next \
chartjs-plugin-datalabels \
chartjs-node-canvas \
chart.js
chart.js --build-from-source

# will be created and become working dir
WORKDIR $OUT/lib

# gather missing libraries
RUN cp $LIBS/libblkid.so.1 . \
&& cp $LIBS/libmount.so.1 . \
&& cp $LIBS/libuuid.so.1 . \
&& cp $LIBS/libfontconfig.so.1 . \
&& cp $LIBS/libpixman-1.so.0 .

RUN curl https://raw.githubusercontent.com/ncopa/lddtree/v1.26/lddtree.sh -o $OUT/lddtree.sh \
&& chmod +x $OUT/lddtree.sh \
&& cp $($OUT/lddtree.sh -l $OUT/nodejs/node_modules/canvas/build/Release/canvas.node | grep '^\/lib' | sed -r -e '/canvas.node$/d') .

WORKDIR $OUT

RUN zip -r -9 node${NODE_VERSION}_canvas_layer.zip nodejs \
&& zip -r -9 node${NODE_VERSION}_canvas_lib64_layer.zip lib

ENTRYPOINT ["zip","-r","-9"]
CMD ["/out/layers.zip", $OUT]
61 changes: 61 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
NODE_VERSION=12

help:
@echo "Usage:"
@echo " make help -- display this help"
@echo " make build -- Build the layers using docker"
@echo " make upload-layers -- Upload the layer to AWS"
@echo " make lambda-tests -- Test the layers in lambda docker"
@echo " make clean -- Remove built layers"

build:
docker build --build-arg NODE_VERSION="${NODE_VERSION}" \
--tag node${NODE_VERSION}-canvas-layers .
mkdir -p build
docker create -ti --name dummy node${NODE_VERSION}-canvas-layers bash
docker cp dummy:/root/layers/node${NODE_VERSION}_canvas_lib64_layer.zip build/
docker cp dummy:/root/layers/node${NODE_VERSION}_canvas_layer.zip build/
docker rm -f dummy

upload-layers: build upload-lib upload-nodejs

upload-lib:
aws lambda publish-layer-version \
--layer-name "node${NODE_VERSION}CanvasLib64" \
--compatible-runtimes nodejs${NODE_VERSION}.x \
--zip-file "fileb://build/node${NODE_VERSION}_canvas_lib64_layer.zip" \
--description "Node canvas lib 64"

upload-nodejs:
aws lambda publish-layer-version \
--layer-name "node${NODE_VERSION}Canvas" \
--compatible-runtimes nodejs${NODE_VERSION}.x \
--zip-file "fileb://build/node${NODE_VERSION}_canvas_layer.zip" \
--description "A Lambda Layer which includes node canvas, chart.js, chartjs-node-canvas, chartjs-plugin-datalabels"

lambda-test: unzip-layers
docker run --rm \
--volume "$$(pwd)":/var/task:ro,delegated \
--volume "$$(pwd)/lib":/opt/lib:ro,delegated \
--volume "$$(pwd)/nodejs":/opt/nodejs:ro,delegated \
lambci/lambda:nodejs${NODE_VERSION}.x test.handler

lambda-test-bash: unzip-layers
docker run --rm -it \
--volume "$$(pwd)":/var/task \
--volume "$$(pwd)/lib":/opt/lib \
--volume "$$(pwd)/nodejs":/opt/nodejs \
lambci/lambda:build-nodejs${NODE_VERSION}.x bash

unzip-layers: build
unzip-layers: nodejs
unzip-layers: lib

nodejs:
unzip build/node${NODE_VERSION}_canvas_layer.zip

lib:
unzip build/node${NODE_VERSION}_canvas_lib64_layer.zip

clean:
rm -rf build lib nodejs
49 changes: 44 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,49 @@ aws lambda publish-layer-version \

## Build

The build script included in this repo will compile new layers and (optionally)
uploaded them to AWS into your default region. Be sure to have Docker installed
then run the follwing command:
Build new layers using the Dockerfile and copy them to the build folder.
Be sure to have Docker installed then run the following command:

```zsh
./build.sh
```
make build
```

To build for a different node version set the `NODE_VERSION`:

```zsh
make build NODE_VERSION=10
```

## Upload

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

```zsh
make upload-layers
```

## Test

A lambda image (e.g. `lambci/lambda:nodejs12.x` ) 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:

```zsh
make lambda-test
```

## Debug

A lambda build image (e.g. `lambci/lambda:build-nodejs12.x`) can be used to
debug any issues with the layers.

Use `make lambda-test-bash` to start an interactive bash session in a lambda
container where you can use `ldd` or [lddtree](https://github.com/ncopa/lddtree)
to examine the `canvas.node` binary:

```zsh
ldd /opt/nodejs/node_modules/canvas/build/Release/canvas.node
```
38 changes: 0 additions & 38 deletions build.sh

This file was deleted.

Binary file modified node12_canvas_layer.zip
Binary file not shown.
Binary file modified node12_canvas_lib64_layer.zip
Binary file not shown.
15 changes: 15 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const { createCanvas } = require('canvas');

exports.handler = async (event) => {
const canvas = createCanvas(200, 200)
const ctx = canvas.getContext('2d')
console.log(event)
ctx.font = '30px Impact'
ctx.fillText('Awesome!', 50, 100)

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