- 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)
- It will now create a new Function with the given name, Now in Designer Tab click
+ 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"
Above step will generate a API in the Function Page (Click on Api Gateway in Designer Tab):
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)
Copy the URL generated for the ZIP file something like below:
In the Function Page, Click on function name in Designer Tab:
- Select "Upload From S3" and enter the zip url and Click "Save"
Also make sure you have Timeout atleast 10secs and Memory atleast 500MB (recomended)
To Test the API give the the following inputs to the endpoint:
- Method - POST
- EndPoint URL - Can find this from API Gateway Details Page (As shown in https://i.imgur.com/4L7ib6S.png)
- API Access Key - Can find this from API Gateway Details Page (As shown in https://i.imgur.com/4L7ib6S.png)
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: {
......
......
}
}));
we don't need all the packages for Function - so to install only production packages run:
yarn install --production
yarn install
nodemon local.js
# or
node local.js
Which can be accessable from http://localhost:8000