polotno-node 2.9.27 → 2.9.29

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -393,6 +393,153 @@ const makeInstance = async () => {
393
393
  const instance = await makeInstance();
394
394
  ```
395
395
 
396
+ ## AWS lambda with Layer configuration
397
+
398
+ In order to run the **polotno-node** in AWS Lambda, it's recommended to use a [Lambda Layer configurations](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html) which will manage the dependencies, like **chronium**.
399
+ In addition, the Lambda's configuration needs to be changed as well. It's recommeded to change the **timeout** to 30 seconds instead of default value (3 seconds) and **Memory limit** increase to minimum 512 Mb instead of 128 mb ( default value ).
400
+
401
+ Dependencies:
402
+
403
+ - @sparticuz/chromium
404
+ - puppeteer-core
405
+ - polotno-node
406
+
407
+ Pre-requirements:
408
+
409
+ - The chronium and puppeteer versions need to be satisfied. Please check this [document](https://pptr.dev/supported-browsers).
410
+ - The **minumum Memory limit** needs to be `512 Mb` for AWS Lambda. Default is `128 Mb`.
411
+ - The **timeout** should be increased from default 3 seconds to higher value, for example up to 30 seconds. It depends on the Memory limit. For instance, if the memory limit was set to 1024, the polotno process might take ~15 secs, if 512 mb, ~30sec.
412
+
413
+ ==========================================
414
+
415
+ How to create a configuration layer with a dependency on `chronium` ?
416
+
417
+ 1. Create a `.zip` file from a chronium project. For example:
418
+
419
+ ```
420
+ mkdir chronium-112 && cd chronium-112
421
+ npm init -y
422
+ npm install @sparticuz/chromium@112.0
423
+ zip -r chronium.zip ./*
424
+ ```
425
+
426
+ 2. Go to AWS console then open a Lambda section and click on `Layers`.
427
+ 3. Following the [documentation](https://docs.aws.amazon.com/lambda/latest/dg/chapter-layers.html) create a Layer with a chrnioum dependency by uploading a zip file. Keep in mind that environment like `nodejs18.x` should match between layer and function.
428
+
429
+ > Approximatelly, the size of the zip, will be around `73 Mb`, so in order to upload, the S3 service will be needed. 4. Finally, open the Lambda function, select a `Code` section, at the bottom click on `Add Layer` and select a created layer.
430
+
431
+ ==========================================
432
+
433
+ How to run the full example with aws-cli ?
434
+
435
+ - Create a Lambda configuration layer, and upload a **.zip** archive which stores a **chronium** npm or git package.
436
+
437
+ ```
438
+ export CH_VERSION=112.0
439
+ export BUCKET=example-bucket
440
+ export AWS_ACC_ID=000000000000
441
+ // create a chronium project for Layer
442
+ mkdir chronium-layer
443
+ cd chronium-layer
444
+ npm init -y
445
+ npm install @sparticuz/chromium@${CH_VERSION}
446
+ zip -r chronium.zip ./*
447
+ aws s3 cp chronium.zip s3://${BUCKET}/lambda-layers/chronium-${CH_VERSION}.zip
448
+ aws lambda publish-layer-version \
449
+ --layer-name chronium-polotno \
450
+ --content "S3Bucket=${BUCKET},S3Key=lambda-layers/chronium-{CH_VERSION}.zip" \
451
+ --description Chronium-dev-${CH_VERSION} \
452
+ --compatible-architectures x86_64 \
453
+ --compatible-runtimes nodejs18.x
454
+ ```
455
+
456
+ - Create a JS file for a Lambda's handler
457
+
458
+ ```
459
+ // create a folder
460
+ mkdir handler && cd handler
461
+ // init a node project
462
+ npm init -y
463
+ // install dependencies
464
+ npm install --save polotno-node puppeteer-core@19.8.0
465
+ // create a file handler
466
+ touch index.mjs
467
+ // add code example
468
+ echo "
469
+ import chromium from "@sparticuz/chromium";
470
+ import puppeteer from "puppeteer-core";
471
+ import { createInstance } from "polotno-node";
472
+ export const handler = () => {
473
+ const browser = await puppeteer.launch({
474
+ args: [
475
+ ...chromium.args,
476
+ "--no-sandbox",
477
+ "--hide-scrollbars",
478
+ "--disable-web-security",
479
+ "--allow-file-access-from-files",
480
+ "--disable-dev-shm-usage",
481
+ ],
482
+ defaultViewport: chromium.defaultViewport,
483
+ executablePath: await chromium.executablePath(),
484
+ headless: true,
485
+ ignoreHTTPSErrors: true,
486
+ });
487
+ const polotnoInstance = await createInstance({
488
+ key: process.env.POLOTNO_API_KEY,
489
+ browser,
490
+ });
491
+ const body = polotnoInstance.jsonToImageBase64(event.json)
492
+
493
+ return {
494
+ StatusCode: 200,
495
+ headers: {
496
+ 'Content-Type': 'image/png'
497
+ },
498
+ body,
499
+ }
500
+ };
501
+ " >> index.mjs
502
+ // create file with variables
503
+ echo '{
504
+ "Variables": {
505
+ "POLOTNO_API_KEY": "your-api-key"
506
+ }
507
+ }
508
+ ' >> vars.json
509
+ // archive the folder
510
+ zip -r index.zip ./*
511
+ // copy to S3
512
+ aws s3 cp index.zip s3://${BUCKET}/polotno-handler.zip
513
+ // crete a trust policy
514
+ echo '{
515
+ "Version": "2012-10-17",
516
+ "Statement": [
517
+ {
518
+ "Effect": "Allow",
519
+ "Principal": {
520
+ "Service": "lambda.amazonaws.com"
521
+ },
522
+ "Action": "sts:AssumeRole"
523
+ }
524
+ ]
525
+ }
526
+ ' >> trustPolicy.json
527
+ // create roles and policies
528
+ aws iam create-role --role-name lambda-polotno-role --assume-role-policy-document file://trustPolicy.json
529
+ aws iam attach-role-policy --role-name lambda-polotno-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
530
+ aws lambda create-function --function-name lambda-polotno \
531
+ --runtime nodejs18.x \
532
+ --zip-file fileb://index.zip \
533
+ --layers "arn:aws:lambda:eu-central-1:${AWS_ACC_ID}:layer:chronium-polotno:1" \
534
+ --handler index.handler \
535
+ --environment file://vars.json \
536
+ --role "arn:aws:iam::${AWS_ACC_ID}:role/lambda-polotno-role" \
537
+ --timeout 60 \
538
+ --memory-size 512
539
+ // try it
540
+ aws lambda invoke --function-name lambda-polotno --cli-binary-format raw-in-base64-out --payload file://polotno-project-json.json image
541
+ ```
542
+
396
543
  ## Troubleshooting
397
544
 
398
545
  If you have an error like this