/chartjs-node-canvas-lambda

This is a docker image to run chartjs-node-canvas module inside aws lambda

Primary LanguageJavaScriptMIT LicenseMIT

node-chart-lambda-image

AWS lambda image to generate different charts using node-canvas and chart.js

Read the article on Medium

Do you need to create line chart or bar chart from your lambda? This lambda docker image is at your service.

There are many use-cases where you need to generate graphs in your email content or slack message. This is different from rendeing graphs on the browser. Using this image you can create your lambda function which will generate the graph and return you the base64 encoded string.

Supported charts

Line chart

linechart

Input to the lambda

{
  "type": "line",
  "labels": ["2017", "2018", "2019"],
  "data": [
    { "label": "Bears", "data": [90, 60, 120] },
    { "label": "Dolphins", "data": [60, 80, 100] },
    { "label": "Whales", "data": [70, 90, 100] }
  ],
  "title": "Wildlife Population"
}

Bar chart

alt

{
  "type": "bar",
  "labels": ["2017", "2018", "2019"],
  "data": [
    {
      "label": "Count",
       "data": [90, 60, 120]
    }
  ],
  "title": "Bear counts by year"
}

Lambda Runtime and Architecture

This lambda supports

  • Node 16.x
  • Node 18.x

This is built on arm 64 bit as it's better than x86 64 bits both in terms of cost and performance. For more read this article

Build and Test locally

As this a docker container, it's easy to run and test on your local computer. All you need to do is build the image, run the container.

make build ENVFILE=.env

Note docker tag in the console. Run the container

docker run --rm -p 8080:8080 node-chart:<tag>

Then invoke the lambda

aws lambda invoke --region us-east-1 --endpoint http://localhost:8080  --no-sign-request --function-name function --cli-binary-format raw-in-base64-out \
 --payload '{"type":"line","labels":["2017","2018","2019"],"data":[{"label":"Bears","data":[90,60,120]},{"label":"Dolphins","data":[60,80,100]},{"label":"Whales","data":[70,90,100]}],"title":"Wildlife Population"}' output.txt


Publish to ECR

make publish ENVFILE=.env