Opencv4nodejs: Recipe for building for AWS lambda with package size limit

Created on 23 Jan 2019  路  4Comments  路  Source: justadudewhohacks/opencv4nodejs

Has anyone successfully built a package for AWS lambda distribution?

The following closed issue discuss that subject, however the resulting package size issue has not beed addressed:

https://github.com/justadudewhohacks/opencv4nodejs/issues/113

I've followed the recipe for building native module here:
https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

But the resulting node_modules ends up being over 1gb, leading to this error from Lambda:
"Unzipped size must be smaller than 262144000 bytes"

Most helpful comment

@mtford90 Any chance you still have a copy of that layer? That link has unfortunately expired.

All 4 comments

Here's how I finally succeeded setting up as a layer with Serverless Framework. Final package size is 46.7Mb

Follow these instructions to build static libraries

https://aws.amazon.com/blogs/compute/nodejs-packages-in-lambda/

Compile module, takes 20-30 mins

install/build opencv4nodejs with static libraries

cd layer/nodejs/
PKG_CONFIG_PATH=~/opencv/lib/pkgconfig/ npm install opencv4nodejs

Install Serverless plugin to avoid having symlinks as duplicate files

npm i --savedev serverless-plugin-zip-symlinks-compress-harder

Remove files not essential to run or not needed

rm -r layer/nodejs/node_modules/opencv-build/opencv/build/share
rm -r layer/nodejs/node_modules/opencv-build/opencv/build/modules
rm -r layer/nodejs/node_modules/opencv-build/opencv/build/3rdparty
rm -r layer/nodejs/node_modules/opencv-build/opencv/build/CMakeFiles
rm -r layer/nodejs/node_modules/opencv-build/opencv/build/downloads
rm -r layer/nodejs/node_modules/opencv-build/opencv/build/include
rm -r layer/nodejs/node_modules/opencv-build/opencv/build/lib64

Moving files based on this https://github.com/justadudewhohacks/opencv-electron/issues/11

mv nodejs/node_modules/opencv-build/opencv/build/lib/* nodejs/node_modules/opencv4nodejs/build/Release/

Serverless.yml

service: lambda-layer-opencv
provider:
name: aws
plugins:

  • serverless-plugin-zip-symlinks-compress-harder
    package:
    artifact: lambda-layer-opencv.zip

layers:
opencv:
path: nodejs
description: nodejs package of Opencv library and dependencies
package:
artifact: lambda-layer-opencv.zip

resources:
Outputs:
OpencvLayerExport:
Value:
Ref: OpencvLambdaLayer
Export:
Name: OpencvLambdaLayer

After a frustrating couple of days here is the layer I came up with for lambdas running 10.15: https://dropfile.nl/get/hK96J

It's quite large (unzips to 159mb...) but thankfully still remained within the limits for my lambda. Might be possible to reduce the size further by deleting unused libraries etc from the node_modules.

@mtford90 Any chance you still have a copy of that layer? That link has unfortunately expired.

After a frustrating couple of days here is the layer I came up with for lambdas running 10.15: https://dropfile.nl/get/hK96J

It's quite large (unzips to 159mb...) but thankfully still remained within the limits for my lambda. Might be possible to reduce the size further by deleting unused libraries etc from the node_modules.

Can you share the copy of your layer again? The link has been expired. Thanks a lot.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

s1hofmann picture s1hofmann  路  7Comments

OluAgunloye picture OluAgunloye  路  3Comments

je3f0o picture je3f0o  路  6Comments

seanquijote picture seanquijote  路  6Comments

developer239 picture developer239  路  5Comments