rpidanny / gm-lambda-layer

AWS Lambda layer with GraphicsMagick binaries.
http://www.graphicsmagick.org
MIT License
68 stars 24 forks source link

how to implement this layer? #7

Closed dfloresgonz closed 3 years ago

dfloresgonz commented 3 years ago

Hi,

I'm trying to use this layer as soon as I do require("gm") I get error: Cannot find module what is the exact thing i need to do?

{ "errorType": "Error", "errorMessage": "Cannot find module 'gm'", "code": "MODULE_NOT_FOUND", "stack": [ "Error: Cannot find module 'gm'", " at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)", " at Function.Module._load (internal/modules/cjs/loader.js:562:25)", " at Module.require (internal/modules/cjs/loader.js:692:17)", " at require (internal/modules/cjs/helpers.js:25:18)", " at Runtime.exports.handler (/var/task/index.js:70:14)", " at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)" ] }

thanks.

github-actions[bot] commented 3 years ago

Message that will be displayed on users' first issue

rpidanny commented 3 years ago

Hey @dfloresgonz , take a look at the example here https://github.com/rpidanny/gm-lambda-layer/tree/master/test

This layer doesn't include node_modules, it's generic gm binary

dfloresgonz commented 3 years ago

Hello @rpidanny

Thanks a lot I could finally get a thumbnail from a pdf I had to use this layer also: https://github.com/shelfio/ghostscript-lambda-layer

exports.handler = async (event) => {
    // TODO implement
    const { execSync } = require('child_process');
    const fs = require('fs');

    const res = await getObject_S3('mybucket', `my_pdf_file.pdf`); // custom function
    fs.writeFileSync('/tmp/tmp_pdf.pdf', res.data.Body);

    execSync('gm convert -thumbnail x600 -background white /tmp/tmp_pdf.pdf[0] /tmp/thumbnail.png', { encoding: 'utf8', stdio: 'inherit' });
    console.log('converted.');
    const bffr = fs.readFileSync('/tmp/thumbnail.png');
    await uploadFile_S3('mybucket', 'thumbnail.png', bffr); // custom function

    const response = {
        statusCode: 200,
        body: JSON.stringify('Hello from Lambda!'),
    };
    return response;
};

Thanks again.