Skip to content

Instantly share code, notes, and snippets.

@ChaituVR
Last active June 3, 2020 11:55
Show Gist options
  • Save ChaituVR/510a60d8c01f9b5d6ad91acfedf3fb61 to your computer and use it in GitHub Desktop.
Save ChaituVR/510a60d8c01f9b5d6ad91acfedf3fb61 to your computer and use it in GitHub Desktop.
Instructions

chartjs-to-image

Instructions to Deploy Script as a AWS Lambda function:

Create a Lambda Function:

  • Login to AWS Console and go to https://console.aws.amazon.com/lambda/home
  • Select Region and click "Create Function" button
  • Select "Auther from Scratch" and Give your function a name (ex: convert-chartjs-to-image)

Image showing Aws Functions

  • It will now create a new Function with the given name, Now in Designer Tab click + Add Trigger Button

Image showing Add trigger button

This Step is Used to create an API to trigger our Function and to use API key (to make it not to be accessable by public):

  • In the Trigger Configuration Page,
  • Select "Api Gateway"
  • In Api Select "Create an Api"
  • Api type is "REST API"
  • Security is "API Key"

Image showing Add Api

Above step will generate a API in the Function Page (Click on Api Gateway in Designer Tab):

API key

Upload the function code to Lambda Function:

In Lambda for files larger than 10 MB, should upload zip using Amazon S3. Following steps will show how to upload to S3 bucket:

  • Click on "Create Bucket" function
  • Give your Bucket a name, region (Make sure same region as Function), permissions, etc and upload the Zip file into it and copy the URL of the zip file (Does't need Public Access)

New Bucket

Copy the URL generated for the ZIP file something like below:

zip link

In the Function Page, Click on function name in Designer Tab:

  • Select "Upload From S3" and enter the zip url and Click "Save"

zip url

Also make sure you have Timeout atleast 10secs and Memory atleast 500MB (recomended)

memory

Trigger Function:

To Test the API give the the following inputs to the endpoint:

Headers
x-api-key:1G8Q9sdltB6XCbXWscx0tcegUsVYH9R8tytaZgTh
Content-Type:application/json
  • Post Body - type: application/json
{
	"height": 100,
	"width": 400,
	"chartjs": "%7B%22type%22:%22bar%22,%22data%22:%7B%22labels%22:%5B%..."
}

Where height and width are canvas width and height, where as chartjs is encoded string of ChartJS options object

To convert options object into string use following:

encodeURI(JSON.stringify({
    type: 'bar',
    data: {
      ......
      ......
    }
}));

postman example

For Future Development:

To Generate zip

we don't need all the packages for Function - so to install only production packages run:

yarn install --production

For Local Development:

yarn install
nodemon local.js
# or
node local.js

Which can be accessable from http://localhost:8000

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment